0

PHPループに複数のフォームを入力し、<form id='X'> それらを次のように変更します。HTMLでは次のようになります。

<form id='34' class='customer_contact' name='customercontact' method='post' action='customerUpdate.php'>
    Förnamn: <br /><input type='text' name='contact_firstname' class='textbox' value='text....'>
    Efternamn: <br /><input type='text' name='contact_lastname' class='textbox' value='text....'>
    Telefonnummer: <br /><input type='text' name='contact_phone' class='textbox' value='text....'>
    Mobiltelefon: <br /><input type='text' name='contact_cellphone' class='textbox' value='text....'>
    E-mail: <br /><input type='text' name='contact_email' class='textbox' value='text....'>
    <select name='isActive'>
        <option value="0" selected>Inaktiv</option>
        <option value="1">Aktiv</option>
    </select>
</form>

そして、私は次のコードを使用してそれらをシリアル化しようとします:

<script type="text/javascript">
$(document).ready(function() {
$('#customer_contact_save').live('click', function(e){
    e.preventDefault();
    $.each($('form.customer_contact'), function(index) { 
        var sData = $(this).serialize();
        $.ajax({
            type: "POST",
            url: "updateCustomer.php",
            data: sData,
            success: function(someMessageFromPhp) {
                alert(someMessageFromPhp); 
            }
        });
    });
  });
});

(そのコードを提供してくれたSOのZagor23に感謝します)

しかし、私のPOSTデータは空です(FireBugを使用して何が送信されているかを確認しています)。

スクリプトに追加しようとしconsole.log(sData);ましたが、FireBugは次を返します:(an empty string)そしてphpvar_dump($_POST);も空です。

誰かが何が悪いのか考えましたか?

4

3 に答える 3

8

実際のコードではname、入力フィールド(facepalm)に追加するのを忘れていたことがわかりました。

とにかくありがとう!

于 2012-07-17T08:27:12.460 に答える
1

here is a fiddle i've made of your code - http://jsfiddle.net/7Y9vn/ if you turn firebug on and press the button you'll see both request are runing ok, so you should probably search for an error other code o the page

于 2012-07-17T06:17:15.047 に答える
1

このようなものはどうですか?

$(".customer_contact").each(function()
{ 
    var form_id = $(this).attr("id");
    var sData = $("#" + form_id).serialize();
    $.ajax({
        type: "POST",
        url: "updateCustomer.php",
        data: sData,
        success: function(someMessageFromPhp) {
            alert(someMessageFromPhp); 
        }
    });
});
于 2012-07-16T19:21:23.403 に答える