Javascript 通过ajax加载的脚本don';无法正确地将post数据发送给自己?
因此,结构是: index.php loads/index.php#ajax/landing.php php有一个表单,表单数据作为post请求发送(篡改数据显示发送的post数据),但是Javascript 通过ajax加载的脚本don';无法正确地将post数据发送给自己?,javascript,php,ajax,Javascript,Php,Ajax,因此,结构是: index.php loads/index.php#ajax/landing.php php有一个表单,表单数据作为post请求发送(篡改数据显示发送的post数据),但是var\u dump($\u post)显示为空。我猜是因为脚本的加载方式,post数据必须发送到index.php,并且不能被landing.php访问 这有什么办法吗 我考虑过使用ajax发送post数据并在div中显示结果的可能性,但我对它不是很在行,也不太了解我在做什么 因此,在我的场景中,这里是我试图
var\u dump($\u post)显示为空。我猜是因为脚本的加载方式,post数据必须发送到index.php,并且不能被landing.php访问
这有什么办法吗
我考虑过使用ajax发送post数据并在div中显示结果的可能性,但我对它不是很在行,也不太了解我在做什么
因此,在我的场景中,这里是我试图做的:
<form name="search_form" id="search_form" method="POST">
<input type="text" name="search" id="search" />
<input type="submit" name="submit_search_form" id="submit_search_form" value="search" />
</form>
<div id="search_results">
</div>
如何让它向/php/search.php
发送POST请求,然后将脚本处理POST数据的结果放入search results div
如有任何想法/帮助,将不胜感激。
注意:我请求POST,因为页面可以访问GET/query字符串,但不适合其他任务,如更改密码,因此尽管在我提到的场景中,作为GET请求也可以,但我需要找到一种方法来让POST正常工作。我使用类似的方法(需要jQuery):
这只是将数据发布到表单中指定的url 我看到了成功,但我对这类编程不太熟悉。console.log(数据)
将做什么?我看到了一些东西,允许您将发送数据的脚本的输出按id插入现有的div?console.log将其拥有的数据输出到浏览器控制台(类似于错误日志)。我把它改成把数据发送给你的分区谢谢,我假设我有使用你代码的权限?发帖请求会和传统的一样吗?对于search.php接收请求的内容,是否可以使用传统的$\u POST['search']
访问变量?对于url,我是放置完整的url/文件路径还是仅放置文件路径?并且还替换了href或add之后的,始终建议使用完整的url(在整个代码库中),但也可以使用类似“submit.php”(或任何文件名)的路径。
$(document).ready(function() { // When the document is completely loaded this function runs
$('form').unbind('submit').bind('submit', function(event) { // catch the pressing of enter and catch submit action. Makes sure the form does not get posted the normal way
if (event.keyCode == 13) { // if enter is pressed
return false;
}
pageRetrieve($(this).attr('action')+"?"+$(this).serialize(), 'POST'); // send this data to the function that sends the post
event.preventDefault();
return false;
});
});
function pageRetrieve(href, sType) { // this function send the data to the server
sType = (typeof sType === "undefined") ? "GET" : sType; // if 'POST' is not set in the previous function, set it to GET
$.ajax({ // function to send the data to the server
url: href, // the url to send it to
type: sType, // the type (post/get)
success: function (data) { // when the server responds successful
$('#search_results').html(data); // put the data in your div
},
error: function () { // if call to server is not correct (eg wrong url)
alert("error");
}
});
}