2017-07-18 96 views
1

這裏是我的代碼:將數據插入數據庫無需重新加載頁面(PHP + jQuery的)

<form action='insert.php' method='post' id='myform' > 
    <input type='hidden' name='tmdb_id'/> 
    <button id='insert'>Insert</button> 
    <p id='result'></p> 
    <script src='insert.js'></script> 
</form> 

<form action='insert.php' method='post' id='myform' > 
    <input type='hidden' name='tmdb_id'/> 
    <button id='insert'>Insert</button> 
    <p id='result'></p> 
    <script src='insert.js'></script> 
</form> 

<form action='insert.php' method='post' id='myform' > 
    <input type='hidden' name='tmdb_id'/> 
    <button id='insert'>Insert</button> 
    <p id='result'></p> 
    <script src='insert.js'></script> 
</form> 

這裏是:insert.js

$('#myform').submit(function(){ 
    return false; 
}); 

$('#insert').click(function(){ 
    $.post(  
     $('#myform').attr('action'), 
     $('#myform :input').serializeArray(), 
     function(result){ 
      $('#result').html(result); 
     } 
    ); 
}); 

問題:

只有代碼首先在<form></form>標籤內工作。如果我點擊其他<form></form>標籤的submit按鈕,則我將重新定向到insert.php文件。

什麼問題?如果它與同一個id相關,那麼我不想添加不同的id's。對於每一個新形式

+2

[ID必須是唯一的(http://stackoverflow.com/questions/5611963/can-multiple-different-html-elements-have-the-same-id-if-theyre-different-eleme),特別是因爲它會導致[JavaScript]中的問題(https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/id)和CSS嘗試與這些元素進行交互。 –

+0

[您是否觀看過瀏覽器開發人員工具中的AJAX請求/響應?你有沒有在項目中包含jQuery庫?是否有任何錯誤報告?您是否在網絡服務器上運行此功能?](http://jayblanchard.net/basics_of_jquery_ajax.html) –

+0

ID必須是唯一的。也許使用類而不是? – JustBaron

回答

-2

嘗試類似下面使用jQuery的Ajax:

$(".submit-button-class").click(function() { 
    var formData = $(this).closest('form').serialize(); 
    $.ajax({ 
    type: "POST", 
    url: "YOUR URL", 
    data: formData, 
    dataType: 'json', 
    beforeSend: function() { 
     //show loading image while processing 
    }, 
    success: function(resp) { 
     // do something here 
    }, 
    error: function(e) { 
     alert('error: ' + JSON.stringify(e)); 
    } 
    }); 
}); 

注意:你應該使用類選擇,而不是id選擇的提交按鈕

+0

問題中提到的錯誤相同。只有第一種形式可行。 – Toby

+0

@Toby請在回答中查看我的「註釋」。使用類選擇器而不是id選擇器 –

+0

雖然此代碼可能回答此問題,但提供有關爲什麼和/或此代碼如何回答此問題的其他上下文會提高其長期價值。 –

1

ID's Must Be Unique,特別是因爲它會導致當您嘗試與這些元素進行交互時,JavaScript和CSS中存在問題。

假設你一次加載insert.js到頁面:

<form action='insert.php' method='post' class='myform' > 
    <input type='hidden' name='tmdb_id'/> 
    <button class='insert'>Insert</button> 
    <p class='result'></p> 
</form> 

<form action='insert.php' method='post' class='myform' > 
    <input type='hidden' name='tmdb_id'/> 
    <button class='insert'>Insert</button> 
    <p class='result'></p> 
</form> 

<form action='insert.php' method='post' class='myform' > 
    <input type='hidden' name='tmdb_id'/> 
    <button class='insert'>Insert</button> 
    <p class='result'></p> 
</form> 

一旦你有做,你可以使用一個jQuery函數來處理所有的形式:

$(function() { 
    $('.insert').click(function(e){ 
     e.preventDefault(); 
     $.post(  
      $(this).closest('.myform').attr('action'), 
      $(this).closest('.myform :input').serializeArray(), 
      function(result){ 
       $(this).closest('.myform').find('.result').html(result); 
      } 
     ); 
    }); 
}); 

你做必須做一些DOM遍歷來獲取與插入按鈕相關的表單元素。

  1. $(this).closest('.myform')發現插入按鈕
  2. $(this)closest(.myform').find('.result')的父母找到與一流的「結果」的子元素結果添加到。
+0

它重新引導我insert.php(即使是第一種形式)先生 – Toby

+1

檢查語法錯誤 – charlietfl

+0

一切都是正確的先生 – Toby

0

你並不需要所有的形式和內容,而不是ID的重複的insert.js

使用通用類的多個實例。

我們還可以使用提交事件發佈數據

HTML

<form action='insert.php' method='post' class='myform'> 
    <input type='hidden' name='tmdb_id' /> 
    <button class='insert'>Insert</button> 
    <p class='result'></p> 

</form> 

<form action='insert.php' method='post' class='myform'> 
    <input type='hidden' name='tmdb_id' /> 
    <button class='insert'>Insert</button> 
    <p class='result'></p> 

</form> 
<!-- load once per page --> 
<script src='insert.js'></script> 

JS

$('.myform').submit(function(){ 
    // `this` is the instance of myForm class the event occurred on 
    var $form = $(this), 
     url = $form.attr('action'), 
     data = $form.serializeArray(); // or use serialize() which is more common 

    $.post(url, data, function(result){ 
     // look inside this form instance for element to populate 
     $form.find('.result').html(result); 
    }); 

    return false; 
}); 
+0

它重定向到insert.php即使是第一種形式)先生 – Toby

+0

確保代碼包裝在'$(function(){/ * code here * /})'中,並檢查瀏覽器控制檯是否存在任何錯誤 – charlietfl

+0

是的,它是先生 – Toby

相關問題