保存PHP动态生成的文本字段


Save dynamically generated textfield from PHP

我有一个表,它是根据用户输入动态生成的,在某些情况下,行有一个文本字段,我想将用户更新的值保存在MySql数据库的文本字段中。例如:

实际代码:

echo "<tr><td>".$sql3[0]."</td><td>".$row1[2]."</td>
      <td><input type='text' class='span1' value='".$recieved."'/>
      </td><td>".$status."</td></tr>";

简化示例:

//some user input e.g Microsoft
Table: 
item             quantity      received
Windows 7          1000       'textfield'
Office 2007         200        nothing required
Windows 8          20000      'textfield'

现在我想保存在文本字段中输入的值,并在数据库中更新它们。问题是我不知道如何定义一个id(文本字段的id),它可以使用javascript读取,这样我就知道我现在谈论的是哪个文本字段。

任何建议都会有所帮助。

您的输出应该看起来像(换行符,仅用于读取--不用于实际代码

var .= '<tr class="someClass">';
var .=   '<td>'.$row["item"].'</td>';
var .=   '<td>'.$row["quantity"].'</td>';
var .=   '<td>';if(!empty($row["received"])){ var .= '<input type="text" value="'.$row["received"].'" id="'.$row['id'].'" />'; }else{ var.= '<input type="text" value="default_value" id="'.$row['id'].'"/>'; } 
var .=   '</td>';
var .= '</tr>';
return var;

现在您使用jQuery的AJAX函数来传递"row"的值ID。

$(".someClass input").live('keypress', function(e){
  var code = (e.keyCode ? e.keyCode : e.which);
  if(code == 13){ // if we pressed enter
    e.preventDefault(); // stop the default submission behavior
    var ourId = this.id;
    var updatedText = $(this).val();
    $.ajax({
      type: 'POST',
      url: 'path/to/my/update/file.php',
      data: 'id='+ourId+'text='+updatedText,
      success: function(data){
        //our post to the file was successful. 
        //The file returns data and that data is available as an object.
        //We declare it as 'data' in the success: function(data) string.
       $("#someContainer").append(data); //append our data returned to #someContainer
      }
    });
  }
});

既然我们已经将输入字段的ID(它也直接与我们想要更新的行相关联)传递到了PHP文件中,我们就可以解析我们需要的数据,并使用它来相应地更新数据库。

<?php
  //include our sql.connect file
  $ourId = $_POST['id'];
  $updatedText = $_POST['text'];
  $q = mysql_query("UPDATE 'our_table' SET received='".$updatedText."' WHERE id = ".$ourId);
?>

我希望这是你想要的。

编辑1--每个用户请求

var .= '<td>'; //open the data cell    
//the below line checks to see if the row 'received' is **NOT** a empty database result
if(!empty($row["received"])){
  //our row is not empty, change the value of the input field to our $row['received'] value
  var .= '<input type="text" value="'.$row["received"].'" id="'.$row['id'].'" />'; 
}else{ 
  //our row was empty, or our row returned null. either way, we use "default_value" as our value, which will literally print as a textbox with the words default_value in it.
  var.= '<input type="text" value="default_value" id="'.$row['id'].'"/>'; 
} 
var . = '</td>'; //close the data cell

编辑2

更改以反映仅由PHP文件中的行生成的唯一类。还更改了按键函数的选择器,以仅反映我们唯一生成的行中的输入

在HTML中添加名称:

<input type='text' class='span1' name="received[]" value='".$recieved."'/>

在PHP中:

$received = $_REQUEST['received']
print_r($received); 

你就会知道该怎么做。

或者,可以执行received[$id],其中$id是行的主要标识符。

您可以尝试

item_id     mediumint(8)    

甚至int

这将足够长,但你也需要usr_id或dunno,如果你已经考虑到了,那就足够了。