Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-11-2012, 04:35 PM   PM User | #1
hans_cellc
New Coder

 
Join Date: Oct 2011
Posts: 92
Thanks: 38
Thanked 0 Times in 0 Posts
hans_cellc is an unknown quantity at this point
Echo Javascript If statements

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.

Code:
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>");
 }
hans_cellc is offline   Reply With Quote
Old 10-11-2012, 04:55 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,645
Thanks: 4
Thanked 2,450 Times in 2,419 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
How do you create the $count? If will only be true if the $count is 1.
Fou-Lu is offline   Reply With Quote
Old 10-11-2012, 06:22 PM   PM User | #3
hans_cellc
New Coder

 
Join Date: Oct 2011
Posts: 92
Thanks: 38
Thanked 0 Times in 0 Posts
hans_cellc is an unknown quantity at this point
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.
hans_cellc is offline   Reply With Quote
Old 10-11-2012, 06:33 PM   PM User | #4
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,645
Thanks: 4
Thanked 2,450 Times in 2,419 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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:
Code:
<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:
Code:
<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.
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
hans_cellc (10-11-2012)
Old 10-11-2012, 06:58 PM   PM User | #5
hans_cellc
New Coder

 
Join Date: Oct 2011
Posts: 92
Thanks: 38
Thanked 0 Times in 0 Posts
hans_cellc is an unknown quantity at this point
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)

Code:
<?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>";
 }
 
?>
hans_cellc is offline   Reply With Quote
Old 10-11-2012, 07:38 PM   PM User | #6
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,645
Thanks: 4
Thanked 2,450 Times in 2,419 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
And what is your output source? If it includes the appropriate script element, this is not a PHP issue.

Edit:
Wait a second.
Quote:
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.

Last edited by Fou-Lu; 10-11-2012 at 07:41 PM..
Fou-Lu is offline   Reply With Quote
Old 10-11-2012, 07:49 PM   PM User | #7
hans_cellc
New Coder

 
Join Date: Oct 2011
Posts: 92
Thanks: 38
Thanked 0 Times in 0 Posts
hans_cellc is an unknown quantity at this point
Ok I got the username to display with the following code change:

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>)";
 	}
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 is offline   Reply With Quote
Old 10-11-2012, 07:58 PM   PM User | #8
hans_cellc
New Coder

 
Join Date: Oct 2011
Posts: 92
Thanks: 38
Thanked 0 Times in 0 Posts
hans_cellc is an unknown quantity at this point
GOOGLE RULES, I found it and here the working snippet of code.

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>)";
 }
hans_cellc is offline   Reply With Quote
Old 10-11-2012, 08:21 PM   PM User | #9
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,645
Thanks: 4
Thanked 2,450 Times in 2,419 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
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'];
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
hans_cellc (10-11-2012)
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:54 AM.


Advertisement
Log in to turn off these ads.