将Javascript变量传递到PHP文件

将Javascript变量传递到PHP文件,javascript,php,jquery,ajax,Javascript,Php,Jquery,Ajax,我想知道你是否能帮忙。我试图将一个变量传递给PHP文件,然后使用该变量运行SQL查询,然后将结果作为变量传递回javascript。目前,我已经使用Ajax成功地将PHP接收回javascript,但无法将ServiceName发送到PHP文件。文件必须分开存放。另外,为了澄清,我已经为隐私替换了某些部分,但是,它们是正确的,并且在代码中起作用。我已经使用了$_GET方法,但是,我只能让javascript访问一个新窗口,而不能返回PHP变量 我目前的代码如下: // If the user c

我想知道你是否能帮忙。我试图将一个变量传递给PHP文件,然后使用该变量运行SQL查询,然后将结果作为变量传递回javascript。目前,我已经使用Ajax成功地将PHP接收回javascript,但无法将ServiceName发送到PHP文件。文件必须分开存放。另外,为了澄清,我已经为隐私替换了某些部分,但是,它们是正确的,并且在代码中起作用。我已经使用了$_GET方法,但是,我只能让javascript访问一个新窗口,而不能返回PHP变量

我目前的代码如下:

// If the user changes the ServiceID field.
if (sender.getFieldName() == 'ServiceID')
  // Declare the value of the new Service name and save it in the variable A.
  a = sender.getValue();

{     
  // if it equals a new variable.
  if (sender.getValue() == a) {
    // Place it within the URL, in order for it to be processed in the php        code.
    window.location.href = "http://IP/development/Query01.php?service=" + a;

    // Attempted code
    // echo jason_encode(a);
    //    $.ajax({var service = a;
    //  $.post('http://IP/development/Query01.php', {variable: service});
    //  }

    //use AJAX to retrieve the results, this does work if the service name        is hard coded into the PHP.   
    $.ajax({
      url: "http://IP/development/Query01.php",
      dataType: "json", //the return type data is jsonn
      success: function(data) { // <--- (data) is in json format
        editors['Contact2'].setValue(data);
        //alert(data);
        //parse the json data
      }
    });
  }
}
}
//如果用户更改ServiceID字段。
if(sender.getFieldName()==“ServiceID”)
//声明新服务名称的值并将其保存在变量A中。
a=发送方.getValue();
{     
//如果它等于一个新变量。
if(sender.getValue()==a){
//将其放在URL中,以便在php代码中进行处理。
window.location.href=”http://IP/development/Query01.php?service=“+a;
//尝试代码
//回波jason_编码(a);
//$.ajax({var service=a;
//$.post($)http://IP/development/Query01.php“,{variable:service});
//  }
//使用AJAX检索结果,如果服务名称硬编码到PHP中,这是可行的。
$.ajax({
url:“http://IP/development/Query01.php",
数据类型:“json”//返回类型数据为jsonn

成功:函数(数据){/您可以像这样使用Ajax请求发送数据

$.ajax({
    url: "http://IP/development/Query01.php",
    method: "POST" // send as POST, you could also use GET or PUT,
    data: { name: "John", location: "Boston" }
    dataType: "json",
    success: function(data) {
        editors['Contact2'].setValue(data);
    }
});
然后在PHP中访问发送的数据:

<?php
print_r($_POST);

/*
[
    "name" => "John",
    "location" => "Boston"
]
*/
?>

不能在同一页面上将javascript变量传递给php


您可以使用POST或GET方法通过ajax调用来实现这一点,然后您可以将处理后的数据发送回浏览器,并将其存储在javascript的对象或变量中。

您可以通过单个ajax调用来实现

从代码中删除此行:

window.location.href = "http://IP/development/Query01.php?service=" + a;
并稍微修改一下Ajax调用

$.ajax({
      type: 'GET'
      data : {
            service: sender.getValue();
      },
      url: "http://IP/development/Query01.php",
      dataType: "json", //the return type data is jsonn
      success: function(data){ // <--- (data) is in json format
            editors['Contact2'].setValue(data);
            //alert(data);
            //parse the json data
      }
});
$.ajax({
键入:“获取”
数据:{
服务:sender.getValue();
},
url:“http://IP/development/Query01.php",
数据类型:“json”//返回类型数据为jsonn

成功:函数(数据){/谢谢你们的帮助。我只是觉得这会很有用,如果我最终发布了我的结果,不管它是否正确,它肯定会完成任务

// If the user changes the ServiceID field.
if (sender.getFieldName() == 'ServiceID')

{      
    // Variable Declaration
    serviceName = sender.getValue();

    {
      // Use JQuery.Ajax to send a variable to the php file, and return the   result.
      $.ajax({
      // Access the PHP file and save the serviceName variable in the URL, to allow the $_GET..
      // method to access the javascript variable to apply it within the SQL  Query.
         url: "http://ServerName/development/DefaultContact1.php?service=" +  serviceName,
         // retrieve the result, using json.
         dataType: "json", // the return type data is jsonn
         success: function(data)// <--- (data) is in json format
                  {
                  // pre-fill the contact1 field with the result of the PHP file.
                  editors['Contact1'].setValue(data);
                  }
             // End of Ajax Query    
             });
     // End of function to prefill Contact1 field.
//如果用户更改ServiceID字段。
if(sender.getFieldName()==“ServiceID”)
{      
//变量声明
serviceName=sender.getValue();
{
//使用JQuery.Ajax将变量发送到php文件,并返回结果。
$.ajax({
//访问PHP文件并将serviceName变量保存在URL中,以允许$\u GET。。
//方法访问javascript变量以在SQL查询中应用它。
url:“http://ServerName/development/DefaultContact1.php?service=“+serviceName,
//使用json检索结果。
数据类型:“json”//返回类型数据为jsonn

success:function(data)//关于向PHP发送JavaScript变量有很多问答。请参阅示例:还要注意,最初执行GET请求时尝试
$。post
无效警告您的PHP代码易受SQL注入的影响。不要将变量与SQL连接起来,而是使用参数化查询:
sqlsrv\u query(“从tblServices中选择DefaultContact2,其中ServiceName=?”,array($service));
谢谢你们的帮助。@Jcaron在我上线之前,我会确保我实现了参数,以防止SQL注入。我想他是在要求vise-versa。哦,是的。重点是:-D