Jump to content
Larry Ullman's Book Forums
sonal

Php Script Is Not Working After Adding Ajax

Recommended Posts

I'm trying to add new records into database using ajax layer. I'm following the example given in the book. My php script works fine itself. But when I add ajax.js and add_client.js, the script does not give result.

 

It seems like the script is not getting form values. I tried to print values after fields data, and the alert shows "This alert values shows "fname=null&lname=null&email=null"

 

When I run the script, the php script goes into query failure part. But I can say, that php script is alright but it is working fine, when run without ajax parts.

 

 

window.onload = init;

function init()

{

var ajax = getXMLHttpRequestObject();

 

if(ajax)

{

if(document.getElementById('results'))

{

document.getElementById('client_form').onsubmit =

function()

{

ajax.open('post', 'add_client_xml.php');

ajax.onreadystatechange = function()

{

handleResponse(ajax);

}

var fields = ['fname', 'lname', 'email'];

for(var i = 0; i < fields.length; i++)

{

fields = fields + '=' + encodeURIComponent(document.getElementById(fields.value));

 

}

var values = fields.join('&');

alert(values);

 

 

This alert values shows "fname=null&lname=null&email=null"

Share this post


Link to post
Share on other sites

I think your problem is here:

 

fields = fields + '=' + encodeURIComponent(document.getElementById(fields.value));

 

Change the end to the following:

 

encodeURIComponent(document.getElementById(fields).value)

 

Basically, move one of the right parentheses. Note that the getElementById method needs the ID of an HTML element, and once you have that DOM object, you can get the value of it (if it's an input element) using the value property.

 

Make sense?

  • Upvote 1

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...