user3623435
user3623435

Reputation: 3

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!

Upvotes: 0

Views: 2095

Answers (2)

Vipin Kumar KM
Vipin Kumar KM

Reputation: 366

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;
}

?>

Upvotes: 0

Billy
Billy

Reputation: 2448

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'];

Upvotes: 1

Related Questions