Jump to content

Session variable either not being set or not being retrieved.


Recommended Posts

The first page is used to submit data to second page (and also show errors if there are any with the data submitted). The second page should be retrieving the posted data and assigning that data to session should it need to refresh back to the first page to show errors. The session data is used to repopulate the form. Right now I'm typing test into the review_name variable and when the page redirects back to the form to display the error messages, that field is not populated with "test".

 

$product_id=$_GET['product'];
session_start();
$error=$_SESSION['error'];
$content.='<div class="product_information_text review_form">
<div class="review_header">Write a Review for '.$product_name.'</div>
<form action="./review_process.php?product='.$product_id.'&p=php" method="POST">
<p class="form_item"><label>Name:</label> <input type="text" name="review_name" size="30"';
if(isset($_SESSION['review_name'])){$content.=' value="'.$_SESSION['review_name'].'"';}
$content.=' />';
if($error[0]=="1"){$content.=' <span class="red">This field is required.</span>';}
$content.='
</p>
<p class="form_item"><label>E-Mail:</label> <input type="text" name="review_email" size="30"';
if(isset($_SESSION['review_email'])){$content.=' value="'.$_SESSION['review_email'].'"';}
$content.=' />';
if($error[2]=="1"){$content.=' <span class="red">This field is required.</span>';}
$content.='
</p>
<p class="form_item"><label>Location:</label> <input type="text" name="review_location" size="30"';
if(isset($_SESSION['review_location'])){$content.=' value="'.$_SESSION['review_location'].'"';}
$content.=' />';
if($error[3]=="1"){$content.=' <span class="red">This field is required.</span>';}
$content.='
</p>
<p class="form_item"><label>Describe Yourself:</label> <input type="text" name="review_describe" size="30"';
if(isset($_SESSION['review_describe'])){$content.=' value="'.$_SESSION['review_describe'].'"';}
$content.=' />';
if($error[4]=="1"){$content.=' <span class="red">This field is required.</span>';}
$content.='
</p>
<p class="form_item"><label>Review Title:</label> <input type="text" name="review_title" size="30"';
if(isset($_SESSION['review_title'])){$content.=' value="'.$_SESSION['review_title'].'"';}
$content.=' />';
if($error[1]=="1"){$content.=' <span class="red">This field is required.</span>';}
$content.='
</p>
<p class="form_item"><label>Best Use of Product:</label> <input type="text" name="review_best_use" size="30"';
if(isset($_SESSION['review_best_use'])){$content.=' value="'.$_SESSION['review_best_use'].'"';}
$content.=' />';
if($error[5]=="1"){$content.=' <span class="red">This field is required.</span>';}
$content.='
</p>
<p class="form_item"><label>Product Pros:</label> <input type="text" name="review_pros" size="30"';
if(isset($_SESSION['review_pros'])){$content.=' value="'.$_SESSION['review_pros'].'"';}
$content.=' />';
if($error[6]=="1"){$content.=' <span class="red">This field is required.</span>';}
$content.='
</p>
<p class="form_item"><label>Product Cons:</label> <input type="text" name="review_cons" size="30"';
if(isset($_SESSION['review_cons'])){$content.=' value="'.$_SESSION['review_cons'].'"';}
$content.=' />';
if($error[7]=="1"){$content.=' <span class="red">This field is required.</span>';}
$content.='
</p>
<p class="form_item"><label>Product Rating:</label><br />
<div class="rating_radio"><input type="radio" name="review_product_rating" value="1"';
if(isset($_SESSION['review_product_rating']) && $_SESSION['review_product_rating']=="1"){$content.='checked';}
$content.=' /> <br />1</div>
<div class="rating_radio"><input type="radio" name="review_product_rating" value="2"';
if(isset($_SESSION['review_product_rating']) && $_SESSION['review_product_rating']=="2"){$content.='checked';}
$content.=' /> <br />2</div>
<div class="rating_radio"><input type="radio" name="review_product_rating" value="3"';
if(isset($_SESSION['review_product_rating']) && $_SESSION['review_product_rating']=="3" || !isset($_SESSION['review_product_rating'])){$content.='checked';}
$content.=' /> <br />3</div>
<div class="rating_radio"><input type="radio" name="review_product_rating" value="4"';
if(isset($_SESSION['review_product_rating']) && $_SESSION['review_product_rating']=="4"){$content.='checked';}
$content.=' /> <br />4</div>
<div class="rating_radio"><input type="radio" name="review_product_rating" value="5"';
if(isset($_SESSION['review_product_rating']) && $_SESSION['review_product_rating']=="5"){$content.='checked';}
$content.=' /> <br />5</div>
<div class="worst">(Worst)</div><div class="best">(Best)</div>
</p>
<p> </p>
<p class="form_item"><label>Comments on Product:';
if($error[7]=="1"){$content.=' <span class="red">This field is required.</span>';}
$content.='
</label><br />
<textarea name="review_text" rows="10" cols="60">';
if(isset($_SESSION['review_text'])){$content.=$_SESSION['review_text'];}
$content.='</textarea>
</p>
<p><input type="submit" value="Submit" name="Submit" /></p>
</form>
</div>
';
session_unset();
session_destroy();

 

