2014-02-24 103 views
0

我在這裏有一個ajax。我需要知道的是,如果有可能發回郵政價值並將其存儲在主頁中的php變量中,這取決於onchange事件? $ _POST [「mainlist_id」]存儲在PHP VAR?Ajax後值和存儲在PHP變量

getajax.php

<?php 
if (isset($_POST["mainlist_id"])) { 
    $mysqli = new mysqli("localhost", "root", "", "2015"); 
    $main = $mysqli->real_escape_string($_POST["mainlist_id"]); 


$result1 = $mysqli->query("SELECT * FROM code WHERE cat_code='$main' GROUP BY item_code ORDER BY item"); 

    $option1 = ''; 
    while($row = $result1->fetch_assoc()) 
     { 
     $option1 .= '<option value = "'.$row['item'].'">'.$row['item'].'</option>'; 
     } 
     echo $option1; 
    } 
?> 

網主頁

<script type="text/javascript"> 
    $('#main').change(function() { 
     $.ajax({ 
      url: 'getajax.php', 
      data: { 
       mainlist_id: $(this).val() 
      }, 
      dataType: 'html', 
      type: 'POST', 
      success: function (data) { 
       $('#languages').html(data); 
      } 
     }); 
    }); 
</script> 
+0

爲什麼你想要將你的POST數據再次發送回'Mainpage'?因爲,您已在'Mainpage'中擁有該數據。 – mi6crazyheart

+0

您可以在getajax.php頁面中使用'$ _SESSION'來存儲POST變量並在主頁面中訪問它。 –

回答

0

getajax.php

<?php 
session_start(); 
if (isset($_POST["mainlist_id"])) { 
    $mysqli = new mysqli("localhost", "root", "", "2015"); 
    $main = $mysqli->real_escape_string($_POST["mainlist_id"]); 
    $_SESSION['mainlist_id']=$main; 

$result1 = $mysqli->query("SELECT * FROM code WHERE cat_code='$main' GROUP BY item_code ORDER BY item"); 

    $option1 = ''; 
    while($row = $result1->fetch_assoc()) 
     { 
     $option1 .= '<option value = "'.$row['item'].'">'.$row['item'].'</option>'; 
     } 
     echo $option1; 
    } 
?> 

網主頁

<?php session_start(); 
    $main_id=$_SESSION['mainlist_id']; 
    ?> 
    <script type="text/javascript"> 
     $('#main').change(function() { 
      $.ajax({ 
       url: 'getajax.php', 
       data: { 
        mainlist_id: $(this).val() 
       }, 
       dataType: 'html', 
       type: 'POST', 
       success: function (data) { 
        $('#languages').html(data); 
       } 
      }); 
     }); 
    </script>