每个人。我是一个相对较新的开发人员,在jQueryAJAX和PHP方面的经验有限。我正在开发一个Wordpress插件,这对我来说也是一个很好的学习练习。
基本描述:在我插件的管理页面上,我有一堆表单,它们将显示为模式窗口(使用jQuery UI),填写后,将把它们的字段提交到一个单独的PHP文件中进行处理。该文件将接受数据,并准备将其插入到我设置的wpdb表中。
插件管理页面(PHP):
<div id="form1" title="My Awesome Form">
<form id="frmNewCom">
<fieldset>
<table>
<tr>
<td><label for="name">Community Name</label>
<td><input type="text" name="newComName" id="newComName" />
</tr>
<tr>
<td><label for="lefthead">Left Column Header</label></td>
<td><input type="text" name="newComLefthead" id="newComLefthead" /></td>
</tr>
<tr>
<td><label for="righthead">Right Column Header</label></td>
<td><input type="text" name="newComRighthead" id="newComRighthead" /></td>
</tr>
</table>
</fieldset>
</form>
</div>
表单的jQuery UI代码($pcal
是我没有冲突的东西):
$pcal('#form1').dialog({
autoOpen: false,
height: 275,
width: 400,
modal: true,
buttons: {
"Add Living Option": function() {
// Functionality for submit
},
Cancel: function() {
$pcal(this).dialog("close");
}
},
close: function() {
// close function
}
});
现在问题来了。我真的不知道从这一点开始该怎么办。我读过很多关于使用.post()
和.serialize()
的文章,在这一点上我感到非常困惑。
你们对javascript/jQuery到PHP的正确处理有什么见解吗?我在Wordpress论坛上也问过这个问题,但我总是在这里找到好的建议,所以我想问一下。感谢您的任何帮助。
一个非常简单的例子是:
$pcal.post("test.php", $pcal("#frmNewCom").serialize(), function() {
// this will run once returned from PHP
alert('thanks');
$pcal(this).dialog("close");
});
这将使用.post()和.serialize()以序列化格式(newComName=1&newComLefthead=2&newComName=3
,其中1,2和3是在表单中输入的值)发布表单(id
=frmNewCom
)
然后在test.php
中,您可以访问这样的值:
$newComName = $_POST['newComName'];
$newComLefthead = $_POST['newComLefthead'];
$newComName = $_POST['newComName'];
// insert into table
如果您想将SQL注入存储在DB 中,则上述内容a)未经测试,b)不包含任何针对SQL注入的预防措施