...

View Full Version : Echo Javascript If statements



hans_cellc
10-11-2012, 04:35 PM
I have the following code that pops up an alert and confirm window depending on its condition. The if statement does not seem to work.

Also I want the username to show in the pop up window but it does not.

Please help.


if($count == 1) {
// Register $myusername and redirect to file "login_success.php"
// Set username session variable
$_SESSION['myusername'] = $_POST['myusername'];
echo ("<script type='text/javascript'>
window.alert('Welcome: '.$_SESSION['myusername']. ' you are successfully logged in.')
window.location.href='securepage.php';
</script>");
}
else {
echo ("<script type='text/javascript'>
if (window.confirm('Username or Password INCORRECT! Do you wish to REGISTER?')
window.location.href='register.php'; // If OK pressed
else
window.location.href='index.php'; // If Cancel pressed
</script>");
}

Fou-Lu
10-11-2012, 04:55 PM
How do you create the $count? If will only be true if the $count is 1.

hans_cellc
10-11-2012, 06:22 PM
Yes count is a boolean that indicates a username and password is extracted from the DB.
That works I have a code that work but not with the extras like:
1) The welcome username
2) The popup window with option to register.

All it does it says plainly you are logged in and just redirects to index.php.

Fou-Lu
10-11-2012, 06:33 PM
This now has me confused. So you are getting into the if branch or no?
Check your source HTML code. If it shows the script tags as expected, than this is not a PHP issue.
So when count is 1, you should see this in the HTML source:


<script type='text/javascript'>
window.alert('Welcome: ' . yourusername . ' you are successfully logged in.')
window.location.href='securepage.php';
</script>

And this when count is not 1:


<script type='text/javascript'>
if (window.confirm('Username or Password INCORRECT! Do you wish to REGISTER?')
window.location.href='register.php'; // If OK pressed
else
window.location.href='index.php'; // If Cancel pressed
</script>


Do you see both of these properly when you control the $count? If that is in the source, than PHP has done its job. It's up to the browser to do something with the JS code.

As for the username not showing up in the alert, that's because your JS code is broken. You have used single quotations and then used single within it. Remove those single quotations within and wrap the session access into {} to force complex evaluation. Or break the double quotes, whichever you like.

hans_cellc
10-11-2012, 06:58 PM
Here is my complete code.

All I want to change is the Welcome username and the window.confirm should have the conditional to go to a register page.

This code works but I would like to add the frills.

The problem with the if statement is that it is a javascript if to run in a echo (PHP)


<?PHP
// Inialize session
session_start();

// Import the connect to DB code as required as we need it and can not continue with out it.
require 'connect_db.php';

?>

<!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>
<title>Logging in - 48323934 - ICT 2613</title>
<meta name="Description" content="PHP ICT2613 Exam Assignment. 48323934 - Logging in " />
<meta http-equiv="content-language" content="en-za" />
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<meta name="Author" content="S.J. Fouche, Student number: 48323934" />
<meta name="HandheldFriendly" content="True" />
<!-- Link to the external style sheet (CSS) -->
<link rel="stylesheet" type="text/css" href="my_css.css" />
<!-- Link to the external javascript file -->
<script type="text/javascript" src="my_javascript.js"></script>

</head>

<body>
<!-- Used the include feature for the header and links on all pages -->
<?PHP require "header.txt" ?>

<!-- Links -->
<table width="100%" style = "margin-top: -25px;">
<tr align="center"><td ><h3 style = "margin-top: -5px; margin-bottom: 1px;"><a href="http://127.0.0.1/exam/index.php" name="top">Home Page</a> | <a href="http://127.0.0.1/exam/register.php">Register</a> |
<a href="#" onclick="showDiv()">Login</a> | <a href="http://127.0.0.1/exam/insert.php">Insert</a> | <a href="http://127.0.0.1/exam/list.php">List</a></h3></td></tr></table>

<h2 style="color: yellow; font-family: Verdana, Arial, Helvetica, sans-serif;" align="center">Members Only</h2>

<!-- Setup image -->
<table width="640" height="350" align="center" cellpadding="0" cellspacing="1">
<tr align="center"><td><img src="site_graphics/controls.jpg" alt="Control Background" width="640" height="330" /></td></tr>
</table>

<!-- PHP Coding -->
<?PHP
// Code below will create Code link at the center and bottom of the page
echo("<p style='position:absolute; bottom:10px; left:50%;'><a href='javascript: codepopup(this)'>Code</a></p>")
?>

</body>
</html>

<?PHP
// username and password sent from form
$myusername=$_POST['myusername'];
$mypassword=$_POST['mypassword'];

// To protect MySQL injection
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);

// Table name variable
$tb_name = "tb_user";

// Set the Query within a variable
$query = "SELECT * FROM $tb_name WHERE username='$myusername' AND password='$mypassword'";
$result = mysql_query($query);

// Mysql_num_row is counting table row
$count = mysql_num_rows($result);

// Create a if statement to check if results returned and if not to display an error
if($count == 1) {
// Register $myusername and redirect to file "login_success.php"
// Set username session variable
$_SESSION['myusername'] = $_POST['myusername'];
echo ("<script type='text/javascript'>
window.alert('You are successfully logged in.')
window.location.href='securepage.php';
</script>");
}
else {
echo "(<script type='text/javascript'>
window.alert('Username or Password INCORRECT!')
window.location.href='index.php';
</script>";
}

?>

Fou-Lu
10-11-2012, 07:38 PM
And what is your output source? If it includes the appropriate script element, this is not a PHP issue.


Wait a second.


The problem with the if statement is that it is a javascript if to run in a echo (PHP)

No, no its not. It's a PHP if, not a JS if. If its intended to be a JS if, you must add that if to a part of the output script and print it out with the rest. PHP and JS are not compatible together and cannot share functionality. JS is nothing more than text output to PHP, and JS is a client language and therefore cannot execute server commands. This is why we have things like AJAX to do background requests.

hans_cellc
10-11-2012, 07:49 PM
Ok I got the username to display with the following code change:


if($count == 1) {
// Register $myusername and redirect to file "login_success.php"
// Set username session variable
$_SESSION['myusername'] = $_POST['myusername'];
$username = $_SESSION['myusername'];
echo "(<script type='text/javascript'>
window.alert(\"Welcome $username you are successfully logged in.\")
window.location.href='securepage.php';
</script>)";
}

Notice I escaped the "" to have a variable read inside.

Now to get the if statement block to work with PHP there must be a way and I am googling like crazy.

hans_cellc
10-11-2012, 07:58 PM
GOOGLE RULES, I found it and here the working snippet of code.


if($count == 1) {
// Register $myusername and redirect to file "login_success.php"
// Set username session variable
$_SESSION['myusername'] = $_POST['myusername'];
$username = $_SESSION['myusername'];
echo "(<script type='text/javascript'>
window.alert(\"Welcome $username you are successfully logged in.\")
window.location.href='securepage.php';
</script>)";
}
else {
echo "(<script type='text/javascript'>
if (window.confirm('Username or Password INCORRECT!'))
window.location.href='register.php';
else
window.location.href='index.php';
</script>)";
}

Fou-Lu
10-11-2012, 08:21 PM
Right, so your Javascript was broken yeah?

You can save the assignment to one line if you want. I don't see a need to extract the $username variable at all, but you could do it in one assignment with $username = $_SESSION['myusername'] = $_POST['myusername'];



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum