Current location: Hot Scripts Forums » Programming Languages » PHP » PHP Select Form Processing


PHP Select Form Processing

Reply
  #1 (permalink)  
Old 06-16-09, 04:24 PM
tjs tjs is offline
Newbie Coder
 
Join Date: Jan 2006
Posts: 42
Thanks: 0
Thanked 0 Times in 0 Posts
Hazard PHP Select Form Processing

Hey all,

Here's my situation:

I have a form that has 2 select boxes with multiple options.

For the First dropdown, I have assigned IDs to each option (1,2,3,etc)
For the Second dropdown, I have also assigned IDs to each option (01,02,03,etc)

When they choose an option from both select boxes and hit submit, I need to the form to processes the information so it realizes that both a First_id and a Second_id are selected, combines the two together -- $First_id.$Second_id -- and redirect the user to another page.

Hope that makes sense and someone can help me out.
Thanks!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 06-17-09, 01:17 AM
xEX3CUT1ONx xEX3CUT1ONx is offline
Wannabe Coder
 
Join Date: Jun 2008
Posts: 116
Thanks: 2
Thanked 0 Times in 0 Posts
Seems fairly easy

PHP Code:



//Create some new variables

$first $_POST['first'];
$second $_POST['second'];

$both_var $first $second;

// Redirect (May/May not work)

header('Location:http://www.yoursite.net'); 
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 06-17-09, 02:10 AM
job0107's Avatar
job0107 job0107 is offline
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
You weren't being very specific, so I can think of at least a couple different possibilities.

Say if you wanted to concatenate id1 and id2 and send the value to another page,
then you may do it like this:
PHP Code:

<?php
if(!empty($_POST["id1"]) && !empty($_POST["id2"]))
{
 
$id $_POST["id1"].$_POST["id2"];
 
header("location: mypage.php?id=$id");
 }
?>
<html>
<head>
<title></title>
</head>
<body>
<form action="#" method="POST">
First Id: <select name="id1">
<option value="">Select first id ...</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<br />
Second Id: <select name="id2">
<option value="">Select second id ...</option>
<option value="01">01</option>
<option value="02">02</option>
<option value="03">03</option>
</select>
<p>
<input type="submit" value="Submit">
</p>
</form>
</body>
</html>
But if you wanted to concatenate id1 and id2 and send the user to a different page depending on the resultant value,
then you could do it like this:
PHP Code:

<?php
if(!empty($_POST["id1"]) && !empty($_POST["id2"]))
{
 switch (
$_POST["id1"].$_POST["id2"])
 {
  case 
"101":
   
header("location: page1.php");
   break;
  case 
"102":
   
header("location: page2.php");
   break;
  case 
"103":
   
header("location: page3.php");
   break;
  case 
"201":
   
header("location: page4.php");
   break;
  case 
"202":
   
header("location: page5.php");
   break;
  case 
"203":
   
header("location: page6.php");
   break;
  case 
"301":
   
header("location: page7.php");
   break;
  case 
"302":
   
header("location: page8.php");
   break;
  case 
"303":
   
header("location: page9.php");
   break;
  }
 }
?>
<html>
<head>
<title></title>
</head>
<body>
<form action="#" method="POST">
First Id: <select name="id1">
<option value="">Select first id ...</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<br />
Second Id: <select name="id2">
<option value="">Select second id ...</option>
<option value="01">01</option>
<option value="02">02</option>
<option value="03">03</option>
</select>
<p>
<input type="submit" value="Submit">
</p>
</form>
</body>
</html>
__________________
Jerry Broughton

Last edited by job0107; 06-17-09 at 02:25 AM.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #4 (permalink)  
Old 06-17-09, 02:11 PM
tjs tjs is offline
Newbie Coder
 
Join Date: Jan 2006
Posts: 42
Thanks: 0
Thanked 0 Times in 0 Posts
wow thanks that helped alot!

now a follow up:
how do I set it up so that if they only choose one of the options, they are taken to the form page again and asked to choose one of the second options.

