我知道在SO和网上有一些相关的条目,但是我无法开始工作 – 任何帮助都将不胜感激.
所以我在Javascript中有一个数组,我正在尝试传递给PHP.
function sendToPHP() {
$.post("index.PHP", { "variable": toSearchArray });
}
<?PHP
$myval = $_POST['variable'];
print_r ($myval);
?>
*那里的印刷品供我检查.
任何想法 – 我在使用MAMP所以它的localhost:8888 / index.PHP.这可能导致URL不正确的问题吗?
谢谢.
解决方法:
你对ajax如何运作有误解.虽然jquery让它变得简单,但它仍然不是自动的.你应该找到一个关于jjery的ajax的教程,但如果你只是想发送一个数组到PHP并在屏幕上看到输出,这样的东西就可以了:
的index.PHP
<html>
<head>
<title>Test</title>
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
//attach to the button a click event
$('#btn').click(function(){
//get the value from the textBox
var txt=$('#txt').val();
//if txt is blank, alert an error
if(txt == ''){
alert("Enter some text");
} else {
//send txt to the server
//notice the function at the end. this gets called after the data has been sent
$.post('catcher.PHP', {'text':txt}, function(data){
//Now data is an object, so put the message in the div
$('#response').text(data.message);
}, 'json');
}
});
});
</script>
</head>
<body>
<input type="text" id="txt">
<input type="button" id="btn">
<pre id="response" style="overflow:auto;width:800px;height:600px;margin:0 auto;border:1px solid black;"> </pre>
</body>
</html>
catcher.PHP:
<?PHP
//if something was posted
if(!empty($_POST)){
//start an output var
$output = array();
//do any processing here.
$output['message'] = "Success!";
//send the output back to the client
echo json_encode($output);
}
最好使用2个文件,一个用于加载用户启动ajax调用,另一个用于处理ajax调用.发送数组的工作原理相同,只需用发送数组替换获取文本框值即可.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。