S. N
S. N

Reputation: 3949

sending a value from drop down box to another page

I have the following drop down box. I want to be able to select a value from this box and using Php sending it to another page.

this is my current code for drop dwon box:

version2:

<form style="margin-top:20px; margin-right:+50px" name="formname" method="post" action="search.php">
<div class="controls controls-row">
<select name="select1" id="select1"> 
<option value="SelectIsland">Select your island</option>
<option value="stMaarten">St. Maarten</option>
<option value="Aruba">Aruba</option>
<option value="Curacou">Curacou</option>
<option value="StBarths">St. Barths</option>
</select>   

<select name="select2"> 
<option value="SelectLoc">Select your location</option>
<option value="Philipsburg">Philipsburg</option>
<option value="SimpsonBay">Simpson Bay</option>
<option value="Maho">Maho</option>
<option value="Cupecoy">Cupecoy</option>
<option value="Middleregion">Middle region</option>
<option value="MadameEstate">Madame Estate</option>
<option value="StPeters">St. Peters</option>
<option value="DawnBeach">Dawn Beach</option>
</select>
<input type="submit" name="button" class="btn btn-primary" value="Find Restaurant" style="margin-top:-10px; margin-right:0px" />
</div>
<?php 
$select1 = $_POST['select1'];
?>
</form>

I am trying to pass $select1 to search.php, using this: $select1 = $_POST['select1'];

however I get this following error, without doing anything: Notice: Undefined index: select1 in C:\wamp\www\iLandgrub.com V 1.2\main page\index.php on line 194, on y index.php.

If I remove $select1 = $_POST['select1']; then it will work but on search.php I dont get the values I wanted.

this my code in search.php:

the same applies for select2, as well. I am already in my index.php withut doing anything I get the error Undefined index.

<?php
if(isset($_POST['select1']) && isset($_POST['select2'])) {
$select1 = $_GET['select1'];
$select2 = $_GET['select2'];
echo $select1;
echo "";
echo $select2;
}
else{
echo "not set";
}
?> 

enter image description here this is the error in index.php, when you go to this page.

Upvotes: 1

Views: 12997

Answers (5)

WhiteHawk
WhiteHawk

Reputation: 89

your updated solution i am run this problem on my computer
Select your island St. Maarten Aruba Curacou St. Barths

<select name="select2"> 
<option value="SelectLoc">Select your location</option>
<option value="Philipsburg">Philipsburg</option>
<option value="SimpsonBay">Simpson Bay</option>
<option value="Maho">Maho</option>
<option value="Cupecoy">Cupecoy</option>
<option value="Middleregion">Middle region</option>
<option value="MadameEstate">Madame Estate</option>
<option value="StPeters">St. Peters</option>
<option value="DawnBeach">Dawn Beach</option>
</select>
<!--<input type="submit" name="button" value="search"  />
-->

<?php
if(isset($_POST['select1']) && isset($_POST['select2'])) {
$select1 = $_POST['select1'];
$select2 = $_POST['select2'];
echo $select1;
echo "";
echo $select2;
}
else{
echo "not set";
}
?>

Upvotes: 0

S. N
S. N

Reputation: 3949

I solved it. buy using only $_POST, instead of $_get and I also removed

<?php 
$select1 = $_POST['select1'];
?>

now the error - undefined index - is not showing up anymore, and I get the correct value in the search.php page. thanks guys for helping me.

Upvotes: 0

Amar Banerjee
Amar Banerjee

Reputation: 5012

Here is the error

<a href="search.php" class="btn btn-primary" style="margin-top:-10px; margin-right:0px"type="button" name="button">Find Restaurant</a>

Use a submit button instead of that.

<input type="submit" class="btn btn-primary" value="Find Restaurant" />

And if you want to use a tag then do like this.

 <a href="Javascript:void(0)" class="btn btn-primary" style="margin-top:-10px; margin-right:0px"type="button" name="button" onclick="document.formname.submit();">Find Restaurant</a>

Give a name to your form tags

<form style="margin-top:20px; margin-right:+50px" name="formname" method="post" action="search.php"> 

Both the way you can do it. But in your existing code you are not submiting the form rather you are just linked it to the file.

Upvotes: 2

Shahbaz
Shahbaz

Reputation: 3463

remove the anchor tag from button when you are using action, you have to do something like this

<input class="btn btn-primary" type="submit" name="button" />

Upvotes: 1

Prisoner
Prisoner

Reputation: 27628

That's because you're using an anchor element. Change

<a href="search.php" class="btn btn-primary" style="margin-top:-10px; margin-right:0px"type="button" name="button">Find Restaurant</a>

to

<input type="submit" class="btn btn-primary" value="Find Restaurant" />

This will post the form data to search.php - if you want the page to submit to itself, change action="search.php" to action="".

Note: you will need to change your CSS to style the button in the same way.

Upvotes: 3

Related Questions