I'm assuming it would be something like:
IF id1 is empty then
or
IF id2 is empty then

Thanks you guys have been a great help!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #5 (permalink)  
Old 06-17-09, 06:25 PM
Jcbones Jcbones is offline
Aspiring Coder
 
Join Date: Mar 2009
Location: North Carolina, USA
Posts: 516
Thanks: 5
Thanked 47 Times in 44 Posts
Untested, but should work.

PHP Code:

<?php
if(empty($_POST['id1']) || empty($_POST['id2'])) {
header('Refresh: 5; url=http://site/formpage.php');
print 
'You must select both id1 and id2 from the form!';
}
elseif(!empty(
$_POST["id1"]) && !empty($_POST["id2"]))
{
 switch (
$_POST["id1"].$_POST["id2"])
 {
  case 
"101":
   
header("location: page1.php");
   break;
  case 
"102":
   
header("location: page2.php");
   break;
  case 
"103":
   
header("location: page3.php");
   break;
  case 
"201":
   
header("location: page4.php");
   break;
  case 
"202":
   
header("location: page5.php");
   break;
  case 
"203":
   
header("location: page6.php");
   break;
  case 
"301":
   
header("location: page7.php");
   break;
  case 
"302":
   
header("location: page8.php");
   break;
  case 
"303":
   
header("location: page9.php");
   break;
  }
 }
?>
<html>
<head>
<title></title>
</head>
<body>
<form action="#" method="POST">
First Id: <select name="id1">
<option value="">Select first id ...</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<br />
Second Id: <select name="id2">
<option value="">Select second id ...</option>
<option value="01">01</option>
<option value="02">02</option>
<option value="03">03</option>
</select>
<p>
<input type="submit" value="Submit">
</p>
</form>
</body>
</html>
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #6 (permalink)  
Old 06-19-09, 09:05 AM
ElTorito ElTorito is offline
New Member
 
Join Date: Jun 2009
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
make it easy

very funny - make a callback - aka name ur functins idproc011.php, idproc021.php etc.

function idproc011();
{
bla, bla,
}

function idproc021();
{
bla, bla,
}

$idselect = "idproc" . $_POST["id1"] . $_POST["id2"];

$idselect(); // this calls function with variable name
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #7 (permalink)  
Old 06-19-09, 04:27 PM
Jcbones Jcbones is offline
Aspiring Coder
 
Join Date: Mar 2009
Location: North Carolina, USA
Posts: 516
Thanks: 5
Thanked 47 Times in 44 Posts
Easy???

Quote:
Originally Posted by ElTorito View Post
very funny - make a callback - aka name ur functins idproc011.php, idproc021.php etc.

function idproc011();
{
bla, bla,
}

function idproc021();
{
bla, bla,
}

$idselect = "idproc" . $_POST["id1"] . $_POST["id2"];

$idselect(); // this calls function with variable name
Not sure how that is any easier? You will have to do the same checks, and processing. You also will not have a default setting like you will with the switch() function.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #8 (permalink)  
Old 12-03-09, 03:41 PM
job0107's Avatar
job0107 job0107 is offline
Community Liaison
 
Join Date: Dec 2006
Location: Tacoma, Washington USA
Posts: 3,454
Thanks: 0
Thanked 140 Times in 137 Posts
We could make Jcbones's version a little more user friendly.
PHP Code:

<?php
$options1 
= array("1","2","3");
$options2 = array("01","02","03");
if(!empty(
$_POST["submit"]))
{
 if(empty(
$_POST['id1']) || empty($_POST['id2'])){echo "You must select both id1 and id2 from the form!";}
 elseif(!empty(
$_POST["id1"]) && !empty($_POST["id2"]))
 {
  switch (
$_POST["id1"].$_POST["id2"])
  {
   case 
"101":
    
header("location: page1.php");
    break;
   case 
"102":
    
header("location: page2.php");
    break;
   case 
"103":
    
header("location: page3.php");
    break;
   case 
"201":
    
header("location: page4.php");
    break;
   case 
"202":
    
header("location: page5.php");
    break;
   case 
"203":
    
header("location: page6.php");
    break;
   case 
"301":
    
header("location: page7.php");
    break;
   case 
"302":
    
header("location: page8.php");
    break;
   case 
"303":
    
header("location: page9.php");
    break;
   }
  }
 }
