Jump to content

[SOLVED] How to Remember Value of Combo Box?


webref.eu

Recommended Posts

Hi All

 

I am developing a Reviews script where the user gets to choose a Rating Out of 10 via a Dropdown Box as follows: 

 

<select name="ReviewRating" id="ReviewRating">
  <option>1</option>
  <option>2</option>
  <option>3</option>
  <option>4</option>
  <option>5</option>
  <option>6</option>
  <option>7</option>
  <option>8</option>
  <option>9</option>
  <option>10</option>
  </select>

 

The script posts back to itself and currently I am then losing the user selected value of this dropdown.  So, how do I make the script remember the rating after postback?  Please let me know or point me to a tutorial. 

 

Many thanks all.

 

OK, here is the form in its entirety, sorry about all the commented code and everything but it's still under development.  As you can see it's posting back on itself. 

 

Rgds

 

<html>
<head>
<title>Add a Review</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link href="styles.css" rel="stylesheet" type="text/css" />
</head>

<body>

<?php
include('inc-dbconnect.php');
//shared functions
include("inc-functions.php");





//NEED TO CLEANSE QUERYSTRING

//get ProductId
$ProductId = $_GET['ProductId'];



//check for cookie or session, or redirect to login page







//If processform flag is set, processing can occur.  If not set, form is being loaded for the first time processing
if ($_POST['processform'] == 1) {

//Process Form
//retrieve form input
$ProductId=$_POST['txtProductId']; 
$ReviewRating=$_POST['ReviewRating'];  
$ReviewDesc=$_POST['txtReviewDesc'];

//for debugging
//echo "ReviewDesc After Post: " . $ReviewDesc . "<br><br>";
//echo "ReviewDesc Length: " . strlen($ReviewDesc) . "<br><br>";

//make safe with htmlspecialchars, will now contain the ascii equivalents to all html chars like <
//$ReviewDesc=htmlspecialchars($ReviewDesc); 

//echo "First character of Review Desc: " . $ReviewDesc[0] . "<br><br>";

//Y Year in four digits
//m Month 01 to 12
//d Day of the month 01 to 31
//H Hour 00 to 23
//i Minutes 00 to 59
//s Seconds 00 to 59
$DateAndTime=date("Y-m-d H:i:s");
//for debugging
//echo $DateAndTime . "<br>";






//Validate fields


// create empty error variable
$ErrorMsg = "";



if (!$ReviewDesc) {
$ErrorMsg = "Please give Your Review.<br>";
}




//check Review is 10 characters or more
//PrepareForForm only actioned under error conditions so does not affect database input
//need to use mb_strlen to cope with multibyte characters like £
if(mb_strlen(PrepareForLengthCheck($ReviewDesc)) < 10) {
$ErrorMsg = $ErrorMsg . "Your Review must be 10 characters or more, please make it longer.<br>";
$ReviewDesc = PrepareForForm($ReviewDesc);
} 



//check Review is not too long
if(mb_strlen(PrepareForLengthCheck($ReviewDesc)) > 15000) {
$ErrorMsg = $ErrorMsg . "Your Review is limited to 15,000 characters so please make it shorter.<br>";
//need to use PrepareForForm to account for magic quotes when putting back into form
//note, correction for magic quotes is therefore dependent on this error being triggered
$ReviewDesc = PrepareForForm($ReviewDesc);
} 




echo "<p class='txterror'>" . $ErrorMsg . "</p>";





//If no errors write to database
If ($ErrorMsg == "") {


//for debugging
//echo "Review Description Before Database Insert: " . $ReviewDesc . "<br><br>";
  


//insert into database
$query = "INSERT INTO Reviews (ProductId, ReviewRating, ReviewDesc, ReviewDate, ReviewIsApproved) VALUES ('" . 
makeSQLSafe($ProductId) . "', '" . 
makeSQLSafe($ReviewRating) . "', '" . 
makeSQLSafe($ReviewDesc) . "', '" . 
makeSQLSafe($DateAndTime) . "', '" . 
makeSQLSafe(0) . "')"; 


mysql_query($query); 


mysql_close(); 

echo "Thank you, your review has been added and is now awaiting approval by our editors.";

//Close If No Errors
}


//Close If processform flag is set
}

?>

<br><br>
<a href="product-page.php">Product Page</a> 




<?php
//If condition for when to display form
//when processform flag is 1 and ErrorMsg is empty we don't want to display the form
//otherwise we display the form for error correction
If (!($_POST['processform'] == 1 && $ErrorMsg == "")) {
?>



<!-- 
- Form gets posted back to itself.
- If there are errors, form will get parsed again, but with updated variables in the fields.
-->

<form name="form1" method="post" action="<? echo $_SERVER['PHP_SELF']; ?>">
  
  <p>Please note all reviews submitted are subject to approval by our editors.
    We want to approve as many of your reviews as possible, but please adhere
    to our guidelines. The  decision of our editors is final and no correspondence
    will be entered into. </p>
  <ul>
    <li>Advertising in any form is not permitted.</li>
    <li>Please keep your review specific to the item being reviewed. </li>
    <li>Reviews must be your own original work and cannot be copied from elsewhere. </li>
    <li>Do not make remarks that insult other reviewers.</li>
    <li>Do not post content which is blasphemous, racist, insulting or hateful.</li>
    <li>Do not use swear words or sexually explicit language.</li>
    <li>Do not post any contact details such as telephone numbers, postal addresses,
      e-mail addresses or URLs.</li>
    <li>Please note your review length is limited to 15,000 characters. </li>
  </ul>
  <p>Thank you for your co-operation.</p>
    Your Rating out of 10:<br>

  
  <select name="ReviewRating" id="ReviewRating">
  <option>1</option>
  <option>2</option>
  <option>3</option>
  <option>4</option>
  <option>5</option>
  <option>6</option>
  <option>7</option>
  <option>8</option>
  <option>9</option>
  <option>10</option>
  </select>

  
  <br>
  <br>

<p>  
<?php
//debugging
//echo "Review Description: " . $ReviewDesc . "<br><br>";
?>  
</p>
  
  Your Review:<br>
  
  <textarea name="txtReviewDesc" cols="80" rows="10"><?print $ReviewDesc; ?></textarea>
  <!--
  <input type="text" name="txtReviewDesc" size="20" maxlength="20" value="<?print $ReviewDesc; ?>" />
   -->
  <br><br>
  <input type="hidden" name="txtProductId" value="<?=$ProductId?>">
  
  <input type="hidden" name="processform" value="1">
  
  <input type="submit" name="Submit" value="Submit">

</form>

<a href="control-panel.php">Home</a> 


<?php
//End If condition for when to display form
}
?> 

</body>
</html>

 

This should work (swap it with the code in your first post):

 

  <select name="ReviewRating" id="ReviewRating">
  <?php
  $rating = (isset($ReviewRating)) ? $ReviewRating : '';
  for ($i = 1; $i <= 10; $i++) {
    $selected = ($rating == $i) ? ' selected="selected"' : '';
    echo "<option$selected>$i</option>\n";
  }
  ?>  </select>

 

It loops 1 through 10, and adds the HTML attribute selected to the option tag, if the rating matches the number being printed.

Archived

This topic is now archived and is closed to further replies.

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