{"id":1329,"date":"2022-08-30T15:15:37","date_gmt":"2022-08-30T15:15:37","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/10\/submitting-a-form-using-ajax-still-not-working-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:15:37","modified_gmt":"2022-08-30T15:15:37","slug":"submitting-a-form-using-ajax-still-not-working-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/submitting-a-form-using-ajax-still-not-working-collection-of-common-programming-errors\/","title":{"rendered":"Submitting a form using AJAX STILL not working-Collection of common programming errors"},"content":{"rendered":"<p>I&#8217;ve spend the past 5hours trying to avoid posting this, but i just cant find where im wrong on this code. I wish to send a comment to email without leaving the HTML page but just adding PHP echo at the bottom of the form. This is my HTML:<\/p>\n<pre><code>\n<\/code><\/pre>\n<table>\n<tr>\n<td valign=\"top\">First Name *<\/td>\n<td valign=\"top\"><\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">Last Name *<\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">Email Address *<\/td>\n<td valign=\"top\"><\/td>\n<\/tr>\n<tr>\n<td valign=\"top\"><\/td>\n<\/tr>\n<tr>\n<td valign=\"top\">Comments *<\/td>\n<td valign=\"top\"><\/td>\n<\/tr>\n<tr>\n<td colspan=\"2\" style=\"text-align:center\"><\/td>\n<\/tr>\n<\/table>\n<p>my AJAX:<\/p>\n<pre><code>function submitForm(str)\n{\nif (str==\"\")\n  {\n  document.getElementById(\"text\").innerHTML=\"\";\n  return;\n  }\nif (window.XMLHttpRequest)\n  {\/\/ code for IE7+, Firefox, Chrome, Opera, Safari\n  xmlhttp=new XMLHttpRequest();\n  }\nelse\n  {\/\/ code for IE6, IE5\n  xmlhttp=new ActiveXObject(\"Microsoft.XMLHTTP\");\n  }\nxmlhttp.onreadystatechange=function()\n  {\n  if (xmlhttp.readyState==4 &amp;&amp; xmlhttp.status==200)\n    {\n    document.getElementById(\"text\").innerHTML=xmlhttp.responseText;\n    }\n  }\nxmlhttp.open(\"GET\",\"contact.php?q=\"+str,true);\n\nxmlhttp.send();\n}\n<\/code><\/pre>\n<p>I can confirm that my php works fine because when i change my form action to &#8220;contact.php&#8221; it works perfectly. The problem is when I try to get the php using AJAX, firefox developer tools confirms that the contact.php is called fine with the funtion: submitForm() but its just not working. Can anybody help?<\/p>\n<ol>\n<li>\n<p>One problem is this: &#8220;contact.php?q=&#8221;+str<\/p>\n<p>Based on the markup above, str will always be undefined, so you are never sending your form data to contact.php. Additionally, when you set the action=&#8221;contact.php&#8221; for testing, you&#8217;re using a POST, and your ajax call is a GET. So you could still have problems with the PHP script.<\/p>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-10 00:17:15. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve spend the past 5hours trying to avoid posting this, but i just cant find where im wrong on this code. I wish to send a comment to email without leaving the HTML page but just adding PHP echo at the bottom of the form. This is my HTML: First Name * Last Name * [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1329","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1329","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=1329"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1329\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1329"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1329"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1329"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}