0

I am trying to create a php form where data in each row can be submitted independently. I am able to create a form which looks similar to the image below. enter image description here

How to configure the submit buttons such that when pressed, the entry in that particular row alone gets posted to the server. I am very new to PHP

Relevant php code:

while ($driverEntries = mysqli_fetch_row($driverList)) {
        printf ("%s \n",  $driverEntries[1]);
        echo "<input name=\"subject\" type=\"text\" id=\"subject\" size=\"50\">";
        echo "<input type=\"submit\" name=\"Submit\" value=\"Submit\">";
        echo "<br>";
 } 
4

4 回答 4

3

尝试这个:

$a=0;
while ($driverEntries = mysqli_fetch_row($driverList)) {
    printf ("%s \n",  $driverEntries[1]);
    echo "<form method=\"post\" action=\some_page.php\">";
    echo "<input name=\"subject\" type=\"text\" id=\"subject_$a\" size=\"50\">";
    echo "<input type=\"submit\" name=\"Submit\" value=\"Submit\">";
    echo "<br>";
    echo "</form>";
    $a++;
}   

每个输入一个表格。注意$a自动增量以使 id 不同

Ajax 版本(使用 JQUERY)

$a=0;
while ($driverEntries = mysqli_fetch_row($driverList)) {
    printf ("%s \n",  $driverEntries[1]);
    echo "<input name=\"subject\" type=\"text\" id=\"subject_$a\" size=\"50\">";
    echo "<span onclick=\"update($a)\">Update</span>";
    echo "<br>";
    $a++;
} 

<script>
 function update(a){
   subject=$('#subject_'+a).val();
    $.post('your_form_page_processor.php', 
    {subject:subject}, 
    function(result){
        alert(result);
    });
 }
</script>

在 your_form_page_processor.php

$subject=isset($_POST['subject'])?$_POST['subject']:NULL;

if(!empty($subject)){
//do something with $subject
}else{
echo 'Subject cannot be empty'; 
}

取决于你想做什么

您应该阅读jquery.ajax及其辅助函数

于 2013-08-26T20:05:28.417 回答
2

你可以这样做:

while ($driverEntries = mysqli_fetch_row($driverList)) {
        printf ("%s \n",  $driverEntries[1]);
        echo "<form method='post'>";
        echo "<input name=\"subject\" type=\"text\" id=\"subject\" size=\"50\">";
        echo "<input type=\"submit\" name=\"Submit\" value=\"Submit\">";
        echo "</form>";
        echo "<br>";
 }

但是不要忘记删除主表单,因为现在我们为每一行使用了不同的表单。

于 2013-08-26T20:05:44.553 回答
0

您可以将每个输入放入自己的表单中,也可以使用 AJAX 请求

于 2013-08-26T20:04:01.847 回答
0

您最好的选择是将每一行输入分开到一个单独的表单中,然后使用 AJAX 从页面中的 php 表单发送和接收信息。这是一个很好的教程http://www.w3schools.com/php/php_ajax_intro.asp希望对您有所帮助!

于 2013-08-26T20:05:59.673 回答