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?
integrating php and javascript? is this possible?
Moderator: General Moderators
Re: integrating php and javascript? is this possible?
Code: Select all
header("Location: ..");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
Last edited by Zoxive on Wed Feb 06, 2008 8:14 am, edited 1 time in total.
Re: integrating php and javascript? is this possible?
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
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?
Novice question here....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
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?
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.
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?
....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.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.