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 01-06-2013, 08:52 PM   PM User | #1
Jian0203
New Coder

 
Join Date: Mar 2012
Posts: 47
Thanks: 2
Thanked 0 Times in 0 Posts
Jian0203 is an unknown quantity at this point
Pass variable from page to page

Can anyone please tell me how should i pass the variable from login.php to display.php ? In my system, when users input their username, their display image will be shown in next page. but i just can't figure out how do i pass the input username from login.php to display.php although i read so many online solving. Can anyone please help or give me some hints perhaps ? Thank you so much for spending time to help me ^^

login.php
PHP Code:
    <?php
    
include("connection.php");
        
        if (isset(
$_POST["submitbtn"]))
    {    
        
        
$name=$_POST["username"];
        
        
$result=mysql_query("SELECT * FROM customerdetail WHERE customer_username = '$name' ");
        
        
$num_row mysql_num_rows($result);
        
        if(
$num_row==1)
        {
            
$row=mysql_fetch_assoc($result);
            
            
$checkname $row["customer_username"];
            
            if(
$name == $checkname)
            {
                
$_SESSION["CID"] = $checkname;
    
?>
            
                <script type="text/javascript">
                
                alert("User ID is correct!! Proceed to password Verification!!");
                
                window.location = "display.php";
                </script>
                
                
    <?php
            
}
                else
                {
    
?>
                
                    <script type="text/javascript">
                
                alert("Login UnsuccessfuL!! Please Try again");
                
                window.location = "login.php";
                </script>
                
                
    <?php
                    
}
                    }
            else
                {
    
?>
                <script type="text/javascript">
                
                alert("Login UnsuccessfuL!! Please Try again");
                
                window.location = "login.php";
                
                </script>
    <?php
                    
}
                }
    
?>            
    <body>
        
        <div id="wrapper">
            
            <div id="header">
                
            </div>
            
            <div id="content">
                
                <h1>Please Enter Your Username: </h1>
    <p>
    <form name = "frm1" method="post">
    <input type="text" name="username">
    </p>
    <p>
    <input type="submit" name="submitbtn" value="Login!">
    </form>
    </p>
            
                </div>    
        </div>
    </body>
    </html>
display.php
PHP Code:
    <?php
        
include("connection.php");
        
$name=$_POST["username"];
        if(!isset(
$name) || empty($name)){
    die(
"Please select your image!");
        }else{
        
$query mysql_query("SELECT * FROM customerdetail WHERE customer_username='$name'");
        
$row mysql_fetch_array($query);
        
$content $row["customer_face"];
        
header('Content-type: image/jpg');
    echo 
$content;
    }
    
?>
Error:

Code:
    Notice: Undefined index: username in C:\xampp\htdocs\MyProject3\display.php on line 5
Jian0203 is offline   Reply With Quote
Old 01-06-2013, 09:03 PM   PM User | #2
Jian0203
New Coder

 
Join Date: Mar 2012
Posts: 47
Thanks: 2
Thanked 0 Times in 0 Posts
Jian0203 is an unknown quantity at this point
i know i should use $_SESSION and $_POST , $_GET, $_REQUEST for cookie and so on, but i just can't figure it out how it actually works ~
Jian0203 is offline   Reply With Quote
Old 01-06-2013, 09:17 PM   PM User | #3
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
PHP Code:
        $name=$_POST["username"]; 
        if(!isset(
$name) || empty($name)){ 
    die(
"Please select your image!"); 
        }else{ 
You can't refer to $_POST['username'] if it isn't set.

PHP Code:
        if (!isset($name) || empty($name)) { 
            die(
"Please select your image!"); 
        } else {
            
// now we can refer to it..
            
$name $_POST["username"]; 
You could pass the value as a get, rather than post, value - although not recommended!

PHP Code:
window.location "display.php?name=$name";   // then refer to it in display.php..
if (isset($_GET['name']) // etc.. 
But you've already stored it as a session variable, so you can check and retrieve this value:

PHP Code:
if (isset($_SESSION["CID"])) { 
BTW Use of the mysql library is deprecated/discouraged, although nobody seems to be getting this message .

IF you are not sure, check the docs, including the comments.
__________________
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
Validate your HTML and CSS

Last edited by AndrewGSW; 01-06-2013 at 09:24 PM..
AndrewGSW is offline   Reply With Quote
Old 01-06-2013, 09:45 PM   PM User | #4
Jian0203
New Coder

 
Join Date: Mar 2012
Posts: 47
Thanks: 2
Thanked 0 Times in 0 Posts
Jian0203 is an unknown quantity at this point
Quote:
Originally Posted by AndrewGSW View Post
PHP Code:
        $name=$_POST["username"]; 
        if(!isset(
$name) || empty($name)){ 
    die(
"Please select your image!"); 
        }else{ 
You can't refer to $_POST['username'] if it isn't set.

PHP Code:
        if (!isset($name) || empty($name)) { 
            die(
"Please select your image!"); 
        } else {
            
// now we can refer to it..
            
$name $_POST["username"]; 
You could pass the value as a get, rather than post, value - although not recommended!

PHP Code:
window.location "display.php?name=$name";   // then refer to it in display.php..
if (isset($_GET['name']) // etc.. 
But you've already stored it as a session variable, so you can check and retrieve this value:

PHP Code:
if (isset($_SESSION["CID"])) { 
BTW Use of the mysql library is deprecated/discouraged, although nobody seems to be getting this message .

IF you are not sure, check the docs, including the comments.

Thank you so much for helping. It finally pass the variable. but, there is another problem which the image just doesn't get displayed.

Is it because my query error ? or this line got error ?
PHP Code:
header('Content-type: image/jpg'
but when i change "image/jpg" to "customer/jpg", it started to download the display.php... so i think it's not the reason that the picture doesnt display. It just shows a icon which the picture is tease into half mean that cant be displayed.

I tried the sample code below, it can work but not with my code...

PHP Code:
 $storedid "3";

  
$id = (int)$storedid;


if(!isset(
$id) || empty($id)){

die(
"Please select your image!");

}else{


$query mysql_query("SELECT * FROM tbl_images WHERE id='$id'");
$row mysql_fetch_array($query);
$content $row['image'];

header('Content-type: image/jpg');
         echo 
$content;

Jian0203 is offline   Reply With Quote
Old 01-06-2013, 10:12 PM   PM User | #5
AndrewGSW
Senior Coder

 
Join Date: Apr 2011
Location: London, England
Posts: 2,120
Thanks: 15
Thanked 354 Times in 353 Posts
AndrewGSW will become famous soon enough
I haven't done this before myself, but is the image stored as a BLOB in the database? Is the MIME-type (image/jpg) correct? Try image/jpeg - this is what I should be I believe.

Quote:
but when i change "image/jpg" to "customer/jpg"
customer/jpg won't work; there are only specific MIME-types that you can use, you can't make them up .
__________________
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
Validate your HTML and CSS

Last edited by AndrewGSW; 01-06-2013 at 10:14 PM..
AndrewGSW is offline   Reply With Quote
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 12:12 AM.


Advertisement
Log in to turn off these ads.