View Full Version : PHP called from javascript help??????

03-22-2009, 08:56 AM

i was wondering if i could use JS to include a php script, so that when the focus is taken off a form item, the php runs, checking the form item, and displaying an error message.

i don't need any code, im just too lazy to code and then figure out it does not work.

thanks in advance,

03-22-2009, 09:21 AM
You're looking for AJAX, me thinks.

Wiki on what AJAX is (http://en.wikipedia.org/wiki/Ajax_(programming))
Patronizing AJAX tutorial (http://www.w3schools.com/Ajax/Default.Asp)
Sample code (http://www.alistapart.com/articles/gettingstartedwithajax)

03-22-2009, 09:45 AM
Depending on the type of check you need to perform, it may also be possible to do this via just JavaScript.

03-22-2009, 01:47 PM
Sure it can.... take a simple example. Save the following code as "sample.php"

<head><title>Js calling PHP variable</title>
<script type="text/Javascript">
var jsVar = <?php require_once("include.php"); echo "'".$someVariableInPhp."'"; ?>
// rest of the Js code here

03-23-2009, 02:33 AM
so i could take that and run almost anything in between <?php ?>, and would this be any time, even after the page is loaded?

03-23-2009, 02:47 AM
You have got to be joking. All php code on a page is executed when that page is requested from the web server. The php code that rochakchauhan posted was executed and the value was echoed at the time the page was sent to the browser.

If you want php to act upon anything that is produced in the browser, you must get that information to the server in the form a http page request, which is why AJAX has already been mentioned as a possible way of accomplishing the validation.