View Full Version : AJAX and PHP

11-12-2008, 06:15 PM
I'm learning ajax and up to this point, I've made web-pages that have 1 form made up of javascript/html that refers to a php page that only has code to process (i.e. update, insert into db) the form from the html page.

I now would like to have 3 unique registration pages, coded in javascript/html, and still have only 1 php page that does the inserting and updating, rather than having 3 php pages to do the backend work.

I thought of creating functions in the php page and correspondingly have the variable data (in the javascript code) enclosed in the appropriate function, so when I send the data only the intended php code is executed. Is that the way to go? Or is there another, better way?

Insight and suggestions are much appreciated. Thanks

11-12-2008, 06:43 PM
in your form have a hidden field, for the example let's call it "which". set that field to a unique value. on your processor page have it pull that variable, then set up a switch based on it. you could have tons and tons of things built into one processor page that way.

11-13-2008, 06:12 PM
Thanks for the response. In the process of adjusting my code, I seemingly altered something that now causes my code to no longer work... I've gone over the code and I can't find what's wrong with it... I've tested both the javascript/ajax side and the php side of the code. What seems to be happening is that the ajax component is not sending or the php side is not accepting the posted variables. Below are my ajax and php codes, can someone take a look and let me know what they think/know is wrong with it? Thanks.

<script language="javascript">
function callback(ajaxResponse){

function sendData(){

var XMLHttpRequestObj = false;

var interval = document.forms['basicRegister'].elements['joinType'].length;
for (i=0; i<interval; i++){
var joinType = document.forms['basicRegister'].elements['joinType'][i].value;
var firstName = document.forms['basicRegister'].elements['firstName'].value;
var lastName = document.forms['basicRegister'].elements['lastName'].value;
var email = document.forms['basicRegister'].elements['email'].value;
var password = document.forms['basicRegister'].elements['password'].value;

var params = "firstName=" + firstName + "&" + "lastName=" + lastName + "&" + "email=" + email + "&" + "password=" + password + "&" + "joinType=" + joinType;

if (window.XMLHttpRequest){
XMLHttpRequestObj = new XMLHttpRequest();
} else if (window.ActiveXObject){
XMLHttpRequestObj = new ActiveXObject("Microsoft.XMLHttp");

if (XMLHttpRequestObj){
var url = "registration.php";
XMLHttpRequestObj.open("POST", url, true);
XMLHttpRequestObj.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
XMLHttpRequestObj.setRequestHeader("Content-length", params.length);
XMLHttpRequestObj.setRequestHeader("Connection", "close");

XMLHttpRequestObj.onreadystatechange = function(){
if (XMLHttpRequestObj.readystate == 4 && XMLHttpRequestObj.status == 200){



include ("objects.php");

$dbConn = new dbConnection();
$format = new stringFormat();

$connected = $dbConn->conn(); // connect to db
$dbConn->select_db(); // select db

for ($i=0; $i<6; $i++) // generate id
$Id .= rand(0,1) ? chr(rand(48,57)) : chr(rand(97,122));

$firstName = $_POST['firstName'];
$lastName = $_POST['lastName'];
$email = $_POST['email'];
$password = $_POST['password'];
$joinType = $_POST['joinType'];

$insertQuery = sprintf("INSERT INTO registration (Id, firstName, LastName, joinType, email, password) VALUES(%s, %s, %s, %s, %s, %s)",

$result = mysql_query($insertQuery, $connected);

Note: I've used objects in the php code and they are working correctly.

11-14-2008, 06:12 PM
The problem with the code was two incorrect variables in the onreadystatechange function:

1. I did not capitalize the "S" in .readyState
2. I left out "Obj" to XMLHttpRequest - the Http request object.

XMLHttpRequestObj.onreadystatechange = function(){
if (XMLHttpRequestObj.readyState == 4 && XMLHttpRequestObj.status == 200){

I do have 2 follow-up questions however, if anyone has the answer to them, please let me know. Thanks.

1. The alert message that I've setup only occurs every other entry, i.e. the first time I fill and submit the form, the alert occurs; second time through, alert doesn't occur; third time, the alert occurs. Why is that?

2. Once the first form is successfully inserted into the database, I'd like to display another form. How can I confirm a successful insert and only then display another, new form?