Hi experts,
I'm extreme beginner in PHP.My problem is that,i've an html page and there's combobox on it,I want to load the form according to the selection of items.
The html page is

<form action="search.php" method="post">
<select name="option" size="1">
<option value="1">value1</option>
<option value="2">value2</option>
<option value="3">value3</option>
<option value="4">value4</option>
</select>
<input type="submit" />
</form>

and the search.php:

<?php

$v = $_POST['option'];

if($v==1)
{
	?>
	<form method="get" action="page1.php"></form>
	
	<?php
}
if($v==2)
{
	?>
	<form method="get" action="page2.php"></form>
	
	<?php
}
if($v==3)
{
	?>
	<form method="get" action="page3.php"></form>
	
	<?php
}
if($v==4)
{
	?>
	<form method="get" action="page4.php"></form>
	
	<?php
}
?>

I want to navigate through the page according to the value in the variable v.
Thanks in advance.

Recommended Answers

All 3 Replies

<?php
$forms=array(
"1"=>"page1.php",
"2"=>"page2.php",
"3"=>"page3.php");
?>

build the form:
<form action="" method="post" name="Form">
...
select box <--
...</form>

On submit, check if the posted value is in the array:

<?php
if ( !empty( $_POST["option"] ) ) {
    $option = $_POST["option"];
    if ( !empty( $forms[$option] ) ) {

        ?>	<form method="get" action="<?php echo htmlspecialchars( $form[$option] );
        ?>"></form><?php
    } else {
        print "You have selected a non existing option...";
    } 
} 

?>

Thanks djjjozsi,
but my problem is not solved,
The form doesn't works

<form method="get" action="<?php echo $forms[$option] ;
        ?>"></form><?php

but the arrays works.
It doesn't navigate to the page specified in the array

if the user posted the form you want to redirect to the specified page?

header("Location: ". $forms[$option] );

notice that, you should not print or have outputted html codes to the display before this header location.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.