no changes in my php page - php

I do not understand why nothing changes in my php page when I make changes in my drop down menu.
This is the page where I test it.
http://php.aryapratinidhisabha.org.uk/excercise4.php
please see the code here
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<table>
<tr>
<td> <label for=”font”> Select Font: </label> </td>
<td> <select id=”font” name=”font”>
<option value=”Verdana”> Verdana </option>
<option value=”Arial”> Arial </option>
<option value=”Times New Roman”> Times New Roman </option>
</select>
</td>
</tr> <tr>
<td> <label for=”size”> Select Size: </label> </td>
<td> <select id=”size” name=”size”>
<option value=”10px”> 10px </option>
<option value=”12px”> 12px </option>
<option value=”16px”> 16px </option>
<option value=”20px”> 20px </option>
</select>
</td>
</tr> <tr>
<td> <label for=”color”> Select Color: </label> </td>
<td> <select id=”size” name=”size”>
<option value=”black”> black </option>
<option value=”green”> green </option>
<option value=”purple”> purple </option>
<option value=”red”> red </option>
</select>
</td>
</tr>
</form>
$_SESSION['font']=$_POST['font'];
$_SESSION['size']=$_POST['size'];
$_SESSION['color']=$_POST['color'];
<p <?php
echo ' style=”font-family: ' . $_SESSION['font'] . '; ';
echo 'font-size: ' . $_SESSION['size'] . '; ';
echo 'color: ' . $_SESSION['color']. ';" ';
?>> Text to display </p>

One, if you are not going to use jQuery or javascript, you will need to submit your form with a button or input type=submit
e.g.:
<button type="submit" >Submit</button>
or
<input type="submit" />
Also, php code that your placing within the html needs to be placed between php tags:
<html>
...
<?php
// php code here
?>
(specifically talking about the: $_SESSION globals).
Additionally, as was brought up in the comments on your question, you are going to need <form> and also to tell it where the form is submitting to.
<form method="post" action="page.php">
The method can also be get and you can also submit to the same page by leaving action="" blank. Up to you.

Related

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>

How to navigate different pages according to selected item in combo box when submit button is clicked in php

I have two combo boxes one is Category and the other one is City,according to what i select from combo box i need to navigate to different pages when submit button is clicked.
ex: If the selected item in combo box is Cars I need to navigate to cars.html page if the selected item is Bikes I need to navigate to bike.html page when submit button is clicked.
This is a part of my code,
<form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post">
<div id="absolute">
<h1><b>Select a category & a City </b></h1>
<table class="spacingTable" width="600">
<tr>
<td><label name="category">Category</label></td>
<td>
<select id="cmb1" name="Category">
<option value="select">Please select a Category</option>
<option value="Cars">Cars</option>
<option value="mbikes">Motorbikes & Scooters</option>
<option value="threewheelers">Three Wheelers</option>
<option value="van">Vans & Busses</option>
<option value="hvd">Heavy-Duty vehicles</option>
</select></td>
</tr>
<tr>
<td><label name="City">City</label></td>
<td>
<input type="text" id="textbox1" name="city">
</td>
<td>
<div id="div-ok">
<input type="submit" name="Submit" value="Ok">
</div></td>
</tr>
</table>
</div>
</form>
</body>
this is php code snippet
<?php
if(isset($_POST['Submit'])){
if(!empty($_POST['Category'])){
// redirect the user to cars.html or mbikes.html
header("Location: " . $_POST['Category'] . ".html");
exit();
}
}
?>
In a nutshell you can do something like this:
<form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post">
<table>
<tr>
<td>
<select name="category">
<option value="cars">Cars</option>
<option value="mbikes">Motorbikes & Scooters</option>
</select>
</td>
<td>
<div id="div-ok">
<input type="submit" name="Submit" value="Ok">
</div>
</td>
</tr>
</table>
</form>
And process the form like this:
if(isset($_POST['Submit'])){
if(!empty($_POST['category'])){
// redirect the user to cars.html or mbikes.html
header("Location: " . $_POST['category'] . ".html");
exit();
}
}
Here are the relevant references:
header()
exit()
Javascript solution:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<form action="#">
<table>
<tr>
<td>
<select name="category" id="category">
<option value="cars">Cars</option>
<option value="mbikes">Motorbikes & Scooters</option>
</select>
</td>
<td>
<div id="div-ok">
<input type="submit" value="Ok" id="goTo">
</div>
</td>
</tr>
</table>
</form>
<script>
document.getElementById('goTo').onclick = function(e){
e.preventDefault();
var page = document.getElementById('category').value,
extension = '.html';
window.location = category + extension;
}
</script>
$_SERVER["PHP_SELF"] is the filename of your form, so, when you submit the form, it points to the self page.
To make it work, you have to put the Rajdeep Paul's php code at the beginning of the form file, in top of everything else.
The result would look like this:
<?php
if(isset($_POST['Submit'])){
if(!empty($_POST['category'])){
// redirect the user to cars.html or mbikes.html
header("Location: " . $_POST['category'] . ".html");
exit();
}
}
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post">
<table>
<tr>
<td>
<select name="category">
<option value="cars">Cars</option>
<option value="mbikes">Motorbikes & Scooters</option>
</select>
</td>
<td>
<div id="div-ok">
<input type="submit" name="Submit" value="Ok">
</div>
</td>
</tr>
</table>
</form>
</body>
</html>
I just took Rajdeep Paul's code and edited it to be easier to you.

