Jump to content

Drop Down based on first, working, but how do I keep the value if page reloaded?


Recommended Posts

Hi everyone,

 

I have a drop down list that is populated via MySQL. Once an item is selected a second drop down appears, again populated by MySQL. I know how to keep the value for the first drop down, if the page is reloaded.... but the second one is where I am having the issue.

 

Is it possible to have a page reload and echo not only the first dropdown value, but also the second within the <select> tags?

 

Thanks

When you say "reload," do you mean that you are submitting the data or pressing refresh? If you are submitting you data it should be easy to grab it from the $_GET or $_POST arrays and reprint depending on what was submitted.

Ideally I would like it to work if the user either hit submit or if the page was reloaded. I am attempting this using sessions.

 

Here is the actually demo:

http://shannoncronin.com/dropdown.php

 

Here is the code:

dropdown.php

<?php session_start; ?>
<html>
<head>
<script src="js/selectuser.js"></script>
</head>
<body>
<select name="comics" onchange="showUser(this.value)">
<?php 
require ('get_connected.php'); //These Seven Lines of Code Populate the comic List from the Database: Do not alter.
$query = mysql_query("SELECT distinct(title) FROM comics ORDER BY title");
echo "<option value=\"0\" selected=\"selected\">Select a Comic Title</option>";
while ($row = mysql_fetch_array($query)) {
$_SESSION['title'] = $row['title'];
echo "<option value=\"" .$_SESSION['title']. "\">" .$_SESSION['title']. "</option>";
	}
?>
</select><br  />
<div id="txtHint"></div> <!--The DIV txtHint Populates the drop down. Do not change. --></p>
<br  />
<input type="submit" name="submit" value="submit" />
</form>
</body>
</html>

 

selectuser.js

// JavaScript Document
var xmlHttp

function showUser(str)
{ 
xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
{
alert ("Browser does not support HTTP Request")
return
}
var url="../getuser.php"
url=url+"?q="+str
url=url+"&sid="+Math.random()
xmlHttp.onreadystatechange=stateChanged 
xmlHttp.open("GET",url,true)
xmlHttp.send(null)
}

function stateChanged() 
{ 
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
{ 
document.getElementById("txtHint").innerHTML=xmlHttp.responseText 
} 
}

function GetXmlHttpObject()
{
var xmlHttp=null;
try
{
// Firefox, Opera 8.0+, Safari
xmlHttp=new XMLHttpRequest();
}
catch (e)
{
//Internet Explorer
try
  {
  xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
  }
catch (e)
  {
  xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
}
return xmlHttp;
}

 

getuser.php

<?php
session_start();
$q=$_GET["q"];
require ('get_connected.php');

$sql="SELECT * FROM comics WHERE title = '".$q."' ORDER BY issue_number";

//ISSUE SELECT
$result = mysql_query($sql);
echo "<br /><label for=\"testinput\"><b>Select an Issue Number</b></label>";
echo "<br>";
echo "<select name=\"issue\">";
echo "<option value=\"NULL\" selected=\"selected\">Select An Issue Number</option>";
while($row2 = mysql_fetch_array($result))
{
$_SESSION['issue_number'] = $row2['issue_number'];
echo "<option value=\"" .$_SESSION['issue_number']. "\"># " .$_SESSION['issue_number']. "</option>";
}
echo "</select>";
echo "<br>";
?>

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.