Ajax post值和php变量存储

时间:2014-02-24 03:20:56

标签: php jquery ajax

我这里有一个ajax。我需要知道是否可以发回post值并将其存储在主页中的php变量中,具体取决于onchange事件? $ _POST [“mainlist_id”]存储在php var?

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>

1 个答案:

答案 0 :(得分: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>