html table inside form, and getting values with php

<!DOCTYPE html>
<html lang="ro">
<head>
<title>MODIFICA</title>
</head>
<body>
<?php
$select1 = isset($_POST['select1']) ? $_POST['select1'] : '';
if($select1 == 'da'){echo 'ok';}
else { echo '!ok'; }
?>
<form method="POST">
<table border="1">
<thead>
<tr>
<th>Nume</th>
<th>Prenume</th>
<th>Adresa</th>
<th>Partid</th>
<th>Consiliul Local</th>
<th>Primar</th>
<th>Consiliul Judetean</th>
<th>Presedinte Consiliul Judetean</th>
<th>Senat</th>
<th>Deputat</th>
<th>Comisia Europarlamentara</th>
<th>Presedinte</th>
<th>Submit</th>
</tr>
</thead>
<tbody>
<?php
$stmt = $db->prepare('SELECT nume,prenume,adresa,partid,consiliul_local FROM candidati');
$stmt->execute();
while($row = $stmt->fetch(PDO::FETCH_OBJ)):?>
<tr>
<td><?php echo $row->nume;?></td>
<td><?php echo $row->prenume;?></td>
<td><?php echo $row->adresa;?></td>
<td><?php echo $row->partid;?></td>
<td>
<select name="select1">
<option value=""></option>
<option value="<?php echo $row->consiliul_local;?>">
<?php echo $row->consiliul_local;?>
</option>
</select>
</td>
<td>
<select name="select2">
<option value=""></option>
<option value="da">DA</option>
</select>
</td>
<td>
<select name="select3">
<option value=""></option>
<option value="da">DA</option>
</select>
</td>
<td>
<select name="select4">
<option value=""></option>
<option value="da">DA</option>
</select>
</td>
<td>
<select name="select5">
<option value=""></option>
<option value="da">DA</option>
</select>
</td>
<td>
<select name="select6">
<option value=""></option>
<option value="da">DA</option>
</select>
</td>
<td>
<select name="select7">
<option value=""></option>
<option value="da">DA</option>
</select>
</td>
<td>
<select name="select8">
<option value=""></option>
<option value="da">DA</option>
</select>
</td>
<td>
<button type="submit" name="btn">SUBMIT</button>
</td>
</tr>
<?php endwhile;?>
</tbody>
</table>
</form>
</body>
</html
I just verify if the $select1 variable is set, and if it's the case, then if the value from "option" (line 44), is 'da', then display it.
The problem that I have is that value is not displayed accordingly. Anyone knows why, and what should I change in order to work?
Or, in simple words, for starting coding the page I need, firstly I just filled-in that "option" tag with the value from db (and yes, the value 'da' is presented in 'consiliul_local' field from db), and then I made a simple echo, above the "form", and if the value of the "select" is 'da', then it should display the message 'ok'. But it always displays the message placed on the else branch. I must specify that I must do the whole logic only with php, and not js or anything else.
Here is how my page looks like, and notice that selected 'da' in the dropdown, which is there because of the MySQL fetch (line 35 in pastebin).
http://www.2shared.com/photo/eXzJ7Glo/1_online.html
This is the page source, which says the value stored in db, is correctly inserted into that value attribute of the tag.
http://www.2shared.com/uploadComplete.jsp?sId=CACRC8H4n6GrF0jJ
So I select 'da' from that dropdown, then I press the button, and I expect the 'ok' message to be displayed. Any fix? Thanks so much!

html form send-mail.php get all items in select list not just selected items

