簡體   English   中英

Ajax發布值並存儲在php變量中

[英]Ajax post value and store in php variable

我這里有一個ajax。 我需要知道是否可以發送回值並將其存儲在onchange事件中的主頁的php變量中? $ _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>

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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM