简体   繁体   中英

Trying to call php function with ajax doesn't work

I'm trying to pass some variables to a php file with ajax

 $(document).ready(function(){ $("button").click(function(){ var id = this.id; var name=this.name; console.log(id+" "+name); $.ajax({ type: 'GET', url: 'utility.php', dataType: 'text', data: {id: id, name: name}, success: console.log('aa'), //error: function( jqXhr, textStatus, errorThrown ){ console.log( errorThrown ); }, //complete: alert(id+' '+name) }); // Ajax Call }); //event handler }); //document.ready 
 <?php warning('getting something'); if($_GET['id'] && $_GET['name']){ $id=$_GET['id']; $name=$_GET['name']; if($id=='delete'){ my_remove($name); } if($id=='modify'){ retrieve($name); my_remove($name); modify($name); } } else { warning('unable to get information'); } function my_remove($name){ warning('deleting'); //mysqli_query($con,"DELETE FROM `book`.`".$page."` WHERE `".$page."`.`name` =\\'".$name."\\'"); //echo "<script type='text/javascript'>alert('$name');</script>"; } function modify($name){ warning('modified'); } function retrieve($name){ warning('fetching'); } function warning($message){ echo "<script type='text/javascript'>alert('$message');</script>"; } ?> 

The .js part seems to run smoothly, it sets the name and id as it should and returns a success message, but nothing else happens, not even the alert('getting something') which should run regardless of parameters. Printing out the data gives [object Object] which I'm not even sure what it means. Please help!

you're using GET not POST so under the line

 if($_GET['id'] && $_GET['name']){

should be get, not post as you have it

$id=$_GET['id'];
$name=$_GET['name'];

since your php script runs in the background with your ajax call, I don't think the alert code in that page will work. Instead try returning the plain text to the ajax function and alert it there

$(document).ready(function(){
        $("button").click(function(){
            var id = this.id;
            var name=this.name;
            console.log(id+" "+name);
            $.ajax({
                        type: 'GET',
                        url: 'utility.php',
                        dataType: 'text',
                        data: {id: id, name: name},

            }).done(function(text){
               alert(text);
            }); // Ajax Call
        }); //event handler
    }); //document.ready

and your php file like this.I changed your warning function

<?php
warning('getting something');
if($_GET['id'] && $_GET['name']){
    $id=$_GET['id'];
    $name=$_GET['name'];
    if($id=='delete'){
        my_remove($name);
    }
    if($id=='modify'){
        retrieve($name);
        my_remove($name);
        modify($name);
    }
}
else {
    warning('unable to get information');
}

function my_remove($name){
    warning('deleting');
    //mysqli_query($con,"DELETE FROM `book`.`".$page."` WHERE `".$page."`.`name` =\'".$name."\'");
    //echo "<script type='text/javascript'>alert('$name');</script>";
}

function modify($name){
    warning('modified');
}

function retrieve($name){
    warning('fetching');
}

function warning($message){
    echo $message;
}

?>

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