[英]Passing variable from Javascript to PHP and back
The console ends up writing back 控制台最终回写
Uncaught ReferenceError: provhtml is not defined
未捕获的ReferenceError:未定义provhtml
Also, when I change up my Javascript to be the code right below, the console ends up just writing " <?= $provhtml ?>
" 另外,当我将Javascript更改为下面的代码时,控制台最终只会写“
<?= $provhtml ?>
”
var provnum = "<?= $provhtml ?>";
console.log(provnum);
I'm not exactly sure what the problem is. 我不确定是什么问题。 I want $provhtml to be written inside the console when a user clicks a button named otherlistbut (I'm also not sure if there needs to be the
d.preventDefault();
Here is my prov.php 我希望当用户单击名为otherlist的按钮时在控制台内编写$ provhtml,但是(我也不确定是否需要
d.preventDefault();
这是我的prov.php
<?php
$phonenum = $_POST["phonenum"];
$provhtml = file_get_contents('http://api.data24-7.com/v/2.0?user=USERNAME&pass=PASSWORD&api=C&p1=1' . $phonenum);
?>
Here are my scripts in my index.html 这是我index.html中的脚本
$('button[name="otherlistbut"]').click(function(d) {
lookupProvider(d);
});
});
/************ FUNCTIONS ******************/
function lookupProvider(d) {
d.preventDefault();
var phonenum = $('input[name="phonenum"]').val();
$.ajax({
type: 'POST',
data: {
phonenum: phonenum
},
url: 'prov.php',
success: function(data) {
var provnum = $(provhtml);
console.log(provnum);
},
error: function(xhr, err) {
console.log("readyState: " + xhr.readyState + "\nstatus: " + xhr.status);
console.log("responseText: " + xhr.responseText);
}
});
}
In your prov.php file 在您的prov.php文件中
<?php
$phonenum = $_POST["phonenum"];
$provhtml = file_get_contents('http://api.data24-7.com/v/2.0?user=USERNAME&pass=PASSWORD&api=C&p1=1' . $phonenum);
echo json_encode( array( "phonenum" => $phonenum,"provhtml"=>$provhtml)); //You should be returning the values to your ajax function
?>
In your index.html 在您的index.html中
function lookupProvider(d) {
d.preventDefault();
var phonenum = $('input[name="phonenum"]').val();
$.ajax({
type: 'POST',
data: {
phonenum: phonenum
},
url: 'prov.php',
success: function(data) { //Receives the data from the php code
var provnum = data.phonenum;
console.log(provnum);
},
error: function(xhr, err) {
console.log("readyState: " + xhr.readyState + "\nstatus: " + xhr.status);
console.log("responseText: " + xhr.responseText);
}
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.