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.
:)
Related
I'm writing a PHP/HTML page that accesses a local SQLite database and displays/alters information from that database. I was having trouble connecting to the database, so I followed the instructions here and now it SEEMS to be connecting. Now the issue I'm having is that when I run the code, select an option from the dropdown menu, and click submit, I get the following errors:
Notice: Undefined index: formFlowerNameQuery in C:\Users\Aubrey\PhpstormProjects\Assignment5\src\main.php on line 45
Fatal error: Call to a member function query() on null in C:\Users\Aubrey\PhpstormProjects\Assignment5\src\main.php on line 46
I have no idea why the first error is occurring, as I have selected an option prior to hitting the submit button. The second one is confusing me as well, because the database shouldn't be null. Here's the HTML and PHP I'm running. This is also my first experience with both languages, so it may be a little hacked together from things I've found from googling what I want to do. The error is in respect to the first section named "Latest Sightings."
<!DOCTYPE html>
<html>
<head>
<title>Assignment 5</title>
</head>
<?php
global $database;
function openDatabase(){
try{
if($this->database==null){
$this->database =new PDO("sqlite:flowers.db","","",array(
PDO::ATTR_PERSISTENT => true
));
}
return $this->database;
}catch(PDOException $e){
print "Error: ".$e->getMessage();
}
}
?>
<body>
<header>
<h1>SSWC Database</h1>
</header>
<hr noshade width=75% align=left>
<h3>Latest Sightings</h3>
<p>To view the last 10 sightings of a flower, select it from the dropdown menu below and click submit.<p>
<p>
<select name="formFlowerNameQuery">
<option value="">Select Flower...</option>
<option value="Draperia">Draperia</option>
<option value="California flannelbush">California Flannelbush</option>
<option value="Sheltons violet">Sheltons violet</option>
</select>
</p>
<form action="main.php" method="post">
<input type="submit" name="submitQuery" value="Submit" />
</form>
<?php
if(isset($_POST['submitQuery'])){
querySubmission($database);
}
function querySubmission($database){
$varFlowerName = $_POST['formFlowerNameQuery'];
$result = $database->query("SELECT PERSON, LOCATION, SIGHTED
FROM SIGHTINGS
WHERE NAME = $varFlowerName
ORDER BY SIGHTED DESC
LIMIT 10");
while($row = $result->fetchArray(SQLITE3_ASSOC)){
echo "NAME: " . $row['NAME'] . "\n";
echo "PERSON: " . $row['PERSON'] . "\n";
echo "LOCATION: " . $row['LOCATION'] . "\n";
echo "SIGHTED ON: " . $row['SIGHTED'] . "\n";
}
}
?>
<hr noshade width=75% align=left>
<h3>Update Flower Information</h3>
<p>To update information on a specific flower, select the flower from the dropdown menu <br />
and input the new information into the provided text boxes. Click submit to update.</p>
<p>
<select name="formFlowerNameUpdate">
<option value="">Select Flower...</option>
<option value="Draperia">Draperia</option>
<option value="California flannelbush">California Flannelbush</option>
<option value="Sheltons violet">Sheltons violet</option>
</select>
</p>
<form>
Genus:<br>
<input type="text" name="genus"><br>
Species:<br>
<input type="text" name="species"><br>
Common Name<br>
<input type="text" name="comname"><br>
<br />
</form>
<button name="submitUpdate">Submit</button>
<hr noshade width=75% align=left>
<h3>Add Sighting</h3>
<p>To add a new flower sighting to the database, select the flower from the drop down menu <br />
and input the sighting information. Click submit to add the sighting to the database.</p>
<p>
<select name="formFlowerNameInsert">
<option value="">Select Flower...</option>
<option value="Draperia">Draperia</option>
<option value="California flannelbush">California Flannelbush</option>
<option value="Sheltons violet">Sheltons violet</option>
</select>
</p>
<p>
<select name="formPerson">
<option value="">Select Person...</option>
<option value="Jennifer">Jennifer</option>
<option value="Maria">Maria</option>
<option value="Michael">Michael</option>
</select>
</p>
<p>
<select name="formLocation">
<option value="">Select Location...</option>
<option value="Scodie Mountains">Scodie Mountains</option>
<option value="Grouse Meadow">Grouse Meadow</option>
<option value="Steve Spring">Steve Spring</option>
</select>
</p>
<p>
<input type="date" name="dateSighted">
</p>
<button name="submitInsert">Submit</button>
</body>
</html>
EDIT: Both errors have been fixed by following steps in #Phil's and #IncredibleHat's comments below.
that is because you put tne select field outside your form
<p>
<select name="formFlowerNameQuery">
<option value="">Select Flower...</option>
<option value="Draperia">Draperia</option>
<option value="California flannelbush">California Flannelbush</option>
<option value="Sheltons violet">Sheltons violet</option>
</select>
</p>
<form action="main.php" method="post">
<input type="submit" name="submitQuery" value="Submit" />
</form>
change it to
<form action="main.php" method="post">
<p>
<select name="formFlowerNameQuery">
<option value="">Select Flower...</option>
<option value="Draperia">Draperia</option>
<option value="California flannelbush">California Flannelbush</option>
<option value="Sheltons violet">Sheltons violet</option>
</select>
</p>
<input type="submit" name="submitQuery" value="Submit" />
</form>
I have the following form but would like to add a condition so that only managers can see 'clear' option.
<form action="vision_ref.php" method="post">
With selected:
<select name='multi'>
<option value='now'>Set next action time to now</option>
<option value='clear'>Remove the ticket</option>
<option value='unassign'>Unassign Specialist</option>
<option value='priority'>Toggle Priority</option>
</select>
<input type="submit" value="Go!">
</form>
Something like: if(havePriv('grp_mgr'))
<?php if(havePriv('grp_mgr')) : ?>
<option value='clear'>Remove the ticket</option>
<?php endif; ?>
Does the trick
You can do this:
<form action="vision_ref.php" method="post">
With selected:
<select name='multi'>
<option value='now'>Set next action time to now</option>
<?php if(havePriv('grp_mgr')){ ?>
<option value='clear'>Remove the ticket</option>
<?php } ?>
<option value='unassign'>Unassign Specialist</option>
<option value='priority'>Toggle Priority</option>
</select>
<input type="submit" value="Go!">
</form>
So "Remove the ticket" will only displayed if the condition is true.
You can say :
if(havePriv('grp_mgr'))
{
echo "<option value='clear'>Remove the ticket</option>";
}
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">
This form contains many other inputs that work fine like <input>, <select> (not multiple) etc...I cannot figure out where exactly my problem is when I use <select multiple. I have the following code:
<form action="phpaction.php" method="post">
<select name="states[]" multiple>
<option value="AL">Alabama</option>
<option value="AK">Alaska</option>
...
</select>
// phpaction.php file
$statesArray=$_POST['states'];
print_r ($statesArray);
Why is not it working? I get the following error: Undefined index: states in phpaction.php on line 72
Try this:
<?php
if ($_SERVER['REQUEST_METHOD']==='POST') {
$statesArray=$_POST['states'];
print_r($statesArray);
}
?>
<form action="" method="post">
<select name="states[]" multiple>
<option value="AL">Alabama</option>
<option value="AK">Alaska</option>
</select>
<input type="submit">
</form>
I have a html form which has a select list box from which you can select multiple values because its multiple property is set to multiple. Consider form method is 'GET'. The html code for the form is as follows:
<html>
<head>
<title>Untitled Document</title>
</head>
<body>
<form id="form1" name="form1" method="get" action="display.php">
<table width="300" border="1">
<tr>
<td><label>Multiple Selection </label> </td>
<td><select name="select2" size="3" multiple="multiple" tabindex="1">
<option value="11">eleven</option>
<option value="12">twelve</option>
<option value="13">thirette</option>
<option value="14">fourteen</option>
<option value="15">fifteen</option>
<option value="16">sixteen</option>
<option value="17">seventeen</option>
<option value="18">eighteen</option>
<option value="19">nineteen</option>
<option value="20">twenty</option>
</select>
</td>
</tr>
<tr>
<td> </td>
<td><input type="submit" name="Submit" value="Submit" tabindex="2" /></td>
</tr>
</table>
</form>
</body>
</html>
I want to display the selected values in select list box on display.php page. So how are the selected values accessed on display.php page using $_GET[] array.
If you want PHP to treat $_GET['select2'] as an array of options just add square brackets to the name of the select element like this: <select name="select2[]" multiple …
Then you can acces the array in your PHP script
<?php
header("Content-Type: text/plain");
foreach ($_GET['select2'] as $selectedOption)
echo $selectedOption."\n";
$_GET may be substituted by $_POST depending on the <form method="…" value.
Change:
<select name="select2" ...
To:
<select name="select2[]" ...
You can use this code to retrieve values from multiple select combo box
HTML:
<form action="c3.php" method="post">
<select name="ary[]" multiple="multiple">
<option value="Option 1" >Option 1</option>
<option value="Option 2">Option 2</option>
<option value="Option 3">Option 3</option>
<option value="Option 4">Option 4</option>
<option value="Option 5">Option 5</option>
</select>
<input type="submit">
</form>
PHP:
<?php
$values = $_POST['ary'];
foreach ($values as $a){
echo $a;
}
?>
Use the following program for select the multiple values from select box.
multi.php
<?php
print <<<_HTML_
<html>
<body>
<form method="post" action="value.php">
<select name="flower[ ]" multiple>
<option value="flower">FLOWER</option>
<option value="rose">ROSE</option>
<option value="lilly">LILLY</option>
<option value="jasmine">JASMINE</option>
<option value="lotus">LOTUS</option>
<option value="tulips">TULIPS</option>
</select>
<input type="submit" name="submit" value=Submit>
</form>
</body>
</html>
_HTML_
?>
value.php
<?php
foreach ($_POST['flower'] as $names)
{
print "You are selected $names<br/>";
}
?>
<html>
<head>
<title>Untitled Document</title>
</head>
<body>
<form id="form1" name="form1" method="get" action="display.php">
<table width="300" border="1">
<tr>
<td><label>Multiple Selection </label> </td>
<td><select name="select2[]" size="3" multiple="multiple" tabindex="1">
<option value="11">eleven</option>
<option value="12">twelve</option>
<option value="13">thirette</option>
<option value="14">fourteen</option>
<option value="15">fifteen</option>
<option value="16">sixteen</option>
<option value="17">seventeen</option>
<option value="18">eighteen</option>
<option value="19">nineteen</option>
<option value="20">twenty</option>
</select>
</td>
</tr>
<tr>
<td> </td>
<td><input type="submit" name="Submit" value="Submit" tabindex="2" /></td>
</tr>
</table>
</form>
</body>
</html>
You can iterate it directly like this
foreach ($_GET['select2'] as $value)
echo $value."\n";
or you can do it like this
$selectvalue=$_GET['select2'];
foreach ($selectvalue as $value)
echo $value."\n";
This will display the selected values:
<?php
if ($_POST) {
foreach($_POST['select2'] as $selected) {
echo $selected."<br>";
}
}
?>
// CHANGE name="select2" TO name="select2[]" THEN
<?php
$mySelection = $_GET['select2'];
$nSelection = count($MySelection);
for($i=0; $i < $nSelection; $i++)
{
$numberVal = $MySelection[$i];
if ($numberVal == "11"){
echo("Eleven");
}
else if ($numberVal == "12"){
echo("Twelve");
}
...
...
}
?>
You could do like this too. It worked out for me.
<form action="ResultsDulith.php" id="intermediate" name="inputMachine[]" multiple="multiple" method="post">
<select id="selectDuration" name="selectDuration[]" multiple="multiple">
<option value="1 WEEK" >Last 1 Week</option>
<option value="2 WEEK" >Last 2 Week </option>
<option value="3 WEEK" >Last 3 Week</option>
<option value="4 WEEK" >Last 4 Week</option>
<option value="5 WEEK" >Last 5 Week</option>
<option value="6 WEEK" >Last 6 Week</option>
</select>
<input type="submit"/>
</form>
Then take the multiple selection from following PHP code below. It print the selected multiple values accordingly.
$shift=$_POST['selectDuration'];
print_r($shift);
I fix my problem with javascript + HTML. First i check selected options and save its in a hidden field of my form:
for(i=0; i < form.select.options.length; i++)
if (form.select.options[i].selected)
form.hidden.value += form.select.options[i].value;
Next, i get by post that field and get all the string ;-)
I hope it'll be work for somebody more. Thanks to all.
foreach ($_POST["select2"] as $selectedOption)
{
echo $selectedOption."\n";
}
form submit with enctype="multipart/form-data"