简体   繁体   中英

Pass Value from jQuery AJAX to PHP

Let's say I have this script for example:

<script type="text/javascript">
    function getdata() {
        var referenceNumber = $("#reference_number").val();

        $.ajax({
            url: 'getlistofsomething.php',
            type: 'POST',
            dataType: 'json',
            data: 'referenceNumber=' + referenceNumber,
            success: function (output_string) {
                $("#name").val(output_string[0]);
                $("#address").val(output_string[1]);
            }
        });
    }
</script>

Is there a way I can pass the value in $("#address").val(output_string[1]); into the PHP file this script above is in?

Thanks.

The easiest way is just to send it to the server using GET . Only do this if the data isn't very sensitive...

<script type="text/javascript">
function getdata()
    {
        var referenceNumber = $("#reference_number").val();
        $.ajax({
            url: 'getlistofsomething.php?address=' + $("#address").val(output_string[1]),
            type: 'POST',
            dataType: 'json',
            data: 'referenceNumber='+referenceNumber,
            success: function(output_string)
                {                               
                    $("#name").val(output_string[0]);
                    $("#address").val(output_string[1]);
                }
        });
    }
</script>

This can then be accessed in your php script with:

$address = $_GET['address'];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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