0

動的に作成された入力フィールドからデータベース内の複数の行を正常に更新する方法について、いくつかのアドバイスが必要です。

だから、私が持っているのはこれです:

PHP

<?php
   while ($row = mysql_fetch_array($sql)) {
       echo "<input class='estemated_days' type='text' id='".$row['scpe_id']."' value='".$row['scpe_estemated_days']."'></td>";
   }
?>

これは次のようなものを出力します:

HTML

<input class='estemated_days' type='text' id='718' value='5'>
<input class='estemated_days' type='text' id='719' value='8'>
<input class='estemated_days' type='text' id='720' value='10'>

<input type='button' id='save' value='Save'> <!-- Button to jQuery -->

.....等。

これが私の知識が不足しているところです。jQueryに次のようなことをさせたい:

jQuery

($"#save").click(function () {

  // Get value of id from (".estemated_days") as an identifier, and get the input value it contains
  // Send to /update.php

});

次に、次のupdate.phpようなことを行います:

PHP

<?php

if (isset($_POST['save'])) {

    /*
        Get all of the id's and the value it contain's

        perform:
    */

    mysql_query = ("UPDATE myDatabase SET estemated_days = '$the_value_from_the_input' WHERE scpe_id = '$the_value_of_the_id'");
    //Repeat this for all rows from the webpage
}

?>

私の知識は基本的なWebプログラミングですが、これを機能させたいと思っています。誰かが私がそれをどのようにすべきかについてアドバイスを受けましたか?

4

1 に答える 1

1
var values = {};
$('input.estimated_days').each(function(n, el){
   values[ $(el).attr('id') ] = $(el).val();
});

$.ajax(
  {
    type : 'POST',
    url : 'update.php',
    data : {edays: values}
  }
); /// see jquery docs for ajax callbacks 

<?php
   foreach($_POST['edays'] as $id=>$value) // ...

http://api.jquery.com/jQuery.ajax/http://api.jquery.com/attr/

...そしてmysqlの入力をサニタイズすることを忘れないでください

于 2012-06-18T23:27:10.650 に答える