Page 1 of 1
integrating php and javascript? is this possible?
Posted: Wed Feb 06, 2008 6:33 am
by hairytea
I am building a website in flash with a contact form and i want form field validation i.e. i want to ensure fields were not left blank before submission.
the following code is a snippet from my sendmail.php code...
elseif (empty($email) || empty($message) || empty($name)) {
echo '<script type="text/javascript">alert("You have failed to complete one of the form fields.\nPlease try again.")</script>';
header( "Location:
http://www.thanweb.co.uk/gap.html" );
}
The php script correctly identifies that fields were left blank and reloads the page.
But, it does not invoke the javascript alert(); function to warn the visitor that one of the fields were left blank/empty.
any ideas?
Re: integrating php and javascript? is this possible?
Posted: Wed Feb 06, 2008 8:11 am
by Zoxive
Is a server side redirect. Meaning nothing is sent to the browser other then telling it to redirect.
So your echo is never being sent.
Normally you would get an Error Saying "Headers Already Sent", so I'm guessing you don't have error reporting on.
Code: Select all
error_reporting(E_ALL);
ini_set('display_errors',true);
// Put this at the top of the page, and you should see some errors
Re: integrating php and javascript? is this possible?
Posted: Wed Feb 06, 2008 8:12 am
by Stryks
PHP operates on the server side, and javascript operates on the client side. They cannot work together in the way you are attempting as they are run at different times in different locations. (Actually, that header shouldn't work should it? Coming after output like that? )
That's not to say that you cant have PHP generate javascript to be executed when the page has loaded, or embed values into a hidden form field.
It's also not to say that you couldn't use AJAX to submit the form data to a PHP processing script and return pass/fail data to the submitting page.
I'm sure there are other solutions as well.
Cheers
Re: integrating php and javascript? is this possible?
Posted: Thu Feb 07, 2008 4:47 am
by hairytea
Stryks wrote:PHP operates on the server side, and javascript operates on the client side. They cannot work together in the way you are attempting as they are run at different times in different locations. (Actually, that header shouldn't work should it? Coming after output like that? )
That's not to say that you cant have PHP generate javascript to be executed when the page has loaded, or embed values into a hidden form field.
It's also not to say that you couldn't use AJAX to submit the form data to a PHP processing script and return pass/fail data to the submitting page.
I'm sure there are other solutions as well.
Cheers
Novice question here....
What is AJAX?
I have just completed a CIW course teaching me Perl and JavaScript and just started PHP (from a seperate revision book that i have purchased myself), but todate have not come across AJAX.
Re: integrating php and javascript? is this possible?
Posted: Sat Feb 09, 2008 12:07 am
by Stryks
AJAX is pretty much just a javascript technique.
The client machine receives a page which contains javascript code which can load another php page in the background and update sections of the already loaded page. Form submission is one example. Form is filled in, submitted in the background to a PHP script, and success / failure messages returned to the form and used to alert the user to any problems.
Have a search around these forums for AJAX and forms ... you should find quite a lot of information.
Re: integrating php and javascript? is this possible?
Posted: Sat Feb 09, 2008 12:59 am
by Festy
Stryks wrote:AJAX is pretty much just a javascript technique.
The client machine receives a page which contains javascript code which can load another php page in the background and update sections of the already loaded page. Form submission is one example. Form is filled in, submitted in the background to a PHP script, and success / failure messages returned to the form and used to alert the user to any problems.
....so this way pages load faster because the you already have pages in background on client side., rather then having to fetch pages/files for each individual request. This is the main advantage of AJAX.