I have created a html form, a javascript validation function, and a PHP insert into database function. I made the html form call the javascript validation function and if the form is valid then it should call the PHP insert function but that doesn't work. So how do I call a PHP function from javascript. Note: the PHP is above the header tag, the javascript is in the header tag, and the html form is in the body tag I tried something like this in javascript: <?PHP insertintodatabase(); ?> or... <?PHP echo (insertintodatabase() ?> neither worked... Can you set a variable in javascript and pass it into PHP? or can someone give me a working example in AJAX? Thanks in advance. ~Imozeb
Here's the mostly relevant search I could help you with: bytes.com/topic/javascript/answers/445757-onload-php
i suggest you to go thru the AJAX (asynchronous javascript and xml) tutorial at w3schools.com to call php using javascript and get response..
I went through the AJAX tutorial on w3schools and couldn't find anything pertaining to my problem. Could you give me an example of how it would work?
This must help you; <html> <body> <span id="header"></span> <script type="text/javascript"> xmlhttp=null; if (window.XMLHttpRequest) {// code for Firefox, Mozilla, IE7, etc. xmlhttp=new XMLHttpRequest(); } else if (window.ActiveXObject) {// code for IE6, IE5 xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); } if (xmlhttp!=null) { xmlhttp.onreadystatechange = function() { if(xmlhttp.readyState == 4) { x=xmlhttp.responseText; document.getElementById("header").innerHTML=x; } } xmlhttp.open("GET", "urFile.php", true); xmlhttp.send(null); } else { alert("Your browser does not support XMLHTTP."); } </script> </body> </html> without the refresh of your page, this script will call the php file urFile.php. and response will come as text, whatever ufFile.php will need to print or display on webpage, that will come as response that you can use.. u just inlcude the file where ur php function exist that you need to call, include that file on urFile.php and call that function on urFile.php.. should work..