2017-04-04 61 views
0

我想知道你是否可以提供帮助。我试图将变量传递给PHP文件,然后使用该变量运行SQL查询,然后将结果作为变量传回给javascript。目前,我已经成功使用Ajax将PHP返回给JavaScript,但无法将ServiceName发送到PHP文件。文件保持分离是非常重要的。也只是为了澄清我已经取代了某些部分的隐私,但是,他们是正确的,并在代码中工作。我已经使用了$ _GET方法,但是,我只能让JavaScript访问一个新窗口而不返回PHP变量。通过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 
     } 
    }); 
    } 
} 
} 
<?php 
    $serverName = "SeverIP"; //serverName\instanceName, portNumber (default is 1433) 
    $connectionInfo = array("Database"=>"DatabaseName", "UID"=>"Username", "PWD"=>"Password 
    $conn = sqlsrv_connect($serverName, $connectionInfo); 
    $service = $_GET['service']; 

    if ($conn) 
    { 
    //echo "Connection established.<br />"; 
    } 
    else 
    { 
    echo "Connection could not be established.<br />"; 
    die(print_r(sqlsrv_errors(), true)); 
    } 

    $sql = ("SELECT DefaultContact2 FROM tblServices WHERE ServiceName = '$service'"); 
    $stmt = sqlsrv_query($conn, $sql); 

    if ($stmt === false) 
    { 
    die(print_r(sqlsrv_errors(), true)); 
    } 
    while ($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) 
    { 
    $dC2 = $row['DefaultContact2']; 
    } 
    echo json_encode ($dC2); 
    sqlsrv_free_stmt($stmt);    
?> 

任何帮助将不胜感激。

+2

关于将JavaScript变量发送到PHP有很多问题。例如,请参阅:http://stackoverflow.com/questions/1917576/how-to-pass-javascript-variables-to-php – trincot

+0

另请注意,当您最初执行GET请求时尝试使用'$ .post'将不起作用 – adeneo

+0

**警告**您的PHP代码易受SQL注入影响。不要连接变量和SQL,而是使用参数化查询:'sqlsrv_query(“SELECT DefaultContact2 FROM tblServices WHERE ServiceName =?”,array($ service));' – jcaron

回答

0

您可以像使用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" 
] 
*/ 
?> 
0

你不可错过的JavaScript的变量到PHP同一页上。

您可以使用POST或GET方法调用ajax来完成此操作,然后您可以将操纵的数据发回给您的浏览器并将其存储在您的javascript对象或变量中。

+0

我认为他是要求反之亦然。 – TheTom

+0

噢。采取的点: - D. – DarkBee

0

您可以在单个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调用的获取。但我不知道你的query01.php是否应该接受在同一个调用中同时执行这两个操作。

0

谢谢你们的帮助。只是认为这将是有益的,如果我最终发布了我的内容,无论它是否是正确的方式,它肯定会完成这项工作。

// 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. 

再次感谢您的回应!