CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   PHP (http://www.codingforums.com/forumdisplay.php?f=6)
-   -   Pass variable from page to page (http://www.codingforums.com/showthread.php?t=285375)

Jian0203 01-06-2013 08:52 PM

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 01-06-2013 09:03 PM

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 ~

AndrewGSW 01-06-2013 09:17 PM

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.

Jian0203 01-06-2013 09:45 PM

Quote:

Originally Posted by AndrewGSW (Post 1304477)
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;



AndrewGSW 01-06-2013 10:12 PM

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 :).


All times are GMT +1. The time now is 10:35 AM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.