This question already has answers here:
How to get multiple selected values of select box in php?
(11 answers)
Closed 8 years ago.
i need to store multiple elements of listbox item into the database , but i am unable to pass the elements from the html form to php. Please help
<html>
<head>
</head>
<body>
<form method="post" action="Save.php">
<select name="country[]" multiple="multiple">
<option value="Belgium">Belgium</option>
<option value="France">France</option>
<option value="Germany">Germany</option>
<option value="Holland">Holland</option>
<option value="Greece">Greece</option>
</select>
<input type="Submit" value="Submit" />
</form>
</body>
</html>
<?php
if(isset($_POST['submit']))
{
$con = $_POST['country'];
foreach($con as $selected)
{
echo 'selected'.$selected;
}
?>
This code shall solve your issue
<?php
if(isset($_POST['submit']))
{
if (isset($_POST['country'])){
$con = $_POST['country'];
foreach($con as $selected)
echo 'selected'.$selected;
}
}
?>
<html>
<head>
</head>
<body>
<form method="post" action="Save.php">
<select name="country[]" multiple="multiple">
<option value="Belgium">Belgium</option>
<option value="France">France</option>
<option value="Germany">Germany</option>
<option value="Holland">Holland</option>
<option value="Greece">Greece</option>
</select>
<input type="Submit" value="Submit" name="submit" />
</form>
</body>
</html>
You don't have a POST index of submit hence your isset() is failing. You are also missing a closing brace for your if statement in your code example. I've modified your example to use the $_POST['country'] for the isset(). This bit worked for me as far as returning selected values goes:
<html>
<head>
</head>
<body>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<select name="country[]" multiple="multiple">
<option value="Belgium">Belgium</option>
<option value="France">France</option>
<option value="Germany">Germany</option>
<option value="Holland">Holland</option>
<option value="Greece">Greece</option>
</select>
<input type="Submit" value="Submit" />
</form>
</body>
</html>
<?php
if(isset($_POST['country']))
{
$con = $_POST['country'];
foreach($con as $selected)
{
echo 'selected'.$selected;
}
}
?>
//the post request takes the Tag NAMES only
<input type="Submit" value="Submit" name="submit"/>
see this phpfiddle it is working fine only
Related
The following code is the HTML from a file that I am trying to out put multiple selections by clicking the submit button:
<form id="eg6b" action="example-6.php" method="post">
<p>
<label for="sport">Favourite sport: </label>
<select id="sport" name="favsport []" size="4" multiple>
<option value="soccer">Soccer</option>
<option value="cricket">Cricket</option>
<option value="squash">Squash</option>
<option value="golf">Golf</option>
<option value="tennis">Tennis</option>
<option value="basketball">Basketball</option>
<option value="baseball">Baseball</option>
</select>
<input type="submit" value="submit"></p>
</form>
The following code is the PHP file to obtain multiple Sports:
<?php
foreach($_POST["favsport"] as $val) {
echo "<p>You chose $val </p>";
}
?>
I just cant find the error. If I run this code it gives me an error that the favsport is undefined and that the argument supplied to the foreach loop is invalid. I have messed around with it alot but now I am just tired.
Change favsport [] to favsport[].
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<form id="eg6b" action="test.php" method="post">
<p>
<label for="sport">Favourite sport: </label>
<select id="sport" name="favsport[]" size="4" multiple>
<option value="soccer">Soccer</option>
<option value="cricket">Cricket</option>
<option value="squash">Squash</option>
<option value="golf">Golf</option>
<option value="tennis">Tennis</option>
<option value="basketball">Basketball</option>
<option value="baseball">Baseball</option>
</select>
<input type="submit" value="submit"></p>
</form>
</body>
</html>
<?php
foreach($_POST["favsport"] as $val) {
echo "<p>You chose $val </p>";
}
?>
You have only little typo mistake as far i can see which is at below like
<select id="sport" name="favsport []" size="4" multiple>
replace above line with below line
<select id="sport" name="favsport[]" size="4" multiple>
ERROR:
The error is in the name attribute which is having extra white space like
name="favsport**here you having one space** []" and it must be like name="favsport[]" .
Hope this will solve your problem.
:)
I want to pass Drop Down value to php, i am new to PHP please help
<html>
<head>
<script>
function displayVals() {
var singleValues = $("#account").val();
$("p").html("<b>Single:</b> " + singleValues);
}
</script>
<select id="account" name="account">
<option value="1">Tes1</option>
<option value="2">Tes2</option>
</select>
</head>
<body>
<?php
$account = $_get['account'];
echo $account;
?>
</body>
</html>
Hi, I want to pass Drop Down value to php, i am new to PHP please help
<?php
if (isset($_POST['submit'])) {
echo $_POST['account'];
}
?>
<form action="" method="POST">
<select id="account" name="account">
<option value="1">Tes1</option>
<option value="2">Tes2</option>
</select>
<input type="submit" name="submit"/>
</form>
You need to make a form
Here is the solution for you,
<?php
if (isset($_REQUEST['save'])) {
$sql = "SELECT Business_Unit, COUNT(Joining_Month) AS 'A' FROM jl WHERE Business_Unit = '".$_REQUEST['opt']."' GROUP BY Business_Unit";
// Here is your sql query
echo $sql;exit();
}
?>
Yor HTML code:
<form method="post" action="">
<select name="opt">
<option value="1">Option 1</option>
<option value="2">Option 2</option>
<option value="3">Option 3</option>
<option value="4">Option 4</option>
</select>
<input type="submit" name="save" />
</form>
Given simple working demo to fulfill your requirement. Hope it will help you.
I'm trying to echo to the screen what a user selects from a dropdown menu. So whenever the user selects a number, it updates the text below it in to display what they selected. Here's my code below:
<html>
<head>
</head>
<body>
<label for="numbers"></label>
<form action="drop.php">
<select name="numbers">
<option value="0">select number</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<br><br>
<input type="submit">
</form>
<?php
if(isset($_POST['numbers']))
{
$numbers = $_POST['numbers'];
$error = "?";
echo "<p>" . $numbers . "</p>";
}
?>
</body>
</html>
UPDATED MAIN CODE
Try method="post" on form declaration. or use $_GET instead of $_POST ON drop.php
if(isset($_POST['numbers']))
I suggest reading PHP's Dealing with Forms tutorial and also researching the change event.
For your code example, I've added a form element along with an "onchange" attribute to the select element which will automatically submit the form when a number is chosen.
<html>
<head>
</head>
<body>
<form action="<?php echo basename(__FILE__); ?>" method="post">
<label for="numbers">Numbers:</label>
<select name="numbers" onchange="document.forms[0].submit();">
<option value="0">select number</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<input type="submit">
</form>
<?php
if (array_key_exists('numbers', $_POST)) {
$numbers = $_POST['numbers'];
echo '<p>' . $numbers . '</p>';
}
?>
</body>
</html>
Add method="post" to the form
<form action="drop.php" method="post">
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 thinks this is pretty easy but yet can't do it for the sake of my life. Is this too much to ask for?
<select name="cars">
<?php
if (isset($_POST['Submit1'])) {
$car = $_POST['cars'];
}
?>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="opel">Opel</option>
<option value="audi">Audi</option>
</select>
</body>
</head>
<body>
<Form name ="form1" Method ="POST" Action ="">
<Input Type = "text" Value ="<?php echo $car; ?>" Name ="word">
<Input Type = "Submit" Name = "Submit1" Value = "Submit">
</FORM>
The <select> tag is an HTML form element and in order to be submitted that tag would have to be inside the <form> tag.
Your new code should look like this:
<body>
<form name="form1" method="POST" action="">
<select name="cars">
<?php
if (isset($_POST['Submit1'])) {
$car = $_POST['cars'];
}
?>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="opel">Opel</option>
<option value="audi">Audi</option>
</select>
<input type="text" value="<?php echo $car; ?>" name="word" />
<input type="Submit" name="Submit1" value="Submit" />
</form>
</body>
Let me know if you have any other questions.