I am trying to use the $_GET value to load on the same page as my form instead of opening a new page. For example, the form is on my page "products.php" and I want the form to filter database results by type of product. So on submit, it should redirect to "products.php?type=tee".
If I manually type it in the address bar it works like a charm, but I can't get the form submit to load it.
Here's my code (Update: Here's the whole file, using require_once into a basic html5 template):
<?php
error_reporting(E_ALL); ini_set('display_errors', 1);
//Populate Items to Show
function populateItems($result){
while ($row = $result->fetch_assoc()){
if ($row['available']){
echo '<li><img src="', $row['image'], '" />';
echo '<ul>';
echo '<li><h1>', $row['product'], '</h1></li>';
echo '<li><h2>', $row['description'], '</h2></li>';
echo '<li><h3>$', $row['price'], '</h3></li>';
echo '</ul>';
echo '</li>';
}
}
$result->free();
}
//Create Item List
echo '<ul class="item">';
//Create Filter
echo '<li id="filter">';
if ($result = $conn->query("SELECT DISTINCT type FROM products")){
echo 'Filter Results By: <form method="GET" action="', $_SERVER['PHP_SELF'], '">';
echo '<select>';
echo '<option>Show All</option>';
while ($type = $result->fetch_assoc()){
echo '<option name="type" value="', $type['type'], '">', $type['type'], 's</option>';
}
echo '</select>';
echo '<input type="submit" value="Go" />';
echo '</form>';
}
echo '</li>';
//Find if Filter Exists
if (isset($_GET['type']) && $_GET['type'] != "" ){
$gettype = $_GET['type'];
$filtertype = $conn->query("SELECT * FROM products WHERE type='$gettype'");
$count = $filtertype->num_rows;
if ($count <= 0){
populateItems($conn->query("SELECT * FROM products"));
}else{
populateItems($conn->query("SELECT * FROM products WHERE type='$gettype'"));
}
}else{
populateItems($conn->query("SELECT * FROM products"));
}
//End Item List
echo '</ul>';
?>
I've searched all over and haven't found anything that quite answers my question... Any help would be appreciated!
You have your <option name="type" named when it should be the <select>.
<option> does not have named attributes.
Therefore, you need to remove name="type" from your <option> and change your <select> to <select name="type">
Change
<form method="post" action="">
To
<form method="get" action="">
You have to change the Method if you want to use GET! Like this:
...<form method="get" action="">...
Related
I have tried to solve it and look around but not even sure what I should be searching for.
I have made a product grid through a while loop, in the loop with each product and input-tag has been used for users to mark how many items of each product is wanted.
The product grid
However I have trouble distinguishing the value of each input field and what "name" it should be stored under to be able to retrieve it when running the second script of processing the order? I also need to be able to connect an id with each value.
The code for the grid:
I know I need to make a unique name in the input name, however how and which makes sense?
<div id="content">
<h1>Products</h1>
<form action="processorder.php" method="post">
<table align="center">
<?php
$db = include "connect2db.php";
mysqli_set_charset($db,"utf8");
$query = 'SELECT * FROM products_josie';
$result = $db->query($query);
$count = 0;
while($res=$result->fetch_assoc())
{
if($count==3)
{
echo '</tr>';
$count = 0;
}
if($count==0)
echo '<tr>';
echo '<td>';
?>
<a href="productsdetails.php?clickedid=<?php echo $res['product_id']?>">
<img src="products/<?php echo $res['photo']; ?>" width="200" height="150"/>
</a>
<br/>
<?php
echo '<p>';
echo $res['product_name'];
echo '</br>';
echo 'DKK ';
echo $res['price'];
echo '</p>';
echo '<p>';
echo '<input type="number" name="amount" min="0"';
echo '</p>';
$count++;
print '</td>';
}
if($count>0)
print '</tr>';
?>
</table>
<input type="submit" value="Submit Order">
</form>
</div>
I think what you are looking for is how to get the amounts for different products. If so, something like this might do it:
$prod = $res['product_name'];
echo '<input type="number" name="amount[$prod]" min="0"';
My question is: I want to show all SQL answers from a specific table inside the <select> using php.
I have tried to get it working, but without much success as it only shows one result.
<?php
error_reporting(E_ALL);
include 'connect.php';
include 'header.php';
set_time_limit(10);
$cat = $bdd->query('SELECT cat_id,cat_name,cat_description FROM categories');
$categories_list = $cat->fetch();
While ($categories_list = $cat->fetch()) {
$cat_name = $categories_list['cat_name'];
}
$cat->closeCursor();
echo '<form method="post" action="accès/create_topic_post.php">';
echo '<label for="sujet">Sujet :';
echo '<input type="text" name="sujet" id="sujet" required autofocus>';
echo '</label>';
echo '<label for="cat">Catégories :';
echo '<select name="topic_name">';
echo "<option value=\"".$cat_name."\" >$cat_name </option>";
echo '</select>';
echo '<input type="submit" value="Envoyer">';
echo '</form>';
?>
That's totally not the way to do it. Have a separation between PHP and HTML. And make sure you have the option echoed out inside the loop. Something like this:
<form method="post" action="accès/create_topic_post.php">
<label for="sujet">Sujet :
<input type="text" name="sujet" id="sujet" required autofocus>
</label>
<label for="cat">Catégories :
<select name="topic_name">
<?php
// Loop it here.
while ($categories_list = $cat->fetch()) {
$cat_name = $categories_list['cat_name'];
echo "<option value=\"".$cat_name."\" >$cat_name </option>";
}
?>
</select>
</label><!-- You forgot this -->
<input type="submit" value="Envoyer" />
</form>
There are two approach to achieve your desired output.
Approach One:
Store cat_name in a new array and use it in your HTML or anywhere, where you want to use.
<?php
$cat_name = array();
while($categories_list = $cat->fetch()) {
$cat_name[] = $categories_list['cat_name'];
}
?>
<select name="topic_name">
<?php
foreach ($cat_name as $key => $value) {
?>
<option value="<?=$value?>" ><?=$value?> </option>
}
?>
</select>
Approach 2:
<?php
while ($categories_list = $cat->fetch()) {
?>
<option value="<?=$categories_list['cat_name']?>" ><?=$categories_list['cat_name']?> </option>
<?php
}
?>
Whats wrong with your code:
$cat_name = $categories_list['cat_name']; this will only store the last value of your query, you must need to store the value in an array or use <option> inside the while() loop.
You keep replacing cat_name every time you read data from the SQL server. You need to store it in an array
<?php
error_reporting(E_ALL);
include 'connect.php';
include 'header.php';
set_time_limit(10);
$cat = $bdd->query('SELECT cat_id,cat_name,cat_description FROM categories');
//$categories_list = $cat->fetch(); This line discards a row of data
$cat_names = array(); //array for category names
While ($categories_list = $cat->fetch()) {
//Add newest 'cat_name' to the array
$cat_names[] = $categories_list['cat_name'];
}
$cat->closeCursor();
echo '<form method="post" action="accès/create_topic_post.php">';
echo '<label for="sujet">Sujet :';
echo '<input type="text" name="sujet" id="sujet" required autofocus>';
echo '</label>';
echo '<label for="cat">Catégories :';
echo '<select name="topic_name">';
//Loop and read back each category name
foreach ($cat_names as $cat_name){
echo "<option value=\"".$cat_name."\" >$cat_name </option>";
}
echo '</select>';
echo '<input type="submit" value="Envoyer">';
echo '</form>';
?>
You don't have to loop over results manually, as PDO has a function that can do it for you already, called fetchAll(). You have to use it instead of fetch() if you want to get an array of rows.
However, beside it, you need to learn PHP and programming in general. Because your current code makes little sense. To output an array, you have to use a loop:
$cat = $bdd->query('SELECT cat_name FROM categories');
$categories_list = $cat->fetchAll(PDO::FETCH_COLUMN); // here it is
echo '<form method="post" action="accès/create_topic_post.php">';
echo '<label for="sujet">Sujet :';
echo '<input type="text" name="sujet" id="sujet" required autofocus>';
echo '</label>';
echo '<label for="cat">Catégories :';
echo '<select name="topic_name">';
foreach ($categories_list as $cat_name)
{
echo "<option value='$cat_name'>$cat_name</option>";
}
echo '</select>';
echo '<input type="submit" value="Envoyer">';
echo '</form>';
Move the fetch loop to the output section of your code.
So instead of this:
echo "<option value=\"".$cat_name."\" >$cat_name </option>";
move the loop so it will look like this:
While ($categories_list = $cat->fetch()) {
$cat_name = $categories_list['cat_name'];
echo "<option value=\"".$cat_name."\" >$cat_name </option>";
}
Use while loop while you're outputting the category in select option.
Like this,
While ($categories_list = $cat->fetch()) {
echo "<option value=\"".$categories_list['cat_name']."\" >$categories_list['cat_name'] </option>";
}
i am trying to sort my products using a drop down box. when i select an option the code does not run and the products do not change position. iv managed to sort products by using different buttons but i think a drop down list would look better on a website.
<?php
echo $sort = #$_GET['order'];
if (!empty($sort)) {
echo $query="SELECT * FROM products ORDER BY '".$sort."'";
} else {
echo $query="SELECT * FROM products order by " ;
}
?>
<form name="sort" action="" method="post">
<select name="order">
<option value="choose">Make A Selection</option>
<option value="price_asc">Price </option>
<option value="price_desc">Z-A</option>
<option value="name_asc">A-Z</option>
</select>
<input type="submit" value=" - Sort - " />
</form>
<?php
//Run the query.
$record_set = $connection->query($query);
while( $row = $record_set->fetch_assoc() ) {
echo '<div class="product">';
echo '<div class="product-content"><h3>'. $row['name'].'</h3>' .'</div>' . '<br />'.'<div class="product-thumb"><img src="/ISD assignment2/images/'. $row['imageName'].'"></div>'. '<div class="product-desc">'.$row['description']. '</div>'. '<br />' .'£'. number_format($row['price'], 2) . '<p>Add</p>';
echo '</div>';
}
?>
Two potential issues:
1) Your Form has no action. I believe you are trying to get it to refresh onto itself.
which would be:
action=" <?php echo $_SERVER['PHP_SELF'] ?>"
2) You are submitting via POST, yet using GET.
$sort = $_POST['order'];
I want the value which I select from the listbox and want to store in php variable because I want to use that variable for further use. Please help..
<form name="myform" method="post">
<?php
include('dbconnect.php');
db_connect();
$query = "SELECT * FROM test_customer"; //Write a query
$data = mysql_query($query); //Execute the query
?>
<select id="cust_name" name="mylist" onchange="selectedvalue()">
<?php
while($fetch_options = mysql_fetch_array($data)) { //Loop all the options retrieved from the query
?>
//Added Id for Options Element
<option id ="<?php echo $fetch_options['test_id']; ?>" value="<?php echo $fetch_options['cust_name']; ?>"><?php echo $fetch_options['cust_name']; ?></option><!--Echo out options-->
<?php
}
?>
</select>
</form>
<select id="cust_name" name="mylist" onchange="selectedvalue(this.value)">
access that values selectedvalue(val) in parameter
get that selected value and store it in input type= hidden as u need it for further use..
If you want to store into a php-variable, you must post the form. I've added a submit-button below. You also must speciy an action to where the form is submitted to: action="whatever.php"
<form name="myform" method="post" action="whatever.php"> <!-- added action here -->
<?php
include('dbconnect.php');
db_connect();
$query = "SELECT * FROM test_customer"; //Write a query
$data = mysql_query($query); //Execute the query
?>
<select id="cust_name" name="mylist" onchange="selectedvalue()">
<?php
while($fetch_options = mysql_fetch_array($data)) { //Loop all the options retrieved from the query
?>
//Added Id for Options Element
<option id ="<?php echo $fetch_options['test_id']; ?>" value="<?php echo $fetch_options['cust_name']; ?>"><?php echo $fetch_options['cust_name']; ?></option><!--Echo out options-->
<?php
}
?>
</select>
<!-- added submit button -->
<input type="submit" value="ok" />
</form>
Create a php-file called whatever.php and in this file, store the value into $cust_name by doing this:
<?php
$cust_name = $_POST['mylist']; //mylist is the name of the select-list
?>
If you want to post the form without having to reload the page, you must use something called ajax.
This works for me.Try this example.
public function getAptTypesBySelectedKy($valKy) {
$stmt = "SELECT ap_typ_ky, ap_typ_desc FROM apt_types WHERE ap_stat=1 order by ap_typ_desc";
try {
$con = DataHandler::connect();
$values = $con->prepare($stmt);
if ($values->execute()) {
echo '<select class="form-control" name="type" id="apt_types_slc">';
while ($row = $values->fetch(PDO::FETCH_ASSOC)) {
if ($valKy === $row['ap_typ_ky']) {
echo '<option value="' . $row['ap_typ_ky'] . '" selected>' . $row['ap_typ_desc'] . '</option>';
} else {
echo '<option value="' . $row['ap_typ_ky'] . '">' . $row['ap_typ_desc'] . '</option>';
}
}
echo '</select>';
}
} catch (PDOException $ex) {
echo 'Error on apartment types';
$error = $ex;
print_r('<pre>' . $ex->getCode() . '</pre>');
print_r('<pre>' . $ex->getMessage() . '</pre>');
}
}
In your html form page.
$__typKy = $_RA['typeky'] //this line get the selected value from database and set as parameter to the function getAptTypesBySelectedKy()
<?php
$__typKy;
if (isset($_RA)) {
$__typKy = $_RA['typeky'];// you need to find this key from database.
//this is the selected value key of `<select>`
}
$var = new DataHandler();
$var->getAptTypesBySelectedKy($__typKy);
?>
I am using this code in my form to create a drop down menu. (the list of options loads corrects from my sql database). Once the user hits submit, I should be able to retrieve the value selected with $_POST['field'].
<form action="page2.php" method="post" name="form" id="form">
<?php
$query = sprintf("SELECT domaine FROM `domainema` WHERE userid='%s' ", $userid);
$result=mysql_query($query);
echo "<select name=domaine value=''>Domain </option>";
while($nt=mysql_fetch_array($result)){
echo "<option value=$nt[id]>$nt[domaine]</option>";
}
echo "</select>";
?>
...
On the second page, I use this code:
$domaine = strip_tags(substr($_POST['domaine'],0,32));
echo "You selected $domaine";
But I get nothing a blank value, what am I doing wrong?
Thanks!
In your query you didn't selected the id, only the domaine. Change it to be like this:
<form action="page2.php" method="post" name="form" id="form">
<?php
$query = sprintf("SELECT id, domaine FROM `domainema` WHERE userid='%s' ", $userid);
$result=mysql_query($query);
echo '<select name="domaine">';
while($nt=mysql_fetch_array($result)){
echo '<option value="$nt[id]">$nt[domaine]</option>';
}
echo "</select>";
?>
This line is probably incorrect...
echo "<select name=domaine value=''>Domain </option>";
Should it be
echo "<select name=domaine value=''>";
You should also note that if none of the options are selected, then you won't get a value back. To ensure you get a value back, select one of them (eg the first one) by default, by adding selected="selected" to it...
I'd also recommend quoting values a little more clearly.
For the sake of completeness...
<?php
$query = sprintf("SELECT domaine FROM `domainema` WHERE userid='%s' ", $userid);
$result=mysql_query($query);
echo '<select name="domaine" value="">';
$isfirst = true;
while ($nt=mysql_fetch_array($result)) {
echo '<option value="'.$nt[id].'"';
if ($isfirst)
echo ' selected="selected"';
echo '>'.$nt[domaine].'</option>';
$isfirst = false;
}
echo '</select>';
?>