2017-02-10 28 views
0

我怎麼能加載文件table.php與我嘗試通過的數據,當ajax調用made.I'm與jquery.load方法trysing第二個參數是一個數據數組,但不起作用。傳遞數據到PHP文件與HTML模板

這是一個基本的例子,我必須讓我得到這個錯誤

Notice: Undefined variable: field in C:\xampp\htdocs\prueba2\table.php on line 2 

的index.php

<?php 
    $datos = array(array('nombre1','apellidos1'),array('nombre2','apellidos2')); 
?> 
<!DOCTYPE html> 
<html> 
<head> 
    <title></title> 
</head> 
<body> 
     <h2>Tabla</h2> 
     <div id="tabla"> 
      <table border="1px"> 
      <?php 
       foreach ($datos as $dato) { 
        echo "<tr><td>".$dato[0]."</td><td>".$dato[1]."</td></tr>"; 
       } 
      ?> 
      </table> 
     </div> 

     <form id="search-form" method="POST"> 
      <input type="text" id="search" name='search'> 
      <input type="submit" value="Buscar"> 
     </form> 
     <script 
       src="https://code.jquery.com/jquery-1.12.4.min.js" 
       integrity="sha256-ZosEbRLbNQzLpnKIkEdrPv7lOy9C27hHQ+Xp8a4MxAQ=" 
       crossorigin="anonymous"></script> 
     <script type="text/javascript" src="submit.js" ></script> 
</body> 
</html> 

submit.js

$("#search-form").submit(function(event) { 
    event.preventDefault(); 
    $.ajax({ 
     url: "http://localhost/prueba2", 
     data:{ action:"selection" } 

    }) 
    .done(function(data) { 
     $data = { field: $("#search").val()}; 
     $("#tabla").load("table.php", $data); 
    }); 

}); 

table.php

<div id="tabla"> 
    <?=$field?> 
    <table border="1px"> 
     <tr> 
      <td>a</td><td>b</td> 
     </tr> 
</table> 

回答

0

變量$field不存在於table.php。您需要使用$_POST['field']$_GET['field']

+0

工程witn $ _POST ['字段'] ...但我認爲,得到是在jQuery上調用的默認方法jQuery – AFS

+0

GET是默認的'load()',除非你調用它與對象 - 對象獲取發送作爲POST。從[documentation](http://api.jquery.com/load/#request-method):如果數據作爲對象提供,則使用POST方法;否則,假定爲GET。 –