PHP-AJAX

SAJAX-Passing Arguments and MySQL Database Manipulation

In the last post I presented an overview of the SAJAX library. Today I will talk about argument passing to and from the SAJAX functions and the respective JavaScript function. Then I shall make a sample HTML form and pass the values to the PHP function for addition into a database. If you are new to SAJAX and don’t know what I’m talking about I suggest you view my earlier post which can be found at: https://garagedeveloper.wordpress.com/2009/01/09/using-ajax-with-php/

The code last time looked something like:

<?php require('Sajax.php'); function worker(){return 'hello World';} $sajax_request_type = "GET"; sajax_init(); $sajax_debug_mode=1; sajax_export("worker"); sajax_handle_client_request(); ?>

sajax_show_javascript();

?>

X_worker(callback);

Function callback(retArg){

Alert(retArg);}

Make sure you have the SAJAX libraries.

Let’s make the worker function do something interesting then a mere hello world. I will edit the worker function so now it takes two strings concatenates them and returns the result back to the JavaScript Function.

So I change the worker function and now it looks like this:

function worker($arg1,$arg2)
{
	$cat=$arg1.' '.$arg2;
	return $cat;
}

Notice however I don’t change the “export” call arguments. So the code till this point looks like:

<?php require('Sajax.php'); function worker($arg1,$arg2) { $cat=$arg1.' '.$arg2; return $cat; } $sajax_request_type = "GET"; sajax_init(); $sajax_debug_mode=1; sajax_export("worker"); sajax_handle_client_request(); ?>

sajax_show_javascript();

?>

X_worker(callback);

Function callback(retArg){

Alert(retArg);}

Now I change the respective JavaScript function Call to look like this:

x_worker('something','intresting',cb)

You can pass any number of arguments at the start and the last argument always is the Call-back function name. So the code looks like:

<?php require('Sajax.php'); function worker($arg1,$arg2) { $cat=$arg1.' '.$arg2; return $cat; } $sajax_request_type = "GET"; sajax_init(); $sajax_debug_mode=1; sajax_export("worker"); sajax_handle_client_request(); ?>

sajax_show_javascript();

?>

x_worker('something','intresting',callback)

Function callback(retArg){

Alert(retArg);}

When the retArg alerts you get something like: “something interesting”. Easy wasn’t it ?. Similarly you can pass any number of arguments this way to your PHP function be it arrays, objects, and strings. Just make sure you don’t echo something out in the PHP function or have your include libraries before the SAJAX stuff.

Now let’s look at how you can get values in your JavaScript function in a similar way. For this I shall declare an array in the PHP function and make it return the array to the JavaScript. So Now the PHP function looks like this:

function worker()
{

 $arr = array("foo" => "bar", "Max" => "Sajax");

 return $arr;

}

$arr is an array with bar at location “foo” and sajax at location “Max”. In the respective JavaScript function I output the values like :

alert(retArg["foo"]);

which outputs bar where retArg is the arg from the call-back function. The code is now:

<?php require('Sajax.php'); function worker() { $arr = array("foo" => "bar", "Max" => "Sajax");
	return $arr;
}

$sajax_request_type = "GET";

sajax_init();

$sajax_debug_mode=1;

sajax_export("worker");

sajax_handle_client_request();

?>

sajax_show_javascript();

?>

x_worker(callback)

Function callback(retArg){

Alert(retArg[“foo”]);}

Okay now that you’ve got grips with all this (I hope) let’s move to creating a simple html form getting data from it and inserting it in a database which is mysql in this case. Ok so let’s begin. First I edit the worker function so that I perform a simple insert MYSQL query. Ill assume you have already setup the connection with the database, I place mine in a separate file which in this case it is ‘database.php’. The worker function now becomes:

function worker($name,$dob,$address,$pnum)
{
 mysql_query("INSERT INTO tbl_name (name,dob,address,pnum) VALUES($name,$dob,$address,$pnum)";
 return 0;
}

I create a simple HTML form which holds some basic details and looks likes this:

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>Untitled Document</title>

</head>


<form name="frmDetails">

Name:<input type="text" name="nam" /> 

DOB:<input type="text" name="DOB" />

Address:<input type="text" name="Address" />

Phone Number:<input type="text" name="Pnum" />

<input type="submit" value="Submit" onclick="run()"/>

</form>


<body>

</body>

</html>

Notice on the onClick event I’m calling a run() function. This is the function which takes all the values from the form when the submit button is clicked and calls our JavaScript worker function so the run function is:

function run()
{
	x_worker(frmDetails.nam.value,frmDetails.DOB.value,frmDetails.Address.value,frmDetails.Pnum.value,callback);
}

I get the values from the form and pass them to the worker function. In the PHP worker function I return a 0 which can act as an identifier if everything went alright or not. The entire code is as follows:

<?php require('Sajax.php'); include(‘ database.php’); function worker($name,$dob,$address,$pnum) { mysql_query("INSERT INTO tbl_name (name,dob,address,pnum) VALUES($name,$dob,$address,$pnum)"; return 0; } $sajax_request_type = "GET"; sajax_init(); //$sajax_debug_mode=1; sajax_export("worker"); sajax_handle_client_request(); ?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>
sajax_show_javascript();?>
function callback(retArg)

{alert(retArg);}

function run()

{ x_worker(frmDetails.nam.value,frmDetails.DOB.value,frmDetails.Address.value,frmDetails.Pnum.value,callback);

}

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>Untitled Document</title>

</head>


<form name="frmDetails">

Name:<input type="text" name="nam" /> 

DOB:<input type="text" name="DOB" />

Address:<input type="text" name="Address" />

Phone Number:<input type="text" name="Pnum" />

<input type="submit" value="Submit" onclick="run()"/>

</form>


<body>

</body>

</html>

Hope this helps and clears out some confusion on the use of SAJAX J

Advertisements

13 thoughts on “SAJAX-Passing Arguments and MySQL Database Manipulation

      1. Thanks again, I appreciate that.

        Sajax is a great tool, but it’s kinda sad developers abandonded this project. Hope one day we’ll see new edition of it.

      2. Np. I agree it is but most of the things are similar to jquery dunno if you’ve managed to use that or not but its even better.

  1. what if we use mysql_query($query,$connection)
    i’ve try to use that but nothing happen….
    but when we use mysql_query($query) ===> without using $connection parameter it work…
    please help me.. because i want to use 2 connection in my application

    thanks..
    please reply to this email
    new_baru17@yahoo.com

    1. well, it works the same way as it would normally in php, try to do a proper mysql_connect(*) before save the resource and then try to use it in your query, hope this helps

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s