简体   繁体   中英

Ajax not passing by post method to php

Im using google chrome 65.0.3325.181 on windows 10, xampp is on to run the php. title explains the rest.

html/php:

    $(document).ready(function (){
        $('#sel_edificio').load('data.php');

        $( ".form-control" ).change(function() {

            var dato = 50;//document.getElementById("sel_edificio").value;

            $.ajax({
                method: "POST",
                data: {'key': dato},
                url: "uno.php",
                success: function(status){
                    var asd = $('#test').load('uno.php');
                    //document.getElementById("NumEstudiantes").value(key);
                }
            });
        });
    });
</script>

uno.php:

<?php
    echo $_POST['key'];
?>

error:

Notice: Undefined index: key in C:\xampp\htdocs\jqbd\uno.php on line 2

change ajax method to type, try this

$.ajax({
                type: "POST",
                data: {'key': dato},
                dataType: "json",
                url: "uno.php",
                success: function(status){
                    //var asd = $('#test').load('uno.php');
                    $('#test').load('uno.php', { key: dato });//document.getElementById("NumEstudiantes").value(key);
                }
            });

You are sending request two time, try this:

html/php

 $(document).ready(function (){

    $('#sel_edificio').load('data.php');

    $( ".form-control" ).change(function() {

        var dato = 50;//document.getElementById("sel_edificio").value;

        $.ajax({
            method: "POST",
            data: {'key': dato},
            url: "uno.php",
            success: function(data){
                $('#test').html(data);
            }
        });
    });
});

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