Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/435.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将JavaScript变量发送到PHP,在SQL语句中使用该变量,然后显示PHP结果_Javascript_Php_Sql_Ajax - Fatal编程技术网

将JavaScript变量发送到PHP,在SQL语句中使用该变量,然后显示PHP结果

将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

我试图将一个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 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的意思。非常感谢。