Insert record into db by using ajax post method?

Thread Solved

Join Date: Feb 2009
Posts: 83
Reputation: ahmksssv is an unknown quantity at this point 
Solved Threads: 7
ahmksssv ahmksssv is offline Offline
Junior Poster in Training

Insert record into db by using ajax post method?

 
0
  #1
Feb 26th, 2009
Hi ...

i need to inserting around 20 values...which method is best in ajax post or get..i dont know abt ajax..plz send me ajax registration or small form (which having inserting records into db) ...i can learn by using this....

Thank u..
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 596
Reputation: buddylee17 has a spectacular aura about buddylee17 has a spectacular aura about 
Solved Threads: 125
buddylee17's Avatar
buddylee17 buddylee17 is offline Offline
Posting Pro

Re: Insert record into db by using ajax post method?

 
1
  #2
Feb 26th, 2009
Here is a simple AJAX form submission using POST. It basically sends the first and last name to the server, and the server outputs the form information and the date on the server. Very simple, but easy to build on.
post.php:
  1. <?php
  2. $FirstName = $_POST['FirstName'];
  3. $LastName = $_POST['LastName'];
  4. $today = date("m/d/Y");
  5. //send output back to page.
  6. echo "Hello $FirstName $LastName. Todays date is $today.";
  7. ?>
html file(name it whatever you want):
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>Untitled Document</title>
  6. <script type="text/javascript" language="javascript">
  7. var http_request = false;
  8. function makePOSTRequest(url, parameters) {
  9. http_request = false;
  10. if (window.XMLHttpRequest) { // Mozilla, Safari,...
  11. http_request = new XMLHttpRequest();
  12. if (http_request.overrideMimeType) {
  13. // set type accordingly to anticipated content type
  14. //http_request.overrideMimeType('text/xml');
  15. http_request.overrideMimeType('text/html');
  16. }
  17. } else if (window.ActiveXObject) { // IE
  18. try {
  19. http_request = new ActiveXObject("Msxml2.XMLHTTP");
  20. } catch (e) {
  21. try {
  22. http_request = new ActiveXObject("Microsoft.XMLHTTP");
  23. } catch (e) {}
  24. }
  25. }
  26. if (!http_request) {
  27. alert('Cannot create XMLHTTP instance');
  28. return false;
  29. }
  30.  
  31. http_request.onreadystatechange = alertContents;
  32. http_request.open('POST', url, true);
  33. http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  34. http_request.setRequestHeader("Content-length", parameters.length);
  35. http_request.setRequestHeader("Connection", "close");
  36. http_request.send(parameters);
  37. }
  38.  
  39. function alertContents() {
  40. if (http_request.readyState == 4) {
  41. if (http_request.status == 200) {
  42. //alert(http_request.responseText);
  43. result = http_request.responseText;
  44. document.getElementById('myspan').innerHTML = result;
  45. } else {
  46. alert(http_request.status);
  47. }
  48. }
  49. }
  50.  
  51. function get(obj) {
  52. var poststr = "FirstName=" + encodeURI( document.getElementById("FirstName").value ) +
  53. "&LastName=" + encodeURI( document.getElementById("LastName").value );
  54. makePOSTRequest('post.php', poststr);
  55. }
  56. </script>
  57. </head>
  58. <body>
  59. <form action="javascript:get(document.getElementById('myform'));" name="myform" id="myform">
  60. <input type="text" id="FirstName" />
  61. <input type="text" id="LastName" />
  62. <input type="Submit" value="Submit" />
  63. </form>
  64. <div id="myspan">This area will be filled with the servers output after submit is clicked.</div>
  65. </body>
  66. </html>
Try it out and study how it works.
Last edited by buddylee17; Feb 26th, 2009 at 10:48 am.
Lost time is never found again.
- Benjamin Franklin
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 83
Reputation: ahmksssv is an unknown quantity at this point 
Solved Threads: 7
ahmksssv ahmksssv is offline Offline
Junior Poster in Training

Re: Insert record into db by using ajax post method?

 
0
  #3
Feb 27th, 2009
Thank You....
its easy to understand...
is it same for more no.of fields? or is there any other way to send more no.of fields....
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 596
Reputation: buddylee17 has a spectacular aura about buddylee17 has a spectacular aura about 
Solved Threads: 125
buddylee17's Avatar
buddylee17 buddylee17 is offline Offline
Posting Pro

Re: Insert record into db by using ajax post method?

 
0
  #4
Feb 27th, 2009
Yes, the same for more fields. Basically, if you add a text field into the form, you'll need to update the javascript variable, postr on line 52 above so that it will send the value to the php file.

Also, for better design, the javascript should be placed in an external .js file (for better organization and so it can be cached) and called from the head of the document.
Lost time is never found again.
- Benjamin Franklin
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 83
Reputation: ahmksssv is an unknown quantity at this point 
Solved Threads: 7
ahmksssv ahmksssv is offline Offline
Junior Poster in Training

Re: Insert record into db by using ajax post method?

 
0
  #5
Feb 28th, 2009
Originally Posted by buddylee17 View Post
Yes, the same for more fields. Basically, if you add a text field into the form, you'll need to update the javascript variable, postr on line 52 above so that it will send the value to the php file.

Also, for better design, the javascript should be placed in an external .js file (for better organization and so it can be cached) and called from the head of the document.

Hi ..

Thank u....i learn a lot from u.....
i cleared my doubt for inserting data....

when i retreiving data from database, how can i get values from php page to html page by using ajax...i mean same concept in reverse formate...

Thank u once again...
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 7
Reputation: fskreuz is an unknown quantity at this point 
Solved Threads: 1
fskreuz fskreuz is offline Offline
Newbie Poster

Re: Insert record into db by using ajax post method?

 
0
  #6
Mar 8th, 2009
Originally Posted by ahmksssv View Post
Hi ..

Thank u....i learn a lot from u.....
i cleared my doubt for inserting data....

when i retreiving data from database, how can i get values from php page to html page by using ajax...i mean same concept in reverse formate...

Thank u once again...
Im kinda more like a beginner at this but i am familliar of using ajax (thanks to marky and the mct team who taught me)

if you try to access the post.php page directly, what prints out in this page is what is forwarded by this script to your page. in short, your page gets the contents of the post.php and display it there.

now to display the data, you must edit the line 52 to meet your query requirements. as soon as this is done, you edit the post.php to get the data and print it on the post.php page. automatically, the script will get the data from the post.php page and displays it to your current page.

example:

you have a page with an item and its button with "get item data" as its value. hidden with that button is a hidden input tag with that item's id. when you click the button, the id is passed to the script which it will then pass to the post.php. then the post.php will display the data you need using that item's id number on the post.php page. then, since its the purpose of ajax, the script will get the data from the post.php page then on to your page.

it's simple as that actually.
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 PHP Forum
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC