Order ofQuery with AJAX PHP and MySQL
Order ofQuery with AJAX PHP and MySQL
情况有点混乱,我的网页上有一个表单,允许用户将信息输入数据库......
形式
<form id="insertbill">
Total <input type="text" id="total" name="total" /><br />
Bill name<input type="text" id="bill-name" name="bill-name" /><br />
bill descriptiion <input type="text" id="bill-description" name="bill-description" /><br />
bill colour<input type="text" id="bill-colour" name="bill-colour" />
<input type="button" value="submit" onClick="insertBill();" />
</form>
然后,此表单通过AJAX将信息发送到我的php,然后将其输入到我的数据库中
阿贾克斯
function insertBill()
{
$.post('insert_bill.php', $('#insertbill').serialize(),
function(data) {
$('#bills').append(data);
});
};
.PHP
$uid = $_SESSION['oauth_id'];
$bill = mysql_real_escape_string($_POST['total']);
$billname = mysql_real_escape_string($_POST['bill-name']);
$billdescription = mysql_real_escape_string($_POST['bill-description']);
$billcolour = mysql_real_escape_string($_POST['bill-colour']);
#Insert Record
$query = mysql_query("INSERT INTO `outgoings` (user_id, bill, bill_name, bill_description, bill_colour ) VALUES ('$uid', '$bill', '$billname', '$billdescription', '$billcolour')") or die(mysql_error());
完成此操作后,数据将返回到网页,并使用允许用户更新记录的另一种形式...
返回的数据/表单
Print "<tr>";
Print "<th>total:</th> <td>".$bill . "</td> ";
Print "<th>bill name:</th> <td>".$$billname . "</td> ";
Print "<th>bill deposit:</th> <td>".$billdescription . "</td> ";
Print "<th>colour:</th> <td>". $billcolour . " </td></tr>";
echo "<th>edit:</th> <td>
<form id='bill-upd'>
<input type='hidden' value='". $billname."' name='billid' id='billid''>
Total <input type='text' id='total' name='total' /><br />
Bill name<input type='text' id='bill-name' name='bill-name' /><br />
bill descriptiion <input type='text' id='bill-description' name='bill-description' /><br />
bill colour<input type='text' id='bill-colour' name='bill-colour' />
<input type='button' value='submit' onClick='updateBill();' />
</form>
</td>";
我的问题是返回的表单没有更新,这是因为我需要以某种方式找到插入的记录的 id,将其放入我的表单中以返回我认为......我希望这不会太令人困惑,但是有没有人有更好的方法在不重新加载页面的情况下做到这一点?
以下是
您可以在insert_bill.php视图页面上包含的内容:
<script>
var json=<?
$arr=array($bill, $billname, $billdescription, $billcolour);
echo json_encode($arr); ?>;
$('tr td:nth-child(0)').html(json[0]);
$('tr td:nth-child(1)').html(json[1]);
$('tr td:nth-child(2)').html(json[2]);
$('tr td:nth-child(3)').html(json[3]);
</script>
我想一个悬而未决的问题是你说从你的数据库中返回的"id"。你能进一步解释一下吗?您的insert_bill.php页面上是否有多种表单?无论如何,您可以将该$id
放入数组中并将其输入并html()
函数,就像我对其他变量所做的那样。
您需要从数据库返回数据然后进行更改,我想如果您只需要id,则必须这样做。
在 PHP 中回显mysql_last_insert_id();所以你得到最后插入的id并"返回"到JS中在 JS 中,函数数据内部您获得回显作为返回并更改 id然后 $("#billid").val(data)
希望对您有所帮助
相关文章:
- JavaScript Arrays and MySQL
- Mysql and onbeforeunload
- Interfacing html, javascript, php and mysql
- chartJs with MySQL and PHP
- filtet data by checkbox using jquery ajax mysql and php
- jQuery $.each loop and json mysql data
- jade, express, and mysql results
- module.exports scope and node-mysql
- Javascript and PHP promt for MySQL
- Working with Node and MySQL
- JavaScript, PHP and MySQL
- jQuery UI Autocomplete append with PHP and MySQL
- jquery autocomplete javascript, php and mysql
- Order ofQuery with AJAX PHP and MySQL
- PHP AJAX and MySQL
- Javascript, PHP and MySQL relations
- $jquery.post() PHP and Mysql
- javascript and MySQL tables
- PHP, AJAX, and Mysql
- node-webkit and mysql queries