将JavaScript变量发送到PHP,在SQL语句中使用该变量,然后显示PHP结果
我试图将一个JavaScript变量发送到PHP脚本,在SQL语句中使用该变量,然后将结果发送回JavaScript JavaScript:将JavaScript变量发送到PHP,在SQL语句中使用该变量,然后显示PHP结果,javascript,php,sql,ajax,Javascript,Php,Sql,Ajax,我试图将一个JavaScript变量发送到PHP脚本,在SQL语句中使用该变量,然后将结果发送回JavaScript JavaScript: // Sending a variable to PHP script var variableToSend = '30'; $.post('myPHPscript.php', {variable: variableToSend}); // Using the variable from earlier, run PHP script to get re
// Sending a variable to PHP script
var variableToSend = '30';
$.post('myPHPscript.php', {variable: variableToSend});
// Using the variable from earlier, run PHP script to get results
var data_from_ajax;
$.get('http://www.mywebsite.com/myPHPscript.php', function(data) {
data_from_ajax = data;
});
// Display results on the document
var displayResult = document.getElementById('displayResult');
displayResult.innerHTML = data_from_ajax;
PHP:
JS调用PHP脚本的原因是,我在JS中使用了一个事件侦听器来确定要发送的变量。variableToSend每次都会根据单击的内容而变化,但我在这里将其设置为一些随机数
我知道问题在于在JavaScript中向PHP脚本发送变量,但我不确定除了使用.post和.get之外,我还能做什么
我也考虑过使用AJAX调用,但我不确定这将如何工作
谢谢你的指点 您已经通过
$.post
和$.get
两次使用ajax了
您只需发出post请求,并显示响应:
// Sending a variable to PHP script via ajax post, and display the returned results
var variableToSend = '30';
$.post('http://www.mywebsite.com/myPHPscript.php', {variable: variableToSend}, function(responseFromPhp){
$('#displayResult').html(responsefromPhp);
});
您已经通过
$.post
和$.get
两次使用ajax
您只需发出post请求,并显示响应:
// Sending a variable to PHP script via ajax post, and display the returned results
var variableToSend = '30';
$.post('http://www.mywebsite.com/myPHPscript.php', {variable: variableToSend}, function(responseFromPhp){
$('#displayResult').html(responsefromPhp);
});
您需要的数据将在这个post ajax调用中返回。要检索它,请指定success函数
$.post( "test.php", { name: "John", time: "2pm" })
.done(function( data ) {
alert( "Data Loaded: " + data );
});
在mysql中使用post值之前,还要使用一些验证和转义:
您想要的数据将在这个post ajax调用中返回。要检索它,请指定success函数
$.post( "test.php", { name: "John", time: "2pm" })
.done(function( data ) {
alert( "Data Loaded: " + data );
});
在mysql中使用post值之前,还要使用一些验证和转义:
您已经在使用2个ajax调用。如果您已经在使用2个ajax调用,则应该只进行一次调用。你应该只做一个我明白你现在使用ajax两次的意思。非常感谢。我理解你现在两次使用ajax的意思。非常感谢。