Hello, I'm looking for a simple example of insert form data into my db using ajax if all good I will get success text message and if not than fail message (not ALERT message). Can you please help me? Thanks in advanced Roi
You can visit W3Schools, great source of learning. http://www.w3schools.com/php/php_ajax_php.asp Code (markup):
OK, so I built this code, but I don't get any result: <html> <head> <script language="javascript" type="text/javascript"> <!-- //Browser Support Code function ajaxFunction(){ var ajaxRequest; // The variable that makes Ajax possible! try{ // Opera 8.0+, Firefox, Safari ajaxRequest = new XMLHttpRequest(); }catch (e){ // Internet Explorer Browsers try{ ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP"); }catch (e) { try{ ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP"); }catch (e){ // Something went wrong alert("Your browser broke!"); return false; } } } // Create a function that will receive data // sent from the server and will update // div section in the same page. ajaxRequest.onreadystatechange = function(){ if(ajaxRequest.readyState == 4){ var ajaxDisplay = document.getElementById('ajaxDiv'); ajaxDisplay.value = ajaxRequest.responseText; } } // Now get the value from user and pass it to // server script. var x = document.getElementById('x').value; var y = document.getElementById('y').value; var queryString = "?x=" + x ; queryString += "&y=" + y; ajaxRequest.open("GET", "2.php" + queryString, true); ajaxRequest.send(null); } //--> </script> </head> <body> <form name='myForm'> <input name='x' /> <input name='y' /> <input type='button' onclick='ajaxFunction()' value='Query MySQL'/> </form> <br /> <div id='ajaxDiv'>Your result will display here</div> </body> </html> PHP: in 2.php I just put: <?PHP error_reporting(E_ERROR | E_WARNING | E_PARSE); $x=$_GET["x"]; $y=$_GET["y"]; echo $x; echo $y; ?> PHP: What did I miss?
Jquery has a very simple AJAX call with only a few lines of code. You may want to look into that as it already is supported by all major browsers and performs the necessary error handling and checks