Jump to content

retain previously selected value on <select> tag after clicking a button


geekisthenewsexy

Recommended Posts

hi guys. i have here a select tag which is wrapped inside php (i'm using mysql as well).

 

<form name="form1" action="do_set.php">
<?php
$sql="SELECT school_year FROM admin_sy ORDER BY school_year ASC" or die (mysql_error());
$result=mysql_query($sql);
echo "Set schoolyear: <select name=SY value=''>";
while($row=mysql_fetch_array($result)) {
	  echo "<option value=$row[school_year]>$row[school_year]</option>";
	  }
	  echo "</select>";
	  ?>
	  <br>
	  Sem: <select name="sem">
        <option value="1st">1st</option>
        <option value="2nd">2nd</option>
        <option value="Summer">Summer</option>
      </select>
  <input type="Submit" name="submit" value="Set">
  </form>

 

as you can see, the values 'school_year' are fetched from a database.

now my problem is, when i select a specific year and sem, the values should be retained after i click set or refresh the page or go back from the previous page. this will be useful so the user will be able to know what schoolyear and sem has already been set.

hope you guys can help..thanks in advance.:)

Link to comment
Share on other sites

Sounds like you're taking on a mammoth build  ;)

 

I am not a PHP developer but to get you started, I would probably take the following approach:

 

$('form input:submit').click(function() {

var selected = '';

$('select option:selected').each(function () {
	selected += $(this).text() + " ";
});

// Go and store selected in session or cookie, use split() to set options on return (if session exists)

});

 

If you are using jQuery then the above will kick you off, the concept would be to store the selections either in PHP Sessions or via Cookies.

 

Cookies have a read here: http://www.w3schools.com/JS/js_cookies.asp

 

PHP sessions - no idea  :P

 

Good luck

Link to comment
Share on other sites

hey there, just thought i'd post this..

 

<?php
include("dbcon.php");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<link rel="stylesheet" href="style.css"/>
</head>
<body> 
<table cellpadding="0" cellspacing="0" border="0" id="table" class="tableview">
<?php
echo '<br/>';
echo '<tr><th class="nosort"><h3> ID</h3><th> Course</th></th><th></th></tr>';
$sql=mysql_query("SELECT * FROM course")or die(mysql_error()); 
$OutLine = array('id'=>'','course'=>'');
while($row=mysql_fetch_array($sql))
{
$OutLine['id'] = $row['c_id'];
$OutLine['course'] = $row['c_name'];
$countroom=0;
if($row>$countroom)
{
echo "<td>".implode('<td/>',$OutLine);
echo '<td align="center">[<a href="editcourse.php?id='.$row['c_id'].'&course='.$row['c_name'].'">Edit</a>] [<a href="delcourse.php?id='.$row['c_id'].'&course='.$row['c_name'].'" onClick="return confirm(\'You are about to delete Course '.$row['c_name'].' and all related records! You will not be able to undo this operation! Delete?\');return false;">Delete</a>]</td>';
echo "</tr>";
}
else
{
echo "<tr>";
echo "<td>No courses added yet.</td>";
echo "</tr>";
}
}
?>
</table>
</body>
</html>

 

this is the page loaded inside the iframe. when i click on delete (note that i'm not using a button) and confirm this,it will send me to 'delcourse.php' -->

 

<?php
include("dbcon.php");
if (isset($_GET['id']) && is_numeric($_GET['id']))
    {  
$id=$_GET['id'];
$course=$_GET['course'];
$result = mysql_query("DELETE FROM course WHERE c_id='$id'") or die(mysql_error());  
$result = mysql_query("DELETE year.*, block.* FROM year,block WHERE year.y_id=block.y_id AND year.c_id='$id'") or die(mysql_error()); 
  }  
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Delete rooms</title>
<link rel="stylesheet" href="style.css"/>
<script language="JavaScript">
function refreshParent()
{

  window.parent.location.reload(); 
  //used this script but the parent won't stop reloading every 2 seconds unless i click the refresh button///
  
}
</script>
</head>
<body onload="setTimeout('refreshParent()',2000)">
<center><img src="images/wait.gif"></center><br>
<center>Record has been deleted!<p>You will be redirected back shortly..</p></center>
</body>
</html>

 

this is the page loaded after i confirm the deletion from 'viewcourse.php'. take note that this is inside the iframe too. notice inside the body tag that i've put a message for the user indicating that the delete was successful. so after 2 seconds 'index.php', the page where the iframe and its contents is nested,should automatically refresh itself.. :confused:

 

Link to comment
Share on other sites

  • 2 weeks later...

hey there nano, thanks again for the response. :)

uhm, do you have like an example where i can place this code?and which ones can i modify for my code? :confused:

 

Easy Way:

 

Put a field in your db that tells you which value is selected. Then use an if statement to echo out the selected="selected" in the <select> element.

 

ex:

<?php
$selected='selected="selected"';
?>
<select name="myList">
<option <?php if($myResult == 'thisValue'){ echo $selected; } ?>>thisValue</option>
</select>

 

this is a basic example. Hope it helps.

 

E

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.