This supposedly simple but for some reason i cant figure out the value of a multiple select.
Here is an example:
<form action="" method="post">
<select name="cars" multiple="multiple">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="opel">Opel</option>
<option value="audi">Audi</option>
</select>
<input type="submit" />
</form>
Now in the php side i am trying to echo or print_r($_POST['cars']);
However it doesnt print an array it only prints one value from the picked options.
Change the name of your <select> to an array:
<form action="" method="post">
<!-- change cars to cars[] -->
<select name="cars[]" multiple="multiple">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="opel">Opel</option>
<option value="audi">Audi</option>
</select>
<input type="submit" />
</form>
Related
I'm new in php, and I need help. I have created drop down list, than I should hit submit button and it should run one of my php files that are in drop down list. (using if statement) I tried in this way:
<p>
What Genre you want?
<select name="Ganre">
<option value="">Select...</option>
<option value="FPS">FPS</option>
<option value="JRPG">JRPG</option>
<option value="RPG">RPG</option>
<option value="Sports">Sports</option>
</select>
<?php if (Genre == "FPS"): ?>
<FORM METHOD="LINK" ACTION="FPS.php">
<INPUT TYPE="submit" VALUE="Submit">
</FORM>;
</p>
Use your code like this:
<?php if (Genre == "FPS") { ?>
<form method="get" action="FPS.php">
<select name="Genre">
<option value="">Select...</option>
<option value="FPS">FPS</option>
<option value="JRPG">JRPG</option>
<option value="RPG">RPG</option>
<option value="Sports">Sports</option>
</select>
<input type="submit" value="Submit" />
</form>
<?php } ?>
You can get values in PHP by using PHP superglobal (POST/GET):
<?php
if(isset($_GET['Ganre'])){
echo $_GET['Ganre'];
// use your stuff that you want in IF condition.
}
?>
And if you need values in URL as sub string than you need to replace form method with $_GET:
Change:
<FORM METHOD="LINK" ACTION="FPS.php">
With:
<form method="get" action="yourfilename.php">
I have a form with two select field. The select use onChange="form1.submit()" to submit form. And how I know what select field in the form is selected by user? I have tried to do this with two Submit Button and I can know what button have submitted easily.
<?php
if(isset($_POST["select"])) {
echo "submit";
}
if(isset($_POST["select2"])) {
echo "submit2";
}
?>
<form id="form1" name="form1" method="post">
<select name="select" id="select" onChange="this.form.submit()">
<option value="1">Select a</option>
<option value="1">Select b</option>
</select>
<select name="select2" id="select" onChange="this.form.submit()">
<option value="2">Select2 a</option>
<option value="2">Select2 b</option>
</select>
</form>
Both the <select> values are sent to the server. What you need to check is for their emptiness. So kindly change your code to:
<?php
if(!empty($_POST["select"])) {
echo "submit";
}
if(!empty($_POST["select2"])) {
echo "submit2";
}
?>
I do believe you should change the value of the select too, here:
Select a
Select b
Select2 a
Select2 b
Generally, to check what's happening / getting posted to the code, you can do it in two ways:
PHP Way
Use var_dump() to check what has been submitted. The best one would be:
var_dump($_REQUEST);
var_dump($_POST);
var_dump($_GET);
Network Tab
If you are using Chrome or Firebug, you can use the Developer Tools' Network tab to check the FORM Data.
Firebug
Chrome
Unrelated Note: You have <select> tags with same id, which is totally wrong, but that doesn't affect it. Checking using the PHP way, will work for you.
You can use a default value, such as an empty string:
<?php
if(!empty($_POST["select"])) {
echo "submit";
} elseif(!empty($_POST["select2"])) {
echo "submit2";
}
?>
<form id="form1" name="form1" method="post">
<select name="select" id="select" onChange="this.form.submit()">
<option value="">Choose</option>
<option value="1a">Select a</option>
<option value="1b">Select b</option>
</select>
<select name="select2" id="select2" onChange="this.form.submit()">
<option value="">Choose</option>
<option value="2a">Select2 a</option>
<option value="2b">Select2 b</option>
</select>
</form>
Try This:
Your HTML (Adding one more option with 0 value):
<form id="form1" name="form1" method="post">
<select name="select" id="select" onChange="this.form.submit()">
<option value="0">Select</option>
<option value="1">Select a</option>
<option value="1">Select b</option>
</select>
<select name="select2" id="select" onChange="this.form.submit()">
<option value="0">Select</option>
<option value="2">Select2 a</option>
<option value="2">Select2 b</option>
</select>
</form>
Your PHP Code:
if($_POST['select'] > 0){
echo "submit";
}
if($_POST['select2'] > 0){
echo "submit2";
}
I have a form in which values get inserted and passed to PHP, everything was working till I thought about inserting a select value, and this one always gets passed on to the database as 0. This is the form:
<form name="myForm" id="register" action="register.php" method="post" accept-charset="utf-8">
<select name="Year" form="register">
<option value="year1">1</option>
<option value="year2">2</option>
<option value="year3">3</option>
</select>
This is how I retrieve all my variables, but this select one doesn't seem to work.
PHP:
$value7 = mysql_real_escape_string($_POST['Year']);
Thank you!
Change this
<option value="year1">1</option>
to this
<option value="1">year1</option>
PHP Code
<?php
if(isset($_POST))
echo mysql_real_escape_string($_POST['Year']);
?>
HTML code
<select name="Year" form="register">
<option value="1">year1</option>
<option value="2">year2</option>
<option value="3">year3</option>
</select>
<input type='submit' >
</form>
change this
<select name="Year" form="register">
<option value="year1">1</option>
<option value="year2">2</option>
<option value="year3">3</option>
</select>
to
<select name="Year" form="register">
<option value="1">year1</option>
<option value="2">year2</option>
<option value="3">year3</option>
</select>
I've been trying to post the value of two selected dropdown menu's using the SESSIONS method, however there seems to be an issue because when submitted the second page displays the PHP code only. Here are the relevant snippets of code:
shop.php
<form action="processing.php" method="post"> <div class="item">
<span class="caption">Essential Gray Suit<br />£459</span>
<select name="size1" class="size" id="size1" onchange="showDiv(this)">
<optgroup label="Size">
<option value="0">SELECT SIZE</option>
<option value="1">XS</option>
<option value="2">S</option>
<option value="3">M</option>
<option value="4">L</option>
<option value="5">XL</option>
</select>
<div id="quantity1" style="display: none;">
<select name="quantity1" class="size" onchange="showCart(this)">
<optgroup label="Quantity">
<option value="0">SELECT QUANTITY</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
</select>
</div>
<input type="submit" value="Submit" name="submit" id="submit">
</form>
processing.php
<?php
session_start();
if (isset($_POST['submit'])) {
$_SESSION['size1'] = $_POST['size1'];
}
echo $_SESSION['size1'];
?>
I've edited the code. Is it right now?
<form action="" method="POST">
<select name="event">
<option value="" disabled="disabled" selected="selected">Please select a Make</option>
<option value="1">Honda</option>
<option value="2">Volkswagen</option>
</select>
<select name="event2">
<option value="" disabled="disabled" selected="selected">Please select a Model</option>
<option value="1">Civic</option>
<option value="2">Passat</option>
</select>
<input type="submit" value="Submit">
</form>
Try this:
<form action="" method="POST" name ="testfrm" id="testfrm">
<select name="event">
<option value="" disabled="disabled" selected="selected">Please select a Make</option>
<option value="1">Honda</option>
<option value="2">Volkswagen</option>
</select>
<select name="event2">
<option value="" disabled="disabled" selected="selected">Please select a Model</option>
<option value="1">Civic</option>
<option value="2">Passat</option>
</select>
<input type="submit" name="btnsbmt" id="btnsbmt" />
</form>
--
Thanks
<form action="" method="POST">
<select name="event">
<option value="" disabled="disabled" selected="selected">Please select a Make</option>
<option value="1">Honda</option>
<option value="2">Volkswagen</option>
</select>
<select name="event2">
<option value="" disabled="disabled" selected="selected">Please select a Model</option>
<option value="1">Civic</option>
<option value="2">Passat</option>
</select>
<input type="submit" value="submit" />
</form>
There you go, an amazing submit button!