?>
<html>
<head>
<title></title>
</head>
<body>
<form action="#" method="POST">
First Id: <select name="id1">
<option value="">Select first id ...</option>
<?php
for($i=0;$i<count($options1);$i++)
{
 if(!empty(
$_POST["id1"]) && $_POST["id1"] == $options1[$i]){echo "<option value='".$options1[$i]."' selected>".$options1[$i]."</option>";}
 else{echo 
"<option value='".$options1[$i]."'>".$options1[$i]."</option>";}
 }
?>
</select>
<br />
Second Id: <select name="id2">
<option value="">Select second id ...</option>
<?php
for($i=0;$i<count($options2);$i++)
{
 if(!empty(
$_POST["id2"]) && $_POST["id2"] == $options2[$i]){echo "<option value='".$options2[$i]."' selected>".$options2[$i]."</option>";}
 else{echo 
"<option value='".$options2[$i]."'>".$options2[$i]."</option>";}
 }
?>
</select>
<p>
<input type="submit" name="submit" value="Submit">
</p>
</form>
</body>
</html>
__________________
Jerry Broughton
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #9 (permalink)  
Old 12-03-09, 05:42 PM
wirehopper's Avatar
wirehopper wirehopper is offline
-
 
Join Date: Feb 2006
Posts: 2,516
Thanks: 20
Thanked 109 Times in 106 Posts
Another option:

PHP Code:

/* Default page - either the same page, or an error page */

$sPage='default.php';
/* Ensure both ids are set.  It's okay if they are set but empty */
if (isset($_POST['id1']) && isset($_POST['id2']))
{
  
$aPageMap=array('101'=>'page1.php',
                                
'102'=>'page2.php',
                                
'103'=>'page3.php',
                                
'201'=>'page4.php',
                                
'202'=>'page5.php',
                                
'203'=>'page6.php',
                                
'301'=>'page7.php',
                                
'302'=>'page8.php',
                                
'303'=>'page9.php');
  
/* Create the array index - it's okay if it is not valid */
  
$iPageIndex=$_POST['id1'].$_POST['id2'];
  
/* Check to see if the array index is valid, if so, use it */
  
if (array_key_exists($iPageIndex,$aPageMap))
    
$sPage=$aPageMap[$iPageIndex];
}
/* Go to the page */
header('location: '.$sPage); 
This could also be done in javascript, like so:

HTML Code:
<script type="text/javascript">
function go()
{
  var o1=document.getElementById('id1');
  var o2=document.getElementById('id2');
  var i=0;l=o1.options.length;
  for (i=0;i<l;i++)
  if (o1.options[i].selected)
  {
    m=o2.options.length;
    for (j=0;j<m;j++)
      if (o2.options[j].selected)
        location.href=o1.options[i].value+o2.options[j].value;
  }
}
</script>

<!-- Be sure to give the <select> tags ids -->
<select name="id1" id="id1">

...

<select name="id2" id="id2">

...

<button onclick="go()">Go</button>
Not tested.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks

Tags
form, php, select


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
help: write new php file to server using a form. jimmyjim PHP 9 03-15-09 04:43 AM
multiple select box with PHP zoliky PHP 4 11-02-06 04:26 AM
submit form in php danlew PHP 11 01-11-06 04:34 AM
Simple order PHP form to Email wearetwo PHP 8 01-06-06 04:33 AM
PHP multi-dimensional array sorting issue aqw PHP 2 06-25-05 12:09 AM


All times are GMT -5. The time now is 07:18 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.