integrating php and javascript? is this possible?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
hairytea
Forum Commoner
Posts: 92
Joined: Mon Feb 04, 2008 8:31 am

integrating php and javascript? is this possible?

Post 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?
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Re: integrating php and javascript? is this possible?

Post by Zoxive »

Code: Select all

header("Location: ..");
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
Last edited by Zoxive on Wed Feb 06, 2008 8:14 am, edited 1 time in total.
User avatar
Stryks
Forum Regular
Posts: 746
Joined: Wed Jan 14, 2004 5:06 pm

Re: integrating php and javascript? is this possible?

Post 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
hairytea
Forum Commoner
Posts: 92
Joined: Mon Feb 04, 2008 8:31 am

Re: integrating php and javascript? is this possible?

Post 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.
User avatar
Stryks
Forum Regular
Posts: 746
Joined: Wed Jan 14, 2004 5:06 pm

Re: integrating php and javascript? is this possible?

Post 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.
Festy
Forum Commoner
Posts: 28
Joined: Wed Jan 30, 2008 10:01 pm

Re: integrating php and javascript? is this possible?

Post 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.
Post Reply