In the code below, when I click submit, it will echo the Name and one of items in the select list, if an item is selected. How can I echo all of the items in the select list. None of the items will be selected. The items are just moved into the list to create a list of choices. Can I get un-selected items? Or, do I need to write a javascript to select all the items in the left box when the submit button is clicked. If that is what is needed I still don't know how to get multiple items, I think it might be with "implode" but I can't figure out the correct syntax.
========== code below ==========
<?php
if(isset($_POST['submit'])){
$name = $_POST['name'];
$colors = $_POST['colors'];
echo $name;
echo $colors;
}
?>
<!DOCTYPE html>
<head>
<title>Form submission</title>
</head>
<body>
<form action="" method="post">
<select name="colors" id="multiSelect" multiple="multiple">
<option value="Blue">Blue</option>
<option value="Green">Green</option>
<option value="Red">Red</option>
<option value="Yellow">Yellow</option>
</select>
Name: <input type="text" name="name"><br>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
=== below is a sample of the real form without the javascript ===
items are moved from the box on the right side to the box on the right side, when the form is submitted I want to receive a list of the items in the left box
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<table border="0" width="900px">
<tr><!-- total the option values added to the select list below-->
<td valign="top" width="155px"><select id="sourceSelect" size="29" multiple="" style="width:244px";">></select></td>
<td width="33px">
<button id="moveAdd" onclick="listboxMoveacross('destSelect', 'sourceSelect', this.id);"><<</button><br>
<button id="moveSubtract" onclick="listboxMoveacross('sourceSelect', 'destSelect', this.id);">>></button>
</td>
<td>
<select id="destSelect" size="29" multiple="multiple" onchange="showDescription(this);">
<option value="Blue Box" onclick="showDescription(this);">Temperature Minimum Time</option>
<option value="Yellow Bird" onclick="showDescription(this);">Temperature Maximum</option>
<option value="Red Car" onclick="showDescription(this);">Temperature Maximum Time</option>
<option value="Gree Stick" onclick="showDescription(this);">Wind Speed</option>
</select>
</td>
<td class="test155" width="" valign="top"><div id="putDescriptionHere"> </div></td>
</tr>
<tr>
<td class="tdCenter">
<button onclick="listboxMove('sourceSelect', 'up');">Move Up</button>
<button onclick="listboxMove('sourceSelect', 'down');">Move Down</button>
</td>
<td> </td><td> </td></tr>
</tbody></table>
</body>
</html>

Form with 3 dropdown menus

I have a form with three different dropdown menus, it looks like this:
<FORM METHOD="LINK" ACTION="/search.php" method="get">
<select style="width:55px;" name="filter_name">
<option> </option>
<option>R13</option>
<option>R14</option>
<option>R15</option>
<option>R16</option>
<option>R17</option>
</select></td></tr>
<tr><td width="40%">Plotis:</td><td colspan="2"><select style="width:55px;" name="">
<option> </option>
<option>165</option>
<option>175</option>
<option>185</option>
<option>195</option>
<option>205</option>
<option>215</option>
<option>225</option>
</select></td></tr>
<tr><td width="40%">Aukštis:</td><td colspan="2"><select style="width:55px;" name="">
<option> </option>
<option>75</option>
<option>70</option>
<option>65</option>
<option>60</option>
<option>55</option>
<option>50</option>
<option>45</option>
</select></td></tr>
<tr><td colspan="2" align="center">
<INPUT style="width:80px; height:25px; font-size:14px; font-weight:600; cursor:pointer;" TYPE="Submit" VALUE="Ieškoti">
</FORM>
Basically, I need to send all 3 Options into next page but joined into one variable..
For example: If I chose
<option>165</option>+<option>70</option>+<option>R13</option>
it should be sent to index.php like this: filter_name=165/70/R13
and also, how to send all this not to index.php only, but to
index.php?route=product/search&FILTER_NAME
Changing ACTION="/index.php" to ACTION="/index.php?route=product/search" was not working.
Any help would be really appreciated.
You could try something like this. But, you have several errors in your HTML that you should check. You have a nested table within your form.
<?php
if (!isset($_POST['send'])) {
?>
<form method="post" action="">
<select style="width:55px;" name="select_one">
<option> </option>
<option value="R13">R13</option>
<option value="R14">R14</option>
<option value="R15">R15</option>
<option value="R16">R16</option>
<option value="R17">R17</option>
</select>
<select style="width:55px;" name="select_two">
<option></option>
<option value="165">165</option>
<option value="175">175</option>
<option value="185">185</option>
<option value="195">195</option>
<option value="205">205</option>
<option value="215">215</option>
<option value="225">225</option>
</select>
<select style="width:55px;" name="select_three">
<option></option>
<option value="75">75</option>
<option value="70">70</option>
<option value="65">65</option>
<option value="60">60</option>
<option value="55">55</option>
<option value="50">50</option>
<option value="45">45</option>
</select>
<input style="width:80px; height:25px; font-size:14px; font-weight:600; cursor:pointer;" type="submit" value="send" value="Ieškoti" />
</form>
<?php
}
else {
header('Location: index.php?route=product/search&filter_name='.$_POST['select_two'].'/'.$_POST['select_three'].'/'.$_POST['select_one']);
}
?>
Join the 3 filter options in php into a variable.
$options = $_POST['f1'] . '/' . $_POST['f2'] . '/' . $_POST['f3'];
It will be less confusing IMO.
The problem can be in the invalid markup. You are using table layout, and form is split between cells. That is not valid, so may be browser renders your layout in an unexpected way.
You can expect this:
<table>
<tr>
<td>
<form>
<input>
</td>
<td>
<input>
</form>
</td>
</tr>
<table>
But browser can render your markup as this (2 forms):
<table>
<tr>
<td>
<form>
<input>
</form>
</td>
<td>
<form>
<input>
</form>
</td>
</tr>
<table>
So when you submit your form not all data are sent, cause only first form is submitted

Categories