943,789 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Marked Solved
  • Views: 8655
  • PHP RSS
Feb 26th, 2009
0

Insert record into db by using ajax post method?

Expand Post »
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..
Similar Threads
Reputation Points: 11
Solved Threads: 7
Junior Poster in Training
ahmksssv is offline Offline
84 posts
since Feb 2009
Feb 26th, 2009
1

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

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:
php Syntax (Toggle Plain Text)
  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):
html Syntax (Toggle Plain Text)
  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.
Reputation Points: 232
Solved Threads: 137
Practically a Master Poster
buddylee17 is offline Offline
665 posts
since Nov 2007
Feb 27th, 2009
0

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

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....
Reputation Points: 11
Solved Threads: 7
Junior Poster in Training
ahmksssv is offline Offline
84 posts
since Feb 2009
Feb 27th, 2009
0

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

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.
Reputation Points: 232
Solved Threads: 137
Practically a Master Poster
buddylee17 is offline Offline
665 posts
since Nov 2007
Feb 28th, 2009
0

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

Click to Expand / Collapse  Quote originally posted by buddylee17 ...
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...
Reputation Points: 11
Solved Threads: 7
Junior Poster in Training
ahmksssv is offline Offline
84 posts
since Feb 2009
Mar 8th, 2009
0

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

Click to Expand / Collapse  Quote originally posted by ahmksssv ...
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.
Reputation Points: 10
Solved Threads: 1
Newbie Poster
fskreuz is offline Offline
14 posts
since Feb 2009

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 PHP Forum Timeline: Paypal Integration with PHP MySQL
Next Thread in PHP Forum Timeline: Using <select> tag with PHP





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


Follow us on Twitter


© 2011 DaniWeb® LLC