sending more than the value through ajax

Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Thread Solved

Join Date: Apr 2009
Posts: 82
Reputation: itisnot_me is an unknown quantity at this point 
Solved Threads: 5
itisnot_me itisnot_me is offline Offline
Junior Poster in Training

sending more than the value through ajax

 
0
  #1
28 Days Ago
ok so i am an avid php developer and i am starting to get into ajax so i can keep file sizes and code down to a minimum.

i am looking in the w3schools ajax examples but i am a little lost at how to send through a variable to the page i am using to process.

what i would like to do is send through an id that is being recalled from the database to tell it what to save over.

any help would be very helpful. thanks in advance

ajax
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. function action()
  2. {
  3. var xmlhttp;
  4. if (window.XMLHttpRequest)
  5. {
  6. // code for IE7+, Firefox, Chrome, Opera, Safari
  7. xmlhttp=new XMLHttpRequest();
  8. }
  9. else
  10. {
  11. // code for IE6, IE5
  12. xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  13. }
  14. xmlhttp.onreadystatechange=function()
  15. {
  16. if(xmlhttp.readyState==4)
  17. {
  18. document.myForm.time.value=xmlhttp.responseText;
  19. }
  20. }
  21. xmlhttp.open("POST","action.php",true);
  22. xmlhttp.send(null);
  23. }

html form
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <form action="#" method="post">
  2. <button name="save" type="submit" value="Save" onclick="action();">Save</button>
  3. <input type="text" name="address" />
  4. </form>
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 252
Reputation: samarudge is an unknown quantity at this point 
Solved Threads: 20
samarudge samarudge is offline Offline
Posting Whiz in Training
 
1
  #2
28 Days Ago
Originally Posted by itisnot_me View Post
ok so i am an avid php developer and i am starting to get into ajax so i can keep file sizes and code down to a minimum.

i am looking in the w3schools ajax examples but i am a little lost at how to send through a variable to the page i am using to process.

what i would like to do is send through an id that is being recalled from the database to tell it what to save over.

any help would be very helpful. thanks in advance

ajax
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. function action()
  2. {
  3. var xmlhttp;
  4. if (window.XMLHttpRequest)
  5. {
  6. // code for IE7+, Firefox, Chrome, Opera, Safari
  7. xmlhttp=new XMLHttpRequest();
  8. }
  9. else
  10. {
  11. // code for IE6, IE5
  12. xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  13. }
  14. xmlhttp.onreadystatechange=function()
  15. {
  16. if(xmlhttp.readyState==4)
  17. {
  18. document.myForm.time.value=xmlhttp.responseText;
  19. }
  20. }
  21. xmlhttp.open("POST","action.php",true);
  22. xmlhttp.send(null);
  23. }

html form
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <form action="#" method="post">
  2. <button name="save" type="submit" value="Save" onclick="action();">Save</button>
  3. <input type="text" name="address" />
  4. </form>
Sending data:
The easiest way to send data to your server from the form is using _GET variables
You can take the variables from your form and add them to your string like this
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. function action()
  2. {
  3. var xmlhttp;
  4. if (window.XMLHttpRequest)
  5. {
  6. // code for IE7+, Firefox, Chrome, Opera, Safari
  7. xmlhttp=new XMLHttpRequest();
  8. }
  9. else
  10. {
  11. // code for IE6, IE5
  12. xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  13. }
  14. xmlhttp.onreadystatechange=function()
  15. {
  16. if(xmlhttp.readyState==4)
  17. {
  18. document.myForm.time.value=xmlhttp.responseText;
  19. }
  20. }
  21. var textfielddata = document.getElementByID('a_field').value;
  22. xmlhttp.open("GET","action.php?Value="+textfielddata);
  23. xmlhttp.send(null);
  24. }

And this would be your new HTML
  1. <form action="#" method="post">
  2. <input id="a_field" name='SomeField' type='text' />
  3. <button name="save" type="submit" value="Save" onclick="action();">Save</button>
  4. <input type="text" name="address" />
  5. </form>
You can do this with as many variables as you like
My Blog, Life and everything that matters to me - SamRudge.co.uk

2x Macbook Pro's, 1x Mac Pro, 1x iMac, 2x Macbook's running Fedora linux - In conclusion, I hate windows =)
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 82
Reputation: itisnot_me is an unknown quantity at this point 
Solved Threads: 5
itisnot_me itisnot_me is offline Offline
Junior Poster in Training
 
0
  #3
28 Days Ago
alright. but will the address field get sent through to the php file or will i have to build in a var to send it over?

Also the id (which is a mysql row id) that i would like to send through doesn't need to be shown in a input field, and the row id is not going to be the same through out. it will be different based upon the file there looking at.

thanks
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 252
Reputation: samarudge is an unknown quantity at this point 
Solved Threads: 20
samarudge samarudge is offline Offline
Posting Whiz in Training
 
0
  #4
27 Days Ago
The request URL would have _GET varaibles in, just create the file action.php with the contents
  1. <?php
  2. print_r($_GET);
  3. ?>
and view the output in Firebug (XHTML requests section)
My Blog, Life and everything that matters to me - SamRudge.co.uk

2x Macbook Pro's, 1x Mac Pro, 1x iMac, 2x Macbook's running Fedora linux - In conclusion, I hate windows =)
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 252
Reputation: samarudge is an unknown quantity at this point 
Solved Threads: 20
samarudge samarudge is offline Offline
Posting Whiz in Training
 
0
  #5
27 Days Ago
This is a good tutorial on Advanced AJAX with MySQL
http://www.tizag.com/ajaxTutorial/aj...l-database.php
My Blog, Life and everything that matters to me - SamRudge.co.uk

2x Macbook Pro's, 1x Mac Pro, 1x iMac, 2x Macbook's running Fedora linux - In conclusion, I hate windows =)
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the JavaScript / DHTML / AJAX Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC