我已经使用ajax创建了一个排序系统,但是将id从ajax页面发送到数据库时遇到问题。
使用下面的代码用于HTML部分,jquery部分和ajax部分,我希望提出一个解决方案。
所以首先是HTML部分。这将列出特定分销商的所有到期交货项目。分发服务器 ID 保存在会话中。
<td class="main" width="10%"><a href="#" id="orderAllocate"><img src="images/users.png" width="25" height="25" border="0" alt=""></a></td>
单击此按钮将打开一个 jQuery 对话框,其中包含分销商的所有不同员工。代码如下:
$("a#orderAllocate").click(function(e) {
e.preventDefault();
if($("#Allocate").length > 0) {
$("#Allocate").remove();
}
$("<div id='Allocate' title='Please Allocate The Order'></div>").dialog({
autoOpen: true,
width:350,
height:360,
resizable: false,
stack: true,
modal: true,
open: function(event, ui) {
$('#Allocate').load("ajax.php?action=allocate_delivery", function (){
$(this).find('button').button();
});
$("#CancelAllocation").button({icons: {primary: "ui-icon-close"}}).live ("click", function() {
$( "#Allocate" ).dialog('close');
});
}
});
});
ajax 部分是:
$html = '';
$allocate_query = tep_db_query("select * from ". TABLE_DRIVERS ." where distributor_id='" . (int)$distributor_id . "'");
$html .= '<form id="save_designation">
<table>
<tr>';
while($allocate = tep_db_fetch_array($allocate_query)){
$html .= '<ul id="selectable">
<li class="ui-state-default"><input type="radio" name="select_driver" value="'.$allocate['driver_id'].'"> <img src="./images/arrow_green.gif" alt="" border="0" height="12" width="15"> '.$allocate['driver_firstname'].' '.$allocate['driver_surname'].'<br></li>
</ul>';
}
'</tr>
</table>';
$html .= '<br>
<table>
<tr>
<td align="left"><button id="CancelAllocation" type="button">Cancel</button></td>
<td align="right"><button id="saveAllocation" type="button" >Save</button></td>
</tr>
</table>
</form>';
echo $html;
当我单击saveAllocation
按钮时,我需要或希望系统做的是将单选按钮的值发送到名为 TABLE_ORDERS
的数据库表。
我该怎么做?
如果你拿起你的保存按钮
<button id="saveAllocation" type="button" >Save</button>
并为其创建一个 Click 事件,您可以在其中放置 ajax 请求。
var saveRadio;
$('#saveAllocation').on("click", function() {
// Get the selected radio value
saveRadio = $('input[name="select_driver"]').val();
$.post("/yourpage.php", {"select_driver":saveRadio}).done(function() {
// Successful request
}).fail(function() {
// Failed request
}).always(function() {
});
});
使用done()
fail()
和always()
函数,您可以设置要发生的情况。
创建一个名为 yourpage.php
的页面或更改上述 jQuery 中的路径。
您将能够通过$_POST['select_driver']
获取价值
你的 SQL 应该看起来像这样
INSERT INTO table_name(my_column)AS('my_value')
使用 PHP 运行 SQL 时。请注意SQL_injection以及如何预防它。 tep_db_query
似乎是您的 PHP 应用程序编写的函数。要插入用户给您的值,请确保您使用的是PDO
或至少mysqli_
- 否则您的网站可能容易受到攻击
通知
这远非困难。我们在这里解决问题,而不是为您编写代码。虽然我很乐意提供帮助,但您应该尽力完成此操作,而无需有人实际为您完成。
让我们分解一下您需要执行的操作。
- 了解如何创建点击时 jQuery 事件
- 了解如何获取所选无线电输入的值
- 了解如何使用 JavaScript 将数据发布到另一个页面 (ajax)
- 了解如何对 MySQL 执行插入查询
您可以在Stack Overflow上找到前两个。第一个和最后一个你也可以在jQuery文档中找到。
成为一名优秀的程序员就是要解决问题并发挥你的主动性。