将表单数据的JQuery.load()或.ajax()或.post()转换为php函数
尝试使用JQuery的一个函数实现AJAX。我一直在以各种方式尝试.load()、.ajax()或.post(),使用了一些有关堆栈溢出的示例,但没有成功 我有一个表单,它查询oracle数据库,并返回另一个表单和一个结果表。我用传统的方式处理单独的PHP文件(重新加载整个新页面)。现在我只想加载内容而不刷新页面 开始表单PHP(checkin_Start.PHP):输入条形码并提交将表单数据的JQuery.load()或.ajax()或.post()转换为php函数,php,jquery,ajax,.post,Php,Jquery,Ajax,.post,尝试使用JQuery的一个函数实现AJAX。我一直在以各种方式尝试.load()、.ajax()或.post(),使用了一些有关堆栈溢出的示例,但没有成功 我有一个表单,它查询oracle数据库,并返回另一个表单和一个结果表。我用传统的方式处理单独的PHP文件(重新加载整个新页面)。现在我只想加载内容而不刷新页面 开始表单PHP(checkin_Start.PHP):输入条形码并提交 <div class="content" id="bodyContent"> <form
<div class="content" id="bodyContent">
<form id="usualValidate" class="mainForm" method="post" action="">
<label>Barcode:<span>*</span></label>
<input type="text" class="required" name="startBarcode" id="startBarcode"/>
<input type="submit" value="submit" class="blueBtn" id="startBtn" />
</form>
</div>
感谢您提供的任何见解……当您使用$.post时,不应引用值,除非它们是文字 试试这个:
$(document).ready(function() {
$("#usualValidate").submit(function(sevt) {
sevt.preventDefault();
var startBC = $("#startBarcode").val();
$.post("checkin_process.php",
{startBarcode : startBC},
function(data) {
$("#bodyContent").html(data);
// log the returned data to console for debug
console.log(data);
});
});
});
使用$.post时,除非是文字,否则不应引用这些值 试试这个:
$(document).ready(function() {
$("#usualValidate").submit(function(sevt) {
sevt.preventDefault();
var startBC = $("#startBarcode").val();
$.post("checkin_process.php",
{startBarcode : startBC},
function(data) {
$("#bodyContent").html(data);
// log the returned data to console for debug
console.log(data);
});
});
});
在javascript文件中,可以使用
post
方法,也可以使用ajax
发送数据,如下例所示:
$.ajax({
type : 'post',
url : 'currentphpfile.php',
//Here is where the data is put
data : {
"ajax" : "1",
"otherData" : "abc"
},
success : function(data, textStatus, jqXHR) {
//Do something with the data here like insert it into the document
},
error : function(XMLHttpRequest, textStatus, errorThrown) {
//Do something to fix the error
},
dataType : 'text' //Or automatically parse a json response with 'json'
});
要使其正常工作,您需要在php文件中包含能够处理请求的内容,如下所示:
if (!empty($_POST['ajax']) && $_POST['ajax']==1){
//Do stuff with the other post data
//Here's how to return the data to your script:
//If you chose a text response above:
echo $importantData;
exit;
//If you chose json
echo json_encode(array("title" => $title, "text" => $text /*, etc... */));
}
我还没有测试过这段代码的bug,但您可能已经知道了。在javascript文件中,您可以使用
post
方法,也可以使用ajax
发送数据,如下例所示:
$.ajax({
type : 'post',
url : 'currentphpfile.php',
//Here is where the data is put
data : {
"ajax" : "1",
"otherData" : "abc"
},
success : function(data, textStatus, jqXHR) {
//Do something with the data here like insert it into the document
},
error : function(XMLHttpRequest, textStatus, errorThrown) {
//Do something to fix the error
},
dataType : 'text' //Or automatically parse a json response with 'json'
});
要使其正常工作,您需要在php文件中包含能够处理请求的内容,如下所示:
if (!empty($_POST['ajax']) && $_POST['ajax']==1){
//Do stuff with the other post data
//Here's how to return the data to your script:
//If you chose a text response above:
echo $importantData;
exit;
//If you chose json
echo json_encode(array("title" => $title, "text" => $text /*, etc... */));
}
我还没有测试过这段代码的bug,但您可能已经知道了。嗯,值不应该是-我不认为这是键的问题?如果值是变量,就不应该引用这些值。在本例中,startBC的值是一个变量。@user2023235。谢谢,这有助于传递变量,我现在可以在firebug中看到这一点。现在它指出了我的下一个问题,checkin_process.php只是内容,我没有提到function.php。将require once functions.php添加到checkin_process.php会导致oci_解析错误。有人知道如何在没有标题的情况下引用它吗?我想我知道问题所在。我将以新问题的形式重新发布。我已经发布了一个与之相关的问题。你能检查一下吗?嗯,值不应该是-我不认为这是键的问题?如果值是变量,则不应该引用这些值。在本例中,startBC的值是一个变量。@user2023235。谢谢,这有助于传递变量,我现在可以在firebug中看到这一点。现在它指出了我的下一个问题,checkin_process.php只是内容,我没有提到function.php。将require once functions.php添加到checkin_process.php会导致oci_解析错误。有人知道如何在没有标题的情况下引用它吗?我想我知道问题所在。我将以新问题的形式重新发布。我已经发布了一个与之相关的问题,您可以检查吗?在您的示例中,删除“startBC”周围的引号:
“startBarcode”:startBC
。您当前正在传递字符串“startBC”,而不是变量的值。在您的示例中,请删除“startBC”周围的引号:“startBarcode”:startBC
。您当前传递的是字符串“startBC”,而不是变量的值。谢谢。阅读json,看看这是否是一个更好的选择。谢谢。阅读json,看看这是否是一个更好的选择。