从PHP保存动态生成的文本字段 [英] Save dynamically generated textfield from PHP
问题描述
我有一个基于用户输入动态生成的表,在某些情况下,行具有文本字段,并且我想将用户更新的值保存在MySql数据库中的文本字段中.例如:
I have a table which is being dynamically generated based on the user input, in some cases rows have a textfield and i want to save the values updated by the user in the textfield by in the MySql database. e.g:
实际代码:
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'
现在,我想保存在文本字段中输入的值,并在数据库中更新它们.问题是我不确定如何定义可以使用JavaScript读取的id
(文本字段的ID),以便知道我当前在谈论哪个文本字段.
Now i want to save the values entered in the textfield and update them in the database. The problem is i am not sure how to define an id
(id of the textfield) which can be read using a javascript so that i know which textfield i am currently talking about.
任何建议都会有所帮助.
Any suggestions would help.
推荐答案
您的输出应类似于(仅用于读取 的换行符-不适用于实际代码)
Your output should look like (linebreaks for reading only -- not for use in actual code)
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函数传递行"的值ID.
Now you use jQuery's AJAX function to pass over the value ID of the 'row'.
$(".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文件,我们可以解析所需的数据,并使用它来相应地更新数据库.
Now that we've passed the ID of the input field (which is also associated directly to the row we want to update) to our PHP file, we can parse the data we need and use it to update the database accordingly.
<?php
//include our sql.connect file
$ourId = $_POST['id'];
$updatedText = $_POST['text'];
$q = mysql_query("UPDATE 'our_table' SET received='".$updatedText."' WHERE id = ".$ourId);
?>
我希望这就是你想要的.
I hope this is what you want.
编辑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文件中的行生成的唯一类.还更改了按键功能的选择器,使其仅反映我们唯一生成的行中的 中的输入.
Changed to reflect a unique class generated only by the rows in our PHP file. Also changed the keypress function's selector to reflect only inputs within our uniquely generated rows.
这篇关于从PHP保存动态生成的文本字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!