Form submit Bootstrap Dropdown data - php

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"]; ?>

Related

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'];
}
?>

Invalid index even when value is selected

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>

select previously selected dropdown value on form self submit

I have a 'this year' dropdown with a form attached. I then select one of the years, like say 2016. When I click the submit button I want the page to be reloaded while the value in the dropdown stays at 2016 (the one that was previously selected).
How can I do that? Here's my form:
<form class="form" method="POST" id="myID" name="myName">
<label id="year" name="year">YEARS</label>
<select name="year" id="year" class="form-control">
<option value="0" selected disabled="">CHOOSE YEAR</option>
<?php foreach($year as $rows){?>
<option value="<?php echo $rows->years?>"><?php echo $rows->years?>
</option>
<?php }?>
</select>
<button id="filter_button" style="margin-top: 26px" name="filter_button"
type="submit" class="for btn btn-info">Show</button>
</form>
You should use the value from $_POST to set the selected as default. Below is the updated code:
<form class="form" method="POST" id="myID" name="myName">
<label id="year" name="year">YEARS</label>
<select name="year" id="year" class="form-control">
<option value="0" selected disabled="">CHOOSE YEAR</option>
<?php foreach($year as $rows){
$isSelected = (isset($_POST['year']) && ($_POST['year'] == $rows->years)) ? 'selected' : '';
?>
<option <?php echo $isSelected; ?> value="<?php echo $rows->years?>"><?php echo $rows->years?>
</option>
<?php }?>
</select>
<button id="filter_button" style="margin-top: 26px" name="filter_button"
type="submit" class="for btn btn-info">Show</button>
</form>

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.

Printing a value in textbox when clicked view after selection

I have a page that selects value from different tables, when I select a class corresponding divisions are displayed (I used javascript) but when I click the view button I want all the values selected to be displayed. I am done with all the value including class, but corresponding division doesnot get displayed. since I am not much eficient in php, cant find out how, plz do help.
Here is my code that displays class in the textbox when selected.
<div class="field">
Class:
<div id="combo" style="width:20px;">
<select name="class" id="class" onChange="getDivision(this.value)">
<option value="">Select</option>
<?php
$select3="select * from ".TABLE_CLASS."";
$res=mysql_query($select3);
while($row=mysql_fetch_array($res)) {
?>
<option value="<?php echo $row['ID']?>" <?php if($row['ID']==#$_POST['class']){ ?> selected="selected"<?php }?>><?php echo $row['class'];?></option>
<?php
}
?>
</select>
</div>
</div>
<!--<div id="aa"></div>-->
<div class="field">
Division:
<div id="combo" style="width:20px;">
<select name="division" id="division" >
<!--<option value="">Select</option>-->
<option value="<?php echo $row['division'];?>"></option>
</select>
</div>
<!--<input type="text" name="a" id="a">-->
</div>
</div> <!-- /login-fields -->
<option value="<?php echo $row['division'];?>"></option>
You got nothing in those line between option tag. Put some thing there i am using $row['division']. just to show you that you are missing that
<option value="<?php echo $row['division'];?>"><?php echo $row['division'];?></option>

Categories