Jump to content

[SOLVED] options form and sessions, help please


meomike2000

Recommended Posts

ok. I have figured out sessions and how to use them. I am still having some problem though with my form that has option values. most of it works except that I use some javascript to control some of the options available depending choices that you make.

 

first, the parts that work. the parts of the form that have actual option values in the form, the ones that you choose from first.

 

the parts that do not work are the ones that are determined by your choice that was made first.

 

i know this is confusing, not sure how to explain it.

here is the code segment of the form.....

 

<form method="post" action="<?=$_SERVER['PHP_SELF']?>">
			State:              
				<select  id="state" name="state" onchange="getCityList(this)">
					<option value="">Select a state</option>
					<option value="Alabama" <?if ($_SESSION['state'] == "Alabama") 
					{echo 'selected="selected"'; } ?> >Alabama</option>
					<option value="North Carolina" <?if ($_SESSION['state'] == "North Carolina") 
					{echo 'selected="selected"'; } ?> >North Carolina</option>
					<option value="Texas" <?if ($_SESSION['state'] == "Texas") 
					{echo 'selected="selected"'; } ?> >Texas</option>
				</select><br />
				<br />
			City:                
				<select id="city" name="city">

				</select><br />
				<br />
			Category:       
				<select id="category" name="category" onchange="getSubCategoryList(this)">
					<option value="">Select a category</option>
					<option value="Auto">Auto</option>
					<option value="Hotel">Hotel</option>
					<option value="Restaurant">Restaurant</option>
				</select><br />
				<br />
			SubCategory:
				<select id="subcat" name="subcat">

				</select><br />
				<br />			
			<input type="submit" name="submit" value="Select">
	</form>

 

in the code above, session works when the page reloads for state and category,

but city and subcategory doesn't. not sure what to do there to get it to work.

the form also uses validation to make sure that you have selected a state and a category.

 

here is the entire form script:

 

<?php
session_start();  
if(isset($_SESSION['state']))
{
 unset($_SESSION['state']);;
}
else
{
$_SESSION['state'] = null;
}
if(isset($_SESSION['city']))
{
 unset($_SESSION['city']);
}
else
{
$_SESSION['city'] = null;
}
if(isset($_SESSION['category']))
{
 unset($_SESSION['category']);
}
else
{
$_SESSION['category'] = null;
}
if(isset($_SESSION['subcat']))
{
 unset($_SESSION['subcat']);
}
else
{
$_SESSION['subcat'] = null;
} 
?>
<html>
<head>

