View Full Version : Javascript in php

08-05-2007, 07:52 PM
Hi there,

Im my private message system that i am working on, i have a form where the person that wants to send a private message types in the recipient username, subject and message.

When they click send it goes to a javascript validate form, and any errors come up with javascript alerts.

But, im trying to do validate weather the reciepent username enterd is valid. Ive put the php in my validation form as this...

$SQL2 = "SELECT * from USERS WHERE USERNAME = '<script type='text/javascript'>
$result2 = @mysql_query( $SQL2 ); $row2 = @mysql_fetch_array( $result2 );
if ("<script type='text/javascript'>
</script>" == $row2[USERNAME]) {
} else {
echo "{ alert ( 'The recipient username you have enterd doesnt exist!' ); valid = false; }";

But where i have put...

<script type='text/javascript'>

Its not searching the database for the reciepient username that the person tryed in, its saying that the whole javascript above is the username.

How can i get it so that it puts in what the person typed in the recipient field into the...



08-06-2007, 12:05 AM
Don't mix JS into php. It doesn't work.

Read up on POST values. This is where you'll find the data you want to process.

$SQL2 = "SELECT * from USERS WHERE USERNAME = '".$_POST['recipient']."'";

Ref: http://www.php.net/manual/en/reserved.variables.php#reserved.variables.post

08-06-2007, 12:51 AM
Yes, but i wanted it to go and check if the user existed, live, and if it doesnt then it would display an alert message.

But, to be honest i could do it another way.

I could let the form submit, and then if the user doesnt exist, then i could load the data back into the form fields.



08-06-2007, 01:04 AM
Since PHP is executed server side, the php code you have in the page you posted is executed when that page is requested by the browser.

To do what you want, verify information entered in a form against information located on the server, without refreshing the page, you must use AJAX techniques - http://w3schools.com/ajax/default.asp (replace any references to .asp as the server side scripting at that link with your equivalent .php code.)

08-06-2007, 08:33 AM
You can't mix up JavaScript & PHP, what you can do is use Ajax, or provide JS with a array of valid users!

08-06-2007, 09:23 AM
Yes, AJAX is the way to go if you are looking for live responses. It is not nearly as scary as it sounds. Basically you want one PHP script that takes the username as a variabe (GET is easiest) all it will output is true/false or 1/0 etc. You call this page from within your Javascript (see the link above fore more details on exact call) and then display the error or move on as you would like to.

Its becoming very popular with username checking to give instant feedback.

"Insanity is hereditary - you get it from your children." Sam Levenson
Web Development Company (http://www.emblemsoftware.com) Projects (Download professional web icons for your site (http://www.imagetrail.net/find_web_icons_photos_2.php) Compare stock image agencies (http://www.microstockforum.com/forums))

08-06-2007, 01:21 PM
Thanks guys, but i have it all sorted out now.