session_start();

$product_id=$_GET['product'];

$review_name=$_POST['review_name'];
$_SESSION['review_name']==$review_name;
$review_name = stripslashes($review_name);
$review_name = mysql_real_escape_string($review_name);
if($review_name==""){
$error0=1;
}
else{
$error0=0;
}

$review_title=$_POST['review_title'];
$_SESSION['review_title']==$review_title;
$review_title = stripslashes($review_title);
$review_title = mysql_real_escape_string($review_title);
if($review_title==""){
$error1=1;
}
else{
$error1=0;
}

$review_email=$_POST['review_email'];
$_SESSION['review_email']==$review_email;
$review_email = stripslashes($review_email);
$review_email = mysql_real_escape_string($review_email);
if($review_email==""){
$error2=1;
}
else{
$error2=0;
}

$review_location=$_POST['review_location'];
$_SESSION['review_location']==$review_location;
$review_location = stripslashes($review_location);
$review_location = mysql_real_escape_string($review_location);
if($review_location==""){
$error3=1;
}
else{
$error3=0;
}

$review_describe=$_POST['review_describe'];
$_SESSION['review_describe']==$review_describe;
$review_describe = stripslashes($review_describe);
$review_describe = mysql_real_escape_string($review_describe);
if($review_describe==""){
$error4=1;
}
else{
$error4=0;
}

$review_best_use=$_POST['review_best_use'];
$_SESSION['review_best_use']==$review_best_use;
$review_best_use = stripslashes($review_best_use);
$review_best_use = mysql_real_escape_string($review_best_use);
if($review_best_use==""){
$error5=1;
}
else{
$error5=0;
}

$review_pros=$_POST['review_pros'];
$_SESSION['review_pros']==$review_pros;
$review_pros = stripslashes($review_pros);
$review_pros = mysql_real_escape_string($review_pros);
if($review_pros==""){
$error6=1;
}
else{
$error6=0;
}

$review_cons=$_POST['review_cons'];
$_SESSION['review_cons']==$review_cons;
$review_cons = stripslashes($review_cons);
$review_cons = mysql_real_escape_string($review_cons);
if($review_cons==""){
$error7=1;
}
else{
$error7=0;
}

$review_product_rating=$_POST['review_product_rating'];
$_SESSION['review_product_rating']=$review_product_rating;
$review_product_rating = stripslashes($review_product_rating);
$review_product_rating = mysql_real_escape_string($review_product_rating);

$review_text=$_POST['review_text'];
$_SESSION['review_text']==$review_text;
$review_text = stripslashes($review_text);
$review_text = mysql_real_escape_string($review_text);
if($review_text==""){
$error8=1;
}
else{
$error8=0;
}

$review_show="n";

date_default_timezone_set('US/Eastern');
$review_date = date("F j, Y, g:i a T");

$error="".$error0."".$error1."".$error2."".$error3."".$error4."".$error5."".$error6."".$error7."".$error8."";


if($_GET['p']=="php"){
if($error!=="000000000"){
$_SESSION['error']=$error;
//header("Location: ./store.php?product=".$product_id."&write=review");
echo $_SESSION['review_name'];
}
else{
$sql="INSERT INTO $tbl_name3 (product_id, review_show, review_title, review_email, review_name, review_location, review_date, review_describe, review_best_use, review_pros, review_cons, review_product_rating, review_text) VALUES ('$product_id', '$review_show', '$review_title', '$review_email', '$review_name', '$review_location', '$review_date', '$review_describe', '$review_best_use', '$review_pros', '$review_cons', '$review_product_rating', '$review_text')";
mysql_query($sql);
header("Location: ./store.php?product=".$product_id."&reviews=thankyou");
}
}

On this second page echo $_SESSION['review_name']; returns nothing, when changed to $_SESSION['review_product_rating']; it returns the rating I selected in the form. I'm probably missing something obvious here.

Link to comment
Share on other sites

What does browsing to a .php script with a phpinfo(); statement in it show for the register_globals setting? -

 

<?php
phpinfo();
?>

 

turned off

 

looking further down on the report it says _POST['review_name'] value=no value under PHP Variables. So is it not even getting data from the form?

Link to comment
Share on other sites

Upon further review of your code, you are using two == signs in your code that is trying to set the $_SESSION variables -

 

$_SESSION['review_name']==$review_name;

 

^^^ That should only be one = sign (an assignment operator.) Two equal signs is a comparison operator.

 

Are you developing and debugging your code on a system with error_reporting set to E_ALL and display_errors set to ON so that all the php detected errors will be reported and displayed. That is how I found the problem (the session index name is not defined, but the line of code was supposed to be setting the value, not referencing the value.)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.