<script language="javascript" type="text/javascript">
<!-- 
//Browser Support Code
function ajaxFunction(){
var ajaxRequest;  // The variable that makes Ajax possible!

try{
	// Opera 8.0+, Firefox, Safari
	ajaxRequest = new XMLHttpRequest();
} catch (e){
	// Internet Explorer Browsers
	try{
		ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		try{
			ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (e){
			// Something went wrong
			alert("Your browser broke!");
			return false;
		}
	}
}
// Create a function that will receive data sent from the server
ajaxRequest.onreadystatechange = function(){
	if(ajaxRequest.readyState == 4){
		document.myForm.city.value = ajaxRequest.responseText;
	}
}
ajaxRequest.open("GET", "secondchoice.php", true);
ajaxRequest.send(null); 

}
//-->
</script>

<script type="text/javascript" src="js/ajax.js"></script>
<script type="text/javascript">

var ajax = new Array();

//this will refresh citys

function getCityList(sel)
{
var stateCode = sel.options[sel.selectedIndex].value;
document.getElementById('city').options.length = 0;	// Empty city select box
if(stateCode.length>0){
	var index = ajax.length;
	ajax[index] = new sack();

	ajax[index].requestFile = 'mygetcity.php?stateCode='+stateCode;	// Specifying which file to get
	ajax[index].onCompletion = function(){ createCities(index) };	// Specify function that will be executed after file has been found
	ajax[index].runAJAX();		// Execute AJAX function
}
}

function createCities(index)
{
var obj = document.getElementById('city');
eval(ajax[index].response);	// Executing the response from Ajax as Javascript code	
}

//this will get subcatagory

function getSubCategoryList(sel)
{
var category = sel.options[sel.selectedIndex].value;
document.getElementById('subcat').options.length = 0;	// Empty subcat select box
if(category.length>0){
	var index = ajax.length;
	ajax[index] = new sack();

	ajax[index].requestFile = 'mygetsubcat.php?category='+category;	// Specifying which file to get
	ajax[index].onCompletion = function(){ createSubCategories(index) };	// Specify function that will be executed after file has been found
	ajax[index].runAJAX();		// Execute AJAX function
}
}
function createSubCategories(index)
{
var obj = document.getElementById('subcat');
eval(ajax[index].response);	// Executing the response from Ajax as Javascript code	
}		
</script>

<title>meo2000.net</title>
<link rel="stylesheet" type="text/css" href="../web1.css" />
<link rel="stylesheet" type="text/css" href="../heading.css" />
<link rel="stylesheet" type="text/css" href="../navigation.css" />
<link rel="stylesheet" type="text/css" href="directory.css" />

</head>
<body>

<div class="navigation" id="navigation">
	<a class="navjust" href="../index.php">Home</a><br />
	<br />
	<a class="navjust" href="http://mail.meo2000.net">Check Mail</a><br />
	<br />
	<a class="navjust" href="../web/webservice.php">Web Services</a><br />
</div>

<div id="justify">
<div id="h">
	<h1>meo2000 listing service</h1>
	<h3>want to get listed, click <a href="getlisted.htm">here</a> to find out how.</h3>
	<h3>This directory is under construction!</h3>
	<br/>
</div>
</div>

<?php
Function DisplayForm($errors = null){
$error_string = $errors; //state, you could use $error_string = implode ("<br />",$errors); if you want to save performance by using an array insead.
	?>
		<div id="left">
		<span class="error">
		<?=$error_string?><br />
		</span>
		<form method="post" action="<?=$_SERVER['PHP_SELF']?>">
			State:              
				<select  id="state" name="state" onchange="getCityList(this)">
					<option value="">Select a state</option>
					<option value="Alabama" <?if ($_SESSION['state'] == "Alabama") 
					{echo 'selected="selected"'; } ?> >Alabama</option>
					<option value="North Carolina" <?if ($_SESSION['state'] == "North Carolina") 
					{echo 'selected="selected"'; } ?> >North Carolina</option>
					<option value="Texas" <?if ($_SESSION['state'] == "Texas") 
					{echo 'selected="selected"'; } ?> >Texas</option>
				</select><br />
				<br />
			City:                
				<select id="city" name="city">

				</select><br />
				<br />
			Category:       
				<select id="category" name="category" onchange="getSubCategoryList(this)">
					<option value="">Select a catagory</option>
					<option value="Auto">Auto</option>
					<option value="Hotel">Hotel</option>
					<option value="Restaurant">Restaurant</option>
				</select><br />
				<br />
			SubCategory:
				<select id="subcat" name="subcat">

				</select><br />
				<br />			
			<input type="submit" name="submit" value="Select">
	</form>
	</div>
<?php
}
//check if form has been submitted
if (!$_POST['submit'])
{
//if not display form
DisplayForm();
}
else
{
//form has been submitted
//items selected, check that number was entered,
 //Declare it so we dont get notices 'undeclared variable'
$error = null;

//set new session values.
$_SESSION['state'] = $_POST['state'];
$_SESSION['city'] = $_POST['city'];
$_SESSION['category'] = $_POST['category'];
$_SESSION['subcat'] = $_POST['subcat'];

//check state has been selected
if (trim($_POST['state']) == "") 
	{	
		$error .='ERROR: Please select a state.';
	}
//check category has been selected
elseif (trim($_POST['category']) == "")
	{
		$error .='ERROR: Please select a category.';
	}
//if there was an error, show it.
if ($error != null){
	DisplayForm($error);
	}
else
	{
	//otherwise carry on?
		echo '<div id="right">';
		$state = $_POST['state'];
		$city = $_POST['city'];
		$cat = $_POST['category'];
		$subcat = $_POST['subcat']; 
			echo 'Here is your selections: <br />';
			echo "<i>$state</i><br />";
			echo "<i>$city</i><br />";
			echo "<i>$cat</i><br />";
			echo "<i>$subcat</i><br />";
		echo '</div>';
	}
}
//clear session values.
if(isset($_SESSION['state']))
    unset($_SESSION['state']);
if(isset($_SESSION['city']))
    unset($_SESSION['city']);
if(isset($_SESSION['categoryt']))
    unset($_SESSION['category']);
if(isset($_SESSION['subcat']))
    unset($_SESSION['subcat']);    
?>

<div class="addspace" id="addspace">
<p class="none" id="addmargin">
	your<br >
	add<br >
	could<br >
	be<br >
	filling<br >
	this<br >
	spot!
</p>
</div>

</body>
</html>

 

there is also a page that is accessed to get the city list after state is selected.

here it is:

<?php

if(isset($_GET['stateCode'])){
  
  switch($_GET['stateCode']){
    
    case "Alabama":
      echo "obj.options[obj.options.length] = new Option('Birmingham');\n";
      echo "obj.options[obj.options.length] = new Option('Huntsville');\n";
      echo "obj.options[obj.options.length] = new Option('Mobile');\n";
      echo "obj.options[obj.options.length] = new Option('Montgomery');\n";
      
      break;
    case "North Carolina":
      
      echo "obj.options[obj.options.length] = new Option('Charlotte');\n";
      echo "obj.options[obj.options.length] = new Option('Greensboro');\n";
      echo "obj.options[obj.options.length] = new Option('Raleigh');\n";
      echo "obj.options[obj.options.length] = new Option('Wilmington');\n";
      
      break;
    case "Texas":
      
      echo "obj.options[obj.options.length] = new Option('Austin');\n";
      echo "obj.options[obj.options.length] = new Option('Dallas');\n";
      echo "obj.options[obj.options.length] = new Option('Houston');\n";
      echo "obj.options[obj.options.length] = new Option('San Antonio');\n";
      
      break;
  }  
}

?> 

 

and the code for that gets the subcat value:

<?php

if(isset($_GET['category'])){
  
  switch($_GET['category']){
    
    case "Auto":
      echo "obj.options[obj.options.length] = new Option('New');\n";
      echo "obj.options[obj.options.length] = new Option('Used');\n";
      echo "obj.options[obj.options.length] = new Option('Parts');\n";
      echo "obj.options[obj.options.length] = new Option('Repair');\n";
      
      break;
    case "Hotel":
      
      echo "obj.options[obj.options.length] = new Option(' ');\n";
      
      break;
    case "Restaurant":
      
      echo "obj.options[obj.options.length] = new Option('Fast Food');\n";
      echo "obj.options[obj.options.length] = new Option('Bar B Que');\n";
      echo "obj.options[obj.options.length] = new Option('Italian');\n";
      echo "obj.options[obj.options.length] = new Option('Sea Food');\n";
      
      break;
  }  
}

?> 

Link to comment
Share on other sites

well i figured out  a fix, may not be the best way or only way but it works.

here is what i added the the code above to fix my problem:

 

City:                
				<select id="city" name="city">
				<?php if ($_SESSION['city'] != null)
				{
					$selected = $_SESSION['city'];
					?> <option value="<?=$_SESSION['city']; ?>" >
					<?php echo "$selected"; ?></option>
					<?php
					$selected = null;
				}
				?>
				</select><br />
				<br />

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.