Jump to content

PHP if statement with javascript


BrianM

Recommended Posts

Can this be done --

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<link rel="stylesheet" type="text/css" href="setup.css" />
<script type="text/javascript">
function incomplete_field()
{
alert("You must complete all fields!");
}
</script>
<title>Setup</title>
</head>
<?php
if (isset($_POST['finish'])) {

// basic setup variables

$site_name = $_POST['site_name'];
$site_url = $_POST['site_url'];
$administration_username = $_POST['administration_username'];
$administration_password = $_POST['administration_password'];

// mysql setup variables

$mysql_server_name = $_POST['mysql_server_name'];
$mysql_username = $_POST['mysql_username'];
$mysql_password = $_POST['mysql_password'];
$mysql_database_name = $_POST['mysql_database_name'];
$mysql_table_prefix = $_POST['mysql_table_prefix'];

if (!$_POST['site_name'] | !$_POST['site_url'] | !$_POST['administration_username'] | !$_POST['administration_password'] | !$_POST['mysql_server_name'] | !$_POST['mysql_username'] | !$_POST['mysql_password'] | !$_POST['mysql_database_name'] | !$_POST['mysql_table_prefix']) {
function incomplete_field()
} else {

$mysql_connect = mysql_connect("$mysql_server_name", "$mysql_username", "$mysql_password") or die(mysql_error());
}
}
?>

 

The Javascript is there in the head. I'm wanting to use an alert box when any field is left blank. I get this error:

 

Parse error: parse error, expecting `'{'' in C:\Program Files\Apache Group\Apache2\htdocs\mps\setup\setup.php on line 34

 

And line 34 is - } else {

Link to comment
https://forums.phpfreaks.com/topic/109115-php-if-statement-with-javascript/
Share on other sites

you can't directly call a javascript function from php. however you could use php to create a body onload event to run the script.

 

example:

 

if (!$_POST['site_name'] || !$_POST['site_url'] || !$_POST['administration_username'] || !$_POST['administration_password'] || !$_POST['mysql_server_name'] || !$_POST['mysql_username'] || !$_POST['mysql_password'] || !$_POST['mysql_database_name'] || !$_POST['mysql_table_prefix']) {
echo "<body onload=\"incomplete_field()\">"; //tells the browser to run incomplete_field() when the page loads
} else {

$mysql_connect = mysql_connect("$mysql_server_name", "$mysql_username", "$mysql_password") or die(mysql_error());
}

 

i changed your | to || (the correct or in php)

 

 

i changed your | to || (the correct or in php)

 

That's not the correct OR, that's the logical OR.  The other one that he had is a bitwise OR.  Although you'd be correct in saying that a majority of the time you'd use || or &&, you don't when you actually need to work with bitwise operators.  EX:

 

if (imagetypes() & IMG_PNG) {

echo "PNG enabled".

}

 

That's not the correct OR, that's the logical OR.  The other one that he had is a bitwise OR.  Although you'd be correct in saying that a majority of the time you'd use || or &&, you don't when you actually need to work with bitwise operators.  EX:

 

oh sorry i didn't know that || has always worked fine for me :)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.