简体   繁体   English

Ajax发布值并存储在php变量中

[英]Ajax post value and store in php variable

I have here a ajax. 我这里有一个ajax。 What I need to know if it possible to send back the post value and store it in php variable in the mainpage depending in onchange event? 我需要知道是否可以发送回值并将其存储在onchange事件中的主页的php变量中? $_POST["mainlist_id"] store in php var? $ _POST [“ mainlist_id”]是否存储在php var中?

getajax.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;
    }
?>

Mainpage 主页

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

getajax.php 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;
    }
?>

Mainpage 主页

<?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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM