Invalid index even when value is selected - php

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>

Related

Form submit Bootstrap Dropdown data

I have the following code, I am trying to make it so that the selected field displays on a php page when I return that hit the submit button, I'm doing this below.
<form action="welcome.php" method="post">
<div class="form-group">
<select class="form-control" id="inputState" style="width: 100%">
<option selected>
Choose...
</option>
<option name="account" value="1">
Minecraft Account
</option>
<option name="cape" value="2">
Minecraft Cape
</option>
</select>
</div>
<input type="submit">
</form>
this is my php echo code, but it does not work
<?php echo $_POST["account"]; ?>
<?php echo $_POST["cape"]; ?>
Set name to your select control
<select name="inputState" class="form-control"
Then you can get the "value" of selected item
<?php echo $_POST["inputState"]; ?>

Pass value of html to PHP variable

I'm having some trouble passing the value of some HTML elements to PHP variable. I have a PHP web app where I give the users some drop down option. What I want to do is after the user has selected the option I give than to click on a button and make some calculations based on his/her selection.
I have tried using the POST and GET method but because I'm new to php I probably did it wrong. I also try with ids but no results.
Here is the HTML code
The 1st dropdown menu :
<div>
<select id="kleidwma" onchange="kleidwmata_change(this.value);kwdikos();ypsos();"
class="form-control selectpicker kleidwmata" data-size="10" data-
style="btn-white" name="guard_kleidwmata" required >
<option value="" > </option>
<option value="12">12 MS</option>
<option value="14">14 KETE</option>
<option value="15">15 ARRIKTON</option>
<option value="16">16 KETE</option>
<option value="22">22 KETE</option>
</select>
</div>
The 2nd dropdown menu :
<div>
<select id="sigkratisi" onchange="kwdikos()" name="sigkratisi"
class="form-control sigkratisi" required>
<option value="" ></option>
<option value="metalliki" >Μεταλλική</option>
<option value="xilogonia" >Ξυλογωνία</option>
</select>
</div>
Here is the button to be clicked after the selection has been made :
<div class="row" style="margin-top:20px; margin-left: 235px;">
<div class="col-md-7">
<input type="hidden" name="tmp_calculate" value="pending" />
<button onclick="calculate()" type="button" class="btn btn-success" >
<?php echo "Υπολογισμός";?></button>
</div>
</div>
And finally here is the function that is called :
function calculate()
{
$var1 = sigkratisi;
$var2 = kleidwma;
"code to be executed base on the var1 and var2"
}
I hope it's clear enough.
Thanks in advance for your time
you can use Forms
<form name="f" action="" method="POST">
<div>
<select id="kleidwma" name="variable"
class="form-control selectpicker kleidwmata" data-size="10" data-
style="btn-white" name="guard_kleidwmata" required >
<option value="" > </option>
<option value="12">12 MS</option>
<option value="14">14 KETE</option>
<option value="15">15 ARRIKTON</option>
<option value="16">16 KETE</option>
<option value="22">22 KETE</option>
</select>
<input type="submit" value="submit" />
</div>
</form>
in PHP
<?php
if(isset($_POST['variable']))
{
echo $_POST['variable'];
}
?>

trying to select multiple answers in a from by PHP

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.
:)

Dependent dropdown getting ids not name

I am trying to get user selection from php form but instead of name I am getting id of elements. instead of ids I want to get names. Please help
here is the url
http://efinancec.com/d/drop4/index.php
and here is the code
<form action="../form/form1.php" method="post" enctype="multipart/form-data">
<div class="frmDronpDown">
<div class="row">
<label>Training:</label><br/>
<select name="training" id="training-list" class="demoInputBox" onChange="getCourse(this.value);">
<option value="">Select Training</option>
<?php
foreach($results as $training) {
?>
<option value="<?php echo $training["id"]; ?>"><?php echo $training["training"]; ?></option>
<?php
}
?>
</select>
</div>
<div class="row">
<label>Course:</label><br/>
<select name="course" id="course-list" class="demoInputBox" onChange="getCountry(this.value);">
<option value="">Select Course</option>
</select>
</div>
<div class="row">
<label>Country:</label><br/>
<select name="country" id="country-list" class="demoInputBox" onChange="getCity(this.value);">
<option value="">Select Country</option>
</select>
</div>
<div class="row">
<label>City:</label><br/>
<select name="city" id="city-list" class="demoInputBox" onChange="getDates(this.value);">
<option value="">Select City</option>
</select>
</div>
<div class="row">
<label>Dates:</label><br/>
<select name="dates" id="dates-list" class="demoInputBox" onChange="getPrice(this.value);">
<option value="">Select Dates</option>
</select>
</div>
<div class="row">
<label>Online Onsite Price:</label><br/>
<select name="price" id="price-list" class="demoInputBox">
<option value="">Select Online or Onsite</option>
</select>
</div>
<input id="submit" name="submit" type="submit" value="Submit">
</form>
and this is how I am getting the values from sql database
<?php
require_once("dbcontroller.php");
$db_handle = new DBController();
if(!empty($_POST["training_id"])) {
$query ="SELECT * FROM course WHERE training_id = '" . $_POST["training_id"] . "'";
$results = $db_handle->runQuery($query);
?>
<option value="">Select Course</option>
<?php
foreach($results as $course) {
?>
<option value="<?php echo $course["id"]; ?>"><?php echo $course["course"]; ?></option>
<?php
}
}
?>
Change this line:
onChange="getCourse(this.value);">
To this :
onChange="getCourse(this.text);">
You are passing id of the course as the value of dropdown and calling that value which is fine but you need to call the text of select option to get the course name.
I changed this.value to this.text as suggested and created the new page and it stoped working at all now it is not pulling the records from database except the first one. Here is the link
http://efinancec.com/d/drop4/index1.php
where as my earlier page is still working fine.
http://efinancec.com/d/drop4/index.php
I think I could not convey my problem earlier. My depended dropdown is working fine but when a user submits the form in the email I receive the ids instead of name. Thats the problem.
For example I myself made some selections on index.php and submitted form Here is the email I got
training: 1
course : 101
country: 1001
city: 5005
Basically I am getting the the respective ids instead of name.

How to get multiple selected values of select box in php?

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"

Categories