我有主文件index.php
。用户执行登录的地方我加载 url.php
在<div>
里面index.php
通过ajax请求。
现在在 url.php 上,在按钮单击事件上,我提交表单并将数据发布到 data.php
文件。如url.php
加载到index.php中,url.php上的按钮单击事件将调用index.php部分中的函数。
我得到两个变量 $x
和$y
作为 data.php 上的结果。
在index.php上我有JS函数,我想在其中使用$x
和$y
。如何找回?
index.php
<script type="text/javascript">
//------------------script 2 starts ---------
function showUser(form, e) {
//alert(myid);
e.preventDefault();
e.returnValue=false;
var xmlhttp;
var sent = form.elements['sent'].value;
//var sent2 = form.elements['sent2'].value;
//alert(sent2);
//var divElements = document.getElementById('d1').innerHTML;
var text1 = document.getElementById('previewUrl').innerText || document.getElementById('previewUrl').textContent;
var text2 = document.getElementById('previewTitle').innerText || document.getElementById('previewTitle').textContent;
var text3 = document.getElementById('previewDescription').innerText || document.getElementById('previewDescription').textContent;
//alert(text3);
console.log(sent);
if (window.XMLHttpRequest) {
xmlhttp=new XMLHttpRequest();
}
else {
// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function(e) {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200){
document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open(form.method, form.action, true);
xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
sent = encodeURIComponent(sent);
text1 = encodeURIComponent(text1);
text2 = encodeURIComponent(text2);
text3 = encodeURIComponent(text3);
xmlhttp.send('sent=' + sent + '&text1=' + text1 + '&text2=' + text2 + '&text3=' + text3);
}
</script>
+
$.ajax({
url:'url.php'
,async: true
,type : 'POST'
,cache: false
,data : 'myid=' + myid
,dataType: 'html'
,success: function(data){
$('body').html(data);
FB.XFBML.parse();
}
}
);
url.php
<form action="data.php" method="POST" onsubmit="showUser(this, event)">
数据.php
if (preg_match_all('/[^=]*=([^;@]*)/', shell_exec("/home/technoworld/Desktop/test/b '$test'"), $matches)){ //Values stored in ma.
echo "hi";
$x = (int) $matches[1][0]; //optionally cast to int
$y = (int) $matches[1][1];
这个x
和y
我想在index.php中获取。
我知道这并非不可能。但对我来说事情很复杂!
最佳答案
我从服务器端发送回数据的首选方法是使用 JSON 响应进行响应。
$data['x'] = $x;
$data['y'] = $y;
header('Content-Type: application/json');
echo json_encode($data);
这应该对您的 ajax 回调做出响应,因此您可以像这样使用它:
$.post('data.php', { "myid": id }, function(data) {
console.log(data.x); //data.x = $x
console.log(data.y); //data.y = $y
});
我没有测试代码来看看它是否真的有效,但它应该告诉我们要做什么。让我知道这是否有意义:)
关于javascript - 从ajax请求调用的php文件中获取变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19595213/