Reputation: 9
I have a radio box and 2 drop down menus which when submitted save to mysql. The radio box is either yes or no and the 2 dropdowns where created in html. It is currently fully working and saves all the data.
What I now wish to do is when a user logs back in, it will show what they have previously selected (if they have).
PHP SCRIPT:
<?php
session_start();
require_once("config.php");
if(!isset($_SESSION['username'])){
header('Location: login.php');
exit;
}else{
$sql = "SELECT attendance1 FROM user WHERE username = '".mysql_real_escape_string($_SESSION['username'])."'";
$res = mysql_query($sql);
$row = mysql_fetch_array($res);
if(($row[0] == "Yes") || ($row[0] == "No")){
header("Location: errorsubmit.html");
exit;
}
}
if(isset($_POST['submit'])){
$sql = "UPDATE user SET attendance1 = '" . mysql_real_escape_string($_POST['attendance1']) . "' WHERE username = '" . mysql_real_escape_string($_SESSION['username']) . "'";
mysql_query($sql) or die("Error in SQL: " . mysql_error());
$sql = "UPDATE user SET colour1= '" . mysql_real_escape_string($_POST['colour1']) . "' WHERE username = '" . mysql_real_escape_string($_SESSION['username']) . "'";
mysql_query($sql) or die("Error in SQL: " . mysql_error());
$sql = "UPDATE user SET shade1= '" . mysql_real_escape_string($_POST['shade1']) . "' WHERE username = '" . mysql_real_escape_string($_SESSION['username']) . "'";
mysql_query($sql) or die("Error in SQL: " . mysql_error());
header("Location: thanks.html", true, 303);
}
?>
FORM:
<form>
<input name="attendance1" type="radio" id="Yes" value="Yes" checked="checked"/>Yes
<br />
<input name="attendance1" type="radio" id="No" value="No" />No
</h3></td>
<td>
<select name="colour1" id="colour1" >
<option selected="selected">Please Select</option>
<option>Red</option>
<option>White</option>
<option>Green</option>
</select>
</td>
<td><h3>
<select name="shade1" id="shade1" >
<option selected="selected">Please Select</option>
<option>light</option>
<option>heavy</option>
</select>
<td> </td>
<td><label>
<input type="submit" name="submit" id="button" value="Submit" />
</label></td>
</tr>
</table>
Upvotes: 1
Views: 2304
Reputation:
Try with below:
you need to fetch values from database and match them with select box values to show them selected.
<select name="shade1" id="shade1" >
<option>Please Select</option>
<option value="light" <?php if($val=='light') echo 'selected'; ?>>light</option>
<option value="heavy"<?php if($val=='heavy') echo 'selected'; ?>>heavy</option>
</select>
Here $val
is variable having value retrieved from database.
while adding you should have :
<option value="light" >light</option>
<option value="heavy">heavy</option>
Upvotes: 2
Reputation: 257
You just have to check if the value in the database has the value of the option field, and if so you echo a "selected='true'" to your option tag. Like
<option <?php if($row["column_name"] == "light") echo "selected=\"true\""; ?>>light</option>
Upvotes: 1