【发布时间】:2014-10-06 02:26:29
【问题描述】:
我有一个页面,用户可以在其中将记录添加到 mysql 数据库。在同一页面上有一个数据库中现有记录的列表。
我使用ajax将记录添加到数据库,对不起旧版本的mysql,我先让这个东西工作后会更新:
$(document).ready(function(){
//on the click of the submit button
$("#btn_submit").click(function(){
//get the form values
var username = $('#hdn_username').val();
var name = $('#txt_name').val();
var brand = $('#txt_brand').val();
//make the postdata
var postData = 'username='+username+'&name='+name+'&brand='+brand;
//call your input.php script in the background, when it returns it will call the success function if the request was successful or the error one if there was an issue (like a 404, 500 or any other error status)
$.ajax({
url : "input.php",
type: "POST",
data : postData,
success: function(data,status, xhr)
{
//if success then just output the text to the status div then clear the form inputs to prepare for new data
$("#status_text").html(data);
$('#name').val('');
$('#brand').val('');
},
error: function (jqXHR, status, errorThrown)
{
//if fail show error and server status
$("#status_text").html('there was an error ' + errorThrown + ' with status ' + textStatus);
}
});
}); });
我的html是:
<table class="simplet" width="640" border="0" cellspacing="0" cellpadding="0">
<thead>
<tr>
<th width="300" colspan="2">Product Name</th>
<th width="150">Brand</th>
<th width="100">Quantity</th>
<th width="60">Price</th>
<th width="30">Delete</th>
</tr>
</thead>
<tbody>
<tr><td colspan="6"><div id="status_text_list" /></td></tr>
[insert_php]
$current_user= wp_get_current_user();
$username= $current_user->user_login;
mysql_connect("localhost","xxxxx","xxxx");//database connection
mysql_select_db("xxxx");
$query= "SELECT * FROM wp_userdata WHERE username='$username'";
$result= mysql_query($query);
if (!$result) { die("Error db request: <br />". mysql_error()); }
while ($result_row = mysql_fetch_array($result, MYSQL_ASSOC)){ echo '<tr class="odd-row"><td width="30"></td><td>'.$result_row['product_name'].'</td><td>'.$result_row['product_brand'].'</td><td>'.$result_row['product_quantity'].'</td><td>'.$result_row['product_link'].'</td><td><a class="link-delete" href="#" data-delete-id="' . $result_row['id'] . '">X</a></td></tr>';
}
[/insert_php]
</tbody></table>
<br />
<table border="1">
<tr>
<td align="center">Add Products</td>
</tr>
<tr>
<td>
<form onsubmit="return false">
<table>
<input type="hidden" id="hdn_username" name="username" value="[insert_php]echo $username;[/insert_php]">
<tr>
<td>Product Name</td>
<td><input type="text" id="txt_name" name="name" size="50">
</td>
</tr>
<tr>
<td>Brand</td>
<td><input type="text" id="txt_brand" name="brand" size="50">
</td>
</tr>
<tr>
<td></td>
<td><div id="status_text" /></td>
</tr>
<tr>
<td></td>
<td align="right"><input type="submit" id="btn_submit" name="submit" value="Submit"></td>
</tr>
</form>
</table>
</td>
</tr>
</table>
而我的 input.php 是:
mysql_connect("localhost","xxxx","xxxx");//database connection
mysql_select_db("xxxx");
//inserting data order
$order = "INSERT INTO wp_userdata
(username, product_name, product_brand)
VALUES
('$_POST[username]',
'$_POST[name]',
'$_POST[brand]')";
//declare in the order variable
$result = mysql_query($order); //order executes
if($result){
echo ("DATA SAVED SUCCESSFULLY");
} else{
echo("<br>Input data is fail");
}
当我点击提交按钮时,表单值被发送到 input.php 并显示成功消息,你可以在这里看到它 - http://suppliesprices.com/?page_id=4
我想要实现的是在不重新加载页面的情况下将新的数据行添加到显示的列表中。
我想用 javascript 渲染它并添加到成功的 ajax 调用中,但不确定它是否可以工作。
如何更改我的代码以获得所需的效果?
【问题讨论】:
标签: javascript php jquery mysql ajax