944,183 Members | Top Members by Rank

Ad:
Nov 8th, 2009
0

sending more than the value through ajax

Expand 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>
Similar Threads
Reputation Points: 12
Solved Threads: 6
Posting Whiz in Training
itisnot_me is offline Offline
245 posts
since Apr 2009
Nov 8th, 2009
1
Re: sending more than the value through ajax
Click to Expand / Collapse  Quote originally posted by itisnot_me ...
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
html Syntax (Toggle Plain Text)
  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
Reputation Points: 26
Solved Threads: 31
Posting Whiz
samarudge is offline Offline
354 posts
since May 2008
Nov 8th, 2009
0
Re: sending more than the value through ajax
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
Reputation Points: 12
Solved Threads: 6
Posting Whiz in Training
itisnot_me is offline Offline
245 posts
since Apr 2009
Nov 9th, 2009
0
Re: sending more than the value through ajax
The request URL would have _GET varaibles in, just create the file action.php with the contents
php Syntax (Toggle Plain Text)
  1. <?php
  2. print_r($_GET);
  3. ?>
and view the output in Firebug (XHTML requests section)
Reputation Points: 26
Solved Threads: 31
Posting Whiz
samarudge is offline Offline
354 posts
since May 2008
Nov 9th, 2009
0
Re: sending more than the value through ajax
This is a good tutorial on Advanced AJAX with MySQL
http://www.tizag.com/ajaxTutorial/aj...l-database.php
Reputation Points: 26
Solved Threads: 31
Posting Whiz
samarudge is offline Offline
354 posts
since May 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in JavaScript / DHTML / AJAX Forum Timeline: AJAX comment form
Next Thread in JavaScript / DHTML / AJAX Forum Timeline: conflicting javascript - clock/time and random background





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC