2016-12-15 232 views
-1

我想將Javascript變量傳遞給PHP,但它從未設置。我有一個PHP文件的一切將Javascript變量傳遞給PHP

CODE:

<!DOCTYPE html> 
<html> 
<head> 
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> 
    <link rel="stylesheet" type="text/css" href="asd.css"> 
    <title>Tabulka</title> 
</head> 
<body> 

<script> 
$(document).ready(function(){ 
    $("#table tr").click(function(){ 
     $(this).addClass('selected').siblings().removeClass('selected'); 

     var value = $("#table tr.selected td:first").html(); 

     var xmlhttp = new XMLHttpRequest(); 
     xmlhttp.open("GET", "tabulka.php?id=" + value, true); 
     xmlhttp.send(); 
    }); 
}); 
</script> 
<table id="table"> 
    <tr> 
     <th>A</th> <th>A</th> <th>B</th> <th>C</th> <th>D</th> 
    </tr> 

    <tr> 
     <td>51</td> <td>41</td> <td>1515</td> <td>419</td> <td>asd</td> 
    </tr> 

    <tr> 
     <td>52</td> <td>41</td> <td>1515</td> <td>419</td> <td>asd</td> 
    </tr> 

    <tr> 
     <td>63</td> <td>41</td> <td>1515</td> <td>419</td> <td>asd</td> 
    </tr> 
</table> 

<form method="POST"> 
    <input type="submit" name="ok-submit" class="ok" value="OKOK"> 
</form> 
</body> 
</html> 


<?php 
if(isset($_REQUEST['id'])){ 
    echo $_REQUEST['id']; 
} 
?> 

我使用XMLHttpRequest來傳遞變量。它的工作原理類似於如果我點擊表格行。首先td傳遞給php變量並打印出來打印出來。但是他$ _REQUEST ['id']從未設置。

+0

不,這不是它是如何工作的。 – Phiter

+0

將返回ajax響應的文件必須是發出請求的文件的單獨文件,或者您可以使用條件,但它會很難看。 – Phiter

+3

可能的重複[如何將JavaScript變量傳遞給PHP?](http://stackoverflow.com/questions/1917576/how-to-pass-javascript-variables-to-php) –

回答

0

,能夠通過JavaScript來傳遞數據到另一頁被處理由PHP服務器端,建立一個結果,然後將當前的頁面,JavaScript的駐留上顯示它。它非常漂亮,不需要刷新頁面或提交表單。

我下面的例子是一個簡單的查找函數。用戶輸入查詢字符串並點擊搜索。文本框的內容被傳遞給一個javascript函數,它傳遞給運行數據庫查詢(或者做任何你想做的事情)的PHP頁面,構建一些HTML,然後在用戶所在的當前頁面上輸出該HTML。在這種情況下,將這個div內傾倒它的innerHTML:

<div id='studentResults'></div> 

下面是HTML:

<div id='quickStudentLookup'> 
     <div> 
      <p>Quick Search</p> 
     </div> 

     <div> 
      <input id='queryString' name='queryString' type='text' placeholder='First OR Last Name' /> 
      <input type='submit' value='Search' onclick="javascript:ajax_post();" /> 

     <div id='studentResults'></div> 
     </div> 
    </div> 

的Javascript:

function ajax_post(){ 
     // Create our XMLHttpRequest object 
     var hr = new XMLHttpRequest(); 

     var url = "studentSearch.php"; // this is a file that would take the queryString, perform something on it and echo some HTML back. The HTML will end up as innerHTML of the div id studentResults 

     // Create some variables we need to send to our PHP file 
     var queryString = document.getElementById("queryString").value; 
     var vars = "queryString="+queryString; 
     hr.open("POST", url, true); 

     // Set content type header information for sending url encoded variables in the request 
     hr.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); 
     // Access the onreadystatechange event for the XMLHttpRequest object 
     hr.onreadystatechange = function() { 

      if(hr.readyState == 4 && hr.status == 200) { 
       var return_data = hr.responseText; 
       document.getElementById("studentResults").innerHTML = return_data; 
      } 
      else{ 

      } 
     } 
     // Send the data to PHP now... and wait for response to update the status div 
     hr.send(vars); // Actually execute the request 
} 

PHP: 這可以是任何你想要的。下面是一個簡單的例子,它接收數據,查詢數據庫和迴應結果。回聲將在ID爲studentResults的DIV內發生。這是在上面的JS中定義的。

<?php 
     // database connection omitted 
     $queryString = $_POST['queryString']; // get var from post 

     $studentSearch = "SELECT 
     student.student_id, 
     student.firstName, 
     student.lastName, 
     student.studentID 
     FROM 
     student 
     WHERE student.lastName = :queryString 
     LIMIT 10"; // build query 

     $sth = $handler->prepare($studentSearch); // pdo to run query 
     $sth->execute(array(':queryString'=>$queryString)); 
     $results = $sth->fetchAll();     

     foreach($results as $r){ // loop through results and output 
      $firstName  = $r['firstName']; 
      $lastName   = $r['lastName']; 
      $studentID  = $r['studentID']; 

      echo "<p>$firstName $lastName - $studentID</p>"; 

     } 

?> 

希望它有幫助!

0

您無法將當前頁面的Javascript變量值傳遞給當前頁面PHP代碼... PHP代碼在服務器上運行,並且它不知道客戶端正在發生什麼。

您需要使用其他機制將變量傳遞給PHP代碼,例如使用GET或POST方法提交表單。

<DOCTYPE html> 
<html> 
    <head> 
    <title>My Test Form</title> 
    </head> 

    <body> 
    <form method="POST"> 
     <p>Please, choose the salary id to proceed result:</p> 
     <p> 
     <label for="salarieids">SalarieID:</label> 
     <?php 
      $query = "SELECT * FROM salarie"; 
      $result = mysql_query($query); 
      if ($result) : 
     ?> 
     <select id="salarieids" name="salarieid"> 
      <?php 
      while ($row = mysql_fetch_assoc($result)) { 
       echo '<option value="', $row['salaried'], '">', $row['salaried'], '</option>'; //between <option></option> tags you can output something more human-friendly (like $row['name'], if table "salaried" have one) 
      } 
      ?> 
     </select> 
     <?php endif ?> 
     </p> 
     <p> 
     <input type="submit" value="Sumbit my choice"/> 
     </p> 
    </form> 

    <?php if isset($_POST['salaried']) : ?> 
     <?php 
     $query = "SELECT * FROM salarie WHERE salarieid = " . $_POST['salarieid']; 
     $result = mysql_query($query); 
     if ($result) : 
     ?> 
     <table> 
      <?php 
      while ($row = mysql_fetch_assoc($result)) { 
       echo '<tr>'; 
       echo '<td>', $row['salaried'], '</td><td>', $row['bla-bla-bla'], '</td>' ...; // and others 
       echo '</tr>'; 
      } 
      ?> 
     </table> 
     <?php endif?> 
    <?php endif ?> 
    </body> 
</html> 
+0

謝謝,這將有所幫助。 –