Skip to content
Advertisement

What could be wrong with this request in AJAX and PHP?

I’m trying to call a PHP page passing parameters with AJAX. I can see that the page is called, but the $_REQUEST doesn’t take any parameters. If anyone can help me, I’d appreciate it. Thanks!

My AJAX is that:

<script>

    $(document).ready(function(){ 
        $('.c_x').change(function(){
            var href = $(this).val();
            $.ajax({
                type: "POST",
                url: "utils/inc_iscrizione.php",
                data: 'cod_evento=' + href,
                success: function (data) {
                    alert("data:" + data);
                }
            });
        }) 
    });
    
</script>

In the PHP Page, I have this.

if(isset($_REQUEST["cod_evento"]))
{
    $search = $_REQUEST["cod_evento"];
    echo "<script>alert('OK');</script>";
}
else
{
    $search = "";
    echo "<script>alert('FAIL');</script>";
}

Always the answer is failure. Thank you!

Advertisement

Answer

1- change your data format in ajax request from data:’cod_evento=’ + href, to data : {code_evento:href}

2 – change your php echo from echo “alert(‘OK’);” to echo “OK”; and same for else clause

Recommended 1 – change $_REQUEST to $_POST 2 – change alert(‘data:’+ data) to console.log(data)

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement