PHP Form DropDown Menu

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
angelc
Forum Newbie
Posts: 6
Joined: Tue Apr 25, 2006 4:17 pm

PHP Form DropDown Menu

Post by angelc »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hi Guys, 

I have a session problem here. Anyone know why I will lost my session and log off when I select the Option from the drop down menu please?

Code: Select all

<script language="javascript">

// FUNCTION - DROP-DOWN MENU
// This function runs the "productSelect" drop down menu
function postBack()
{
	
	//document.selectProduct.submit();
	document.sec.submit();	
	var val=document.selectProduct.products.options[document.selectProduct.products.selectedIndex].value; 
	location='support_test3.php?products=' + val ;
	
}


</script>


<!-- BEGIN PHP SCRIPT -->
<!-- begin graphic header -->
<form method="post" action="support_test3.php" name="sec">
<input type="hidden" name=username value=<?php echo $_POST['username'];?>></input>
<input type="hidden" name=password value=<?php echo $_POST['password'];?>></input>
<input type="hidden" name="login" value=''></input>

</form>

<div class="topGraphic">

<form action="<? echo $_SERVER['PHP_SELF']; // Basically prints "/support.php" ?>" 
method="post" name="selectProduct" style="border:0px ; margin: 0px; padding: 0px;">
	

	<div style="background-image:url(
	
	<?
	
	
	// If statement - If "$supportauth" EQUALS EXACTLY "1" (if "check_pass" was approved), run the code below.
	if($login_pass == 1 ||$userdata['session_logged_in'])  {
		echo "images/support/supportproductsselect3.jpg);";	// Set the new Support image (logged in) as the div's bg image.
		
	} else {
		echo "images/support/supportproducts_03.jpg);";	// Set the default Support banner as the div's bg image.
		
		}
	?>
		background-position:top; background-repeat:no-repeat; width:737px; height:106px; ">
			<div style="position:relative; top:49px; left:205px; display:<? 
			if($login_pass == 1 ||$userdata['session_logged_in'])  {
			echo "block";
			}else{
				echo "none";
				}	// Print whatever was selected 
																							// in the print_combo function 
																							// (either "block" or "none") 
																							?> ">
		
        <select name="products" onchange="postBack()">
        <option selected value="0">(Please select:)</option>
		<option value="0">---------------------------</option>
		<option value="1">Admin Tool 3.5</option>
 		<option value="4">Admin Tool 3.4.3</option>
		<option value="0">---------------------------</option>
		<option value="3">Incident Reporting 3.4.3</option>
        <option value="5">Incident Reporting 3.4.2</option>
      	 </select>
		<? $products=$HTTP_GET_VARS['products'];?>
            </div>
	
	</div>

<div>
	<!-- These are the title images located directly below the main header graphic -->
			<img src="<?
			
			
			$products=$HTTP_GET_VARS['products'];
	
			// If statement - If that number is selected (in the drop down menu), display the accompanying title image.
			switch ($products){
				  case "1" :				 
  				 	echo "./images/support/SOC.jpg";					
   					break;
					
					case "2" :					
  				 	echo "./images/support/IR35.jpg";
   					break;
					
					case "3" :					
  				 	echo "./images/support/IR343.jpg";
   					break;
					
					case "4" :					
  				 	echo "./images/support/IR343admin.jpg";
   					break;
					
					case "5" :					
  				 	echo "./images/support/IR342.jpg";
   					break;
					
					default :
  					echo "./images/support/supportproducts_su.jpg";
   					break;

			}
		
			
			?> ">
	</div>

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

shouldn't the page being submitted to handle that redirection?
angelc
Forum Newbie
Posts: 6
Joined: Tue Apr 25, 2006 4:17 pm

Post by angelc »

Hi,

Whenever the user choose option from the dropdown menu. The page actually submit back to its own page and the content display underneath the dropdown menu. But now I am not sure why the session that I stored ..will lost if i go to the drop down menu first.

I tired to comment out the

Code: Select all

location='support_test3.php?products=' + val ;
The session won't lost but I can't display the content according to the option value that i select from the drop down menu.


Thanks
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Have the page itself do that for you by reading the submitted data.
angelc
Forum Newbie
Posts: 6
Joined: Tue Apr 25, 2006 4:17 pm

Post by angelc »

Hi,

Sorry, I am not really good at PHP. I am just a beginner. What do you mean "Have the page itself do that for you by reading the submitted data."?

Did you see any syntax errors and logically errors ?

Thanks
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Lose the onchange event, have support_test3.php look at $_POST instead of $HTTP_GET_VARS
angelc
Forum Newbie
Posts: 6
Joined: Tue Apr 25, 2006 4:17 pm

Post by angelc »

However, if i take out the onchange = postBack(). Then, how can I excute the

this codes

Code: Select all

var val=document.selectProduct.products.options[document.selectProduct.products.selectedIndex].value; 
        location='support_test3.php?products=' + val ;
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

You don't need it if you do the changes I said.
angelc
Forum Newbie
Posts: 6
Joined: Tue Apr 25, 2006 4:17 pm

Post by angelc »

The whole thing is actually a php login integration page ..i have my own website login page, but I want the user just need to login once. For example after the user login to the my page then they don't have to login to the phpBB forum again. The username and password that the user input will be retrive from the phpBB database.

I have a page called support_loginTest.php

here is the code

Code: Select all

,

Code: Select all

<? session_start(); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<head>
<? include("includes/head.php"); ?>
</head>
<body onload="preloadImages();setFooter();" onresize="setFooter();">
<div id="contentwrap">
<!-- begin header -->
<? include("includes/header.php"); ?>
<!-- end header -->
<!-- begin nav -->
<? $selected="support";
	include("includes/nav.php");
?>
<!-- end nav -->


<!-- begin graphic header -->

<div class="topGraphic">

<form action="<? echo $_SERVER['PHP_SELF']; // Basically prints "/support.php" ?>" 
method="get" name="selectProduct" style="border:0px ; margin: 0px; padding: 0px;">
	<div style="background-image:url(
	
	<?
	
		echo "images/support/supportproducts_04.jpg);";	// Set the default Support banner as the div's bg image.
				
	?>
		background-position:top; background-repeat:no-repeat; width:737px; height:135px; ">
			<div style="position:relative; top:49px; left:205px"></div>
       
	
	</div>

	
</form>
</div>



<!-- begin end graphic header -->
<!-- begin content -->
<table cellpadding="0" cellspacing="0" border="0" id="product">
  <tr>
  <!-- "nav_support.php" is the little Support navigation stuff that appears on the left-hand side of the screen -->
    <td id="content_lc_inside">
	<? 
		
	include("includes/nav_support2.php");	// Insert the code within "nav_support.php" in this spot.
	
	?>
    </td>
	
	<td>
	<div id="product_main">
	<div id="lc">
	<!-- Begin LOGIN FORM -->
				
		<form action="support_test6.php" method="post" style="border:0px ; margin: 0px; padding: 0px;">
		  <table width="100%"  border="0" cellspacing="0" cellpadding="0">
		  <td>
			<tr style="padding-bottom:4px">
			  <td width="80" class="body3"><b>User Name </b></td>
			  <td width="150" align="left"><input type="text" name="username"
			  class="formField" maxlength="50" style="width:150px; margin-bottom:4px;" /></td>
			</tr>
			<tr style="padding-bottom:4px">
			  <td width="80"><b>Password</b> </td>
			  <td width="150" align="left"><input type="password" name="password" class="formField" maxlength="50" style="width:150px;margin-bottom:4px;" /></td>
			</tr>
			<tr>
			  <td width="80">&nbsp;</td>
			  <td width="150" align="right"><input type="submit" name="login" value="Sign In" /></td>
			</tr>
		  </td>
		  </table>
		</form>
	<!-- End LOGIN FORM -->
		</div>
				
		</div>
	</td>
	
   	<!-- ********************************* 
		 PAGE CONTENT (MIDDLE) STARTS HERE 
		 ********************************* -->
		<!-- content pane end -->

  </tr>
</table>
</div>
<!-- end content -->
<!-- begin footer -->
<? include("includes/footer.php"); ?>
<!-- end footer -->
Then the login form will submit to a page called support_test6.php. In support_test6.php, it will validate the username and password from the phpBB database. The user can access to the forum page by a include page that contain the forum button. The support_test6.php also has a drop down menu. If user login success, the drop down menu will show up and allow the user to select option. Each option has a value assigned to it. Whenever, the user select an option the "option value" from the drop down menu will pass to the switch case. Switch($products). The problem I have now is I lost the session $login_pass and $userdata['session_logged_in'] when I select the option from the drop down menu after I login in. However, after I login and go to the phpbb forum page and go back to the support_test6.php. I won't lost the session then logoff and the dropdown menu works fine,

1st Login
$login_pass return 1; $userdata['session_logged_in'] return 0;
**The drop down menu doesn't work. Both session lost and i will be logoff**

1stLogin > phpBBform > support_test6.php
$login_pass return 0; $userdata['session_logged_in'] return 1;
**The drop down menu work fine !**

The following is the code for support_test6.php

Code: Select all

,

Code: Select all

<?
  session_start();   
  define('IN_PHPBB',true); 

  
$phpbb_root_path = /forum/"; // set path to phpBB files 
include($phpbb_root_path . 'extension.inc'); 
include($phpbb_root_path . 'common.'.$phpEx); 

// 
// Start session management 
// 
$userdata = session_pagestart($user_ip, PAGE_LOGIN); // initiate session 
init_userprefs($userdata); // load user prefs 


// 
// End session management 
// 


?>  

<?php


//$userdata = session_pagestart($user_ip, PAGE_LOGIN);
//init_userprefs($userdata);

// session id check
if (!empty($HTTP_POST_VARS['sid']) || !empty($HTTP_GET_VARS['sid']))
{
	$sid = (!empty($HTTP_POST_VARS['sid'])) ? $HTTP_POST_VARS['sid'] : $HTTP_GET_VARS['sid'];
}
else
{
	$sid = '';
}
//make the connection to the database
$connection = @mysql_connect("localhost", "admin", "admin") or die(mysql_error());
$db = @mysql_select_db("user",$connection)or die(mysql_error());

$sql = "SELECT * FROM `phpbb_users` where username = '$_POST[username]' and user_password = '". md5($_POST[password])."'";
//echo $sql;	


$result = @mysql_query($sql,$connection) or die(mysql_error());

	if(@mysql_num_rows($result) == 1)  {
				$login_pass = 1;
				session_register("login_pass");
				
	} else {
				$login_pass = 0;
				
				
	}
	
	
	

?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<head>
<? include("includes/head.php"); ?>
</head>
<body onload="preloadImages();setFooter();" onresize="setFooter();">
<div id="contentwrap">
<!-- begin header -->
<? include("includes/header.php"); ?>
<!-- end header -->
<!-- begin nav -->
<? $selected="support";
	if($login_pass == 1 ||$userdata['session_logged_in']){
		include("includes/nav_test3.php"); 
	} else{
		include("includes/nav.php");
	}
	
	?>
<!-- end nav -->


<script language="javascript">

// FUNCTION - DROP-DOWN MENU
// This function runs the "productSelect" drop down menu
function postBack()
{
	
	//document.selectProduct.submit();
	document.sec.submit();	
	var val=document.selectProduct.products.options[document.selectProduct.products.selectedIndex].value; 
	location='support_test6.php?products=' + val;
	
}


</script>



<!-- BEGIN PHP SCRIPT -->
<!-- begin graphic header -->
<form method="post" action="support_test3.php" name="sec">
<input type="hidden" name=username value=<?php echo $_POST['username'];?>></input>
<input type="hidden" name=password value=<?php echo $_POST['password'];?>></input>
<input type="hidden" name="login" value=''></input>

</form>

<div class="topGraphic">

<form action="<? echo $_SERVER['PHP_SELF']; // Basically prints "/support.php" ?>" 
method="post" name="selectProduct" style="border:0px ; margin: 0px; padding: 0px;">
	

	<div style="background-image:url(
	
	<?
	
	
	// If statement - If "$supportauth" EQUALS EXACTLY "1" (if "check_pass" was approved), run the code below.
	if($login_pass == 1 ||$userdata['session_logged_in'])  {
		echo "images/support/supportproductsselect3.jpg);";	// Set the new Support image (logged in) as the div's bg image.
		
	} else {
		echo "images/support/supportproducts_03.jpg);";	// Set the default Support banner as the div's bg image.
		
		}
	?>
		background-position:top; background-repeat:no-repeat; width:737px; height:106px; ">
			<div style="position:relative; top:49px; left:205px; display:<? 
			
			if($login_pass == 1 ||$userdata['session_logged_in'])  {
			echo "block";
			}else{
				echo "none";
				}	// Print whatever was selected 
																							// in the print_combo function 
																							// (either "block" or "none") 
																							?> ">
		
        <select name="products" onchange="postBack()">
        <option selected value="0">(Please select:)</option>
		<option value="0">---------------------------</option>
		<option value="1">3.5</option>
 		<option value="4">3.4.3</option>
		<option value="0">---------------------------</option>
		<option value="3">Reporting 3.4.3</option>
        <option value="5">Reporting 3.4.2</option>
      	 </select>
		<? $products=$HTTP_GET_VARS['products'];?>
            </div>
	
	</div>

<div>
	<!-- These are the title images located directly below the main header graphic -->
			<img src="<?
			
			
			$products=$HTTP_GET_VARS['products'];
			 
			// If statement - If that number is selected (in the drop down menu), display the accompanying title image.
			switch ($products){
				  case "1" :				 
  				 	echo "./images/support/1jpg";					
   					break;
					
					case "2" :					
  				 	echo "./images/support/I2.jpg";
   					break;
					
					case "3" :					
  				 	echo "./images/support/I3.jpg";
   					break;
					
					case "4" :					
  				 	echo "./images/support/Iadmin.jpg";
   					break;
					
					case "5" :					
  				 	echo "./images/support/I8.jpg";
   					break;
					
					default :
  					echo "./images/support/suppo.jpg";
   					break;

			}
		
			
			?> ">
	</div>
	
</form>
</div>


<!-- begin end graphic header -->
<!-- begin content -->
<table cellpadding="0" cellspacing="0" border="0" id="product">
  <tr>
  <!-- "nav_support.php" is the little Support navigation stuff that appears on the left-hand side of the screen -->
    <td id="content_lc_inside">
	<? 
	
	include("includes/nav_support4.php");	// Insert the code within "nav_support.php" in this spot.
	
	?>
    </td>
	
   	<!-- ********************************* 
		 PAGE CONTENT (MIDDLE) STARTS HERE 
		 ********************************* -->
	<?
	echo $userdata['session_logged_in'];
	echo $login_pass;
	
	if($login_pass == 1 ||$userdata['session_logged_in']) {
	
		
	$products=$HTTP_GET_VARS['products'];
	
			// If statement - If that number is selected (in the drop down menu), display the accompanying title image.
			switch ($products){
				  case "1" :
  				 	include("./support/S.php");
					break;
					
					case "2" :
  				 	include("./support/I5.php");
   					break;
					
					case "3" :
  				 	include("./support/I43.php");
   					break;
					
					case "4" :
  				 	include("./support/I3admin.php");
   					break;
					
					case "5" :
  				 	include("./support/I42.php");
   					break;
					
										
					default :
  					include("./support/ee.php");
   					break;

				}	
			}else { 
				
				
				<p><h1style="color:#FF0000">Login Failed.</h1></p>
				
				
					}
	
		?>
	


	<!-- content pane end -->

  </tr>
</table>
</div>
<!-- end content -->
<!-- begin footer -->
<? include("includes/footer.php"); ?>
<!-- end footer -->
angelc
Forum Newbie
Posts: 6
Joined: Tue Apr 25, 2006 4:17 pm

Post by angelc »

Hi Fedy

I tried what you asked me to do. However, it doesn't work. >< The sesssion didn't lost but the drop down menu doesn't work anymore.

Angel
Post Reply