I print this form in a web page with php:
<?php
include("connectDB.php");
$mySQL=new MySQL();
$queryResult=$mySQL->query("SELECT nombre, precio, id_producto FROM productos");
echo "<form action= 'checkout.php'> method=''POST'";
while($datos=$mySQL->fetch_array($queryResult))
{
$n = 1;
$nombre=$datos['nombre'];
$id_producto=$datos['id_producto'];
$precio=$datos['precio'];
echo "<h1>$nombre</h1>";
echo "<input type=\"checkbox\" name=\"$id_producto\" value=\"$nombre\"> Cantidad: <input type=\"number\" name=\"points\" min=\"1\" max=\"20\" step=\"1\" value=\"1\"><br>";
echo "<h3> Precio: $precio<br>";
}
echo "<br>";
echo "<input type=\"submit\" class=\"button\" value=\"Comprar\">";
echo "</form>";
?>
So it displays a list (which is a form within) of items which can be selected or checked, on submit, I want to do the $_POST[''] of only the checked items, how could I solve this?
When such checkboxes are printed, only those values which were checked are submitted.
If i understood you correctly you wanted to retrieve those that got posted, which you can follow with this simple method
foreach($_POST as $post_key => $post_value){
//Check that the particular input is int and numeric, since i believe the name is the id
if(is_numeric($post_key) && is_int($post_key){
//Here goes your code, $post_key is the id and $post_value is the $nombre
//Although i admit that i have no idea what nombre is since it is in another language. Forgive me if id_producto is not numeric and unique.
}
}
Related
I'm trying to create a very easy stock managing system. I'm able to show all the items in my table 'parts' and i'm showing the amount in a textbox. However, when i change the value from, for example, 0 to 5 in the textbox and i press my submit button, it doesn't update the stock.
Below is my code, i don't have alot of experience with update querys but i've read about it on php.net, obviously.
<?php
echo "<table width=\"800\" class=\"nieuws\">";
$db=mysqli_connect("localhost","root","","lichtwinkel");
$p=mysqli_query($db, "SELECT * FROM parts WHERE product LIKE 1");
echo "<form method='post' action=''>";
echo "<tr><th></th><th>Onderdeel nummer</th><th>Eigenschappen</th><th>Prijs</th><th>Voorraad</th></tr>";
while ($row = mysqli_fetch_array($p)){
echo "<tr>";
echo "<td><img class='lamp' src='../css/images/".trim($row['partnr']).".png' alt='Geen afbeelding beschikbaar'></td>";
echo "<td>".$row['partnr']."</td>";
echo "<td>".$row['specs']."</td>";
echo "<td>€ ".$row['price']."</td>";
echo "<td><input type='text' id='aantal' name='aantal' value=$row[voorraad] /></td>";
echo "<td><input type='submit' id='update' name='update' value='Update' /></td>";
echo "</tr>";
}
echo "</table>";
if(isset($_POST['aantal']) && $_POST['update']) {
$y = $_POST['aantal'];
$p=mysqli_query($db, "UPDATE parts SET voorraad = '$y' WHERE partnr = $row[0]");
}
echo "</form>"
?>
Simply said, what i'm trying to achieve is the following:
Whenever i change the value displayed in the texbox, and i press my submit button, i want it to update the value in the database.
Does anyone know what i'm doing wrong? Any ideas? Articles i should read?
All help would be appreciated.
Thank you.
As i see, you were doing it wrong at all.
First you can't use form tag within more then one td element.
You were didn't close the form tag, only at end. (So if it loops 6 times, you will have 6 forms open, but one ended!).
At update, you're selecting row[0] - it's outside of loop with rows?
Even if you update it, it will show wrong results again. Update should be above selects! So it picks up newly updated value.
What to do:
First make one form for all updates.
Use your submit button to have value DATABASE_ID.
Make the name of "aantal" to "aantalDATABASE_ID".
At submit check for $_POST['update'], and use it's value (DATABASE_ID) to get input $_POST["aantal".$_POST['update']].
Do update, you have all you need.
Example:
<?php
echo "<form method='post' action=''>";
echo "<table width=\"800\" class=\"nieuws\">"
$db=mysqli_connect("localhost","root","","lichtwinkel");
if(isset($_POST['update']) && !empty($_POST['update'])) {
$y = $_POST['aantal'.$_POST['update']];
$p=mysqli_query($db, "UPDATE parts SET voorraad = '".$y."' WHERE partnr = '".$_POST['update']."'");
}
$p=mysqli_query($db, "SELECT * FROM parts WHERE product LIKE 1");
echo "<tr><th></th><th>Onderdeel nummer</th><th>Eigenschappen</th><th>Prijs</th><th>Voorraad</th></tr>";
while ($row = mysqli_fetch_array($p)){
echo "<tr>";
echo "<td><img class='lamp' src='../css/images/".trim($row['partnr']).".png' alt='Geen afbeelding beschikbaar'></td>";
echo "<td>".$row['partnr']."</td>";
echo "<td>".$row['specs']."</td>";
echo "<td>€ ".$row['price']."</td>";
echo "<td><input type='text' id='aantal' name='aantal".$row[0]."' value='".$row[voorraad]."' /></td>";
echo "<td><input type='submit' id='update' name='update' value='".$row[0]."' /></td>";
echo "</tr>";
}
echo "</table>";
echo '</form>';
?>
After all, take care about SQL Injections. "aantal" value is user input. As the submit value can be changed.
I have a form which consist of a multi-D array. It has both checkbox and text. So i would like to use both values to enter them in database. I could not figure out how to use them. Any help would be much appreciated. Here is my code ;
$clothes = array(
"For_Man"=>array("Suit", "Tie"),
"For_Woman"=>array("Skirt","Bra")
);
echo '<form action="" method="POST">';
foreach ($clothes as $cloth => $task) {
echo "<strong>{$cloth}</strong> <br />";
foreach ($task as $type){
echo "<input type=\"checkbox\" name=\"box[]\" value=\"{$type}\" />\r
{$type} <input type=\"textbox\" name=\"note[]\" size=\"2\" /><hr />\r";
}
}
echo "<input type=\"Submit\" name=\"submit\" value=\"Submit\" />\r
</form>";
if(isset($_POST['box'])){
if(isset($_POST['note'])){
$note = $_POST['note'];
}
$box = $_POST['box'];
}
So user can select the item and enter the amount they want so i can use it to enter database like
new_order = mysql_query("INSERT INTO orders(cloth_name, amount,... ) VALUES ('$box', '$note', ...........");
I really don't understand you, do you want to save each product of your array in the database? if it is the case you can put it in another table you know,
=products=
Id_Product
name
=Orders
Id_Order
...
=Products_Orders
Id_PO
Id_Product
Id_Order
amount
note
instead of putting the information in a unique table.
For learning purposes, I'm doing this website where user can select some items and the number of units he wants, let's say it's this simplest shopping app.
So, I read from database the existing items in catalog:
$queryResult=$mySQL->query("SELECT nombre, precio, id FROM productos");
Then I print the list of products:
$queryResult=$mySQL->query("SELECT nombre, precio, id_producto FROM productos");
echo "<form action=\"checkout.php\" method=\"POST\">";
while($datos=$mySQL->fetch_array($queryResult))
{
$nombre=$datos['nombre'];
$id_producto=$datos['id_producto'];
$precio=$datos['precio'];
echo "<h1>$nombre</h1>";
echo "<input type=\"checkbox\" name=\"$id_producto\" value=\"on\"> Cantidad: <input type=\"number\" name=\"$id_producto"."Number\""." min=\"1\" max=\"20\" step=\"1\" value=\"1\"><br>";
echo "<h3>Precio: $precio</h3><br>";
}
echo "<br>";
echo "<input type=\"submit\" class=\"button\" value=\"Comprar\">";
echo "</form>";
They get as value the ones from $id_producto, in the case of the number type inputs I concatenate "Number" so they dont get the same name.
After submission, I'm trying to do something like:
foreach($_POST as $post_key => $post_value){
if ($post_value=="on") {
$var1 = $_POST['.$post_key."Number".'];
}
}
So if a checkbox was selected I check for its corresponding numeric value.
Is it OK to perform another $_POST['somevariable'] inside that loop?
How can I concatenate that argument inside the square brackets?
You don't need the single quotes, that's for when the key is a constant string. Since your key is an expression, just put that expression inside the brackets:
if ($post_value=="on") {
$var1 = $_POST[$post_key."Number"];
}
A better way to do this is to post the inputs as arrays:
echo "<input type=\"checkbox\" name=\"producto[]\" value=\"$id_producto\"> Cantidad: <input type=\"number\" name=\"Number[$id_producto]\" min=\"1\" max=\"20\" step=\"1\" value=\"1\"><br>";
Then you can use the loop:
foreach ($_POST['producto'] as $post_value) {
$var1 = $_POST['Number'][$post_value];
}
I have checkbox list in my form where user can select and save the data in to the database and edit the data.
In the checkbox list i have one checkbox named other and if user check the checkbox named other it populates a textbox in which user has to enter the other value.
while saving the data into the database I just replaced other checkbox value with textbox value.so upto now its working good.
But if user wants edit the form, I have to show the checkboxes with mark if value exists in the database and also if there is any value related to other then other checkbox should be checked and i have to place the value in the textbox.
Please find the code below
$aEquipment = array("Arthroscopy", "K-wires", "C-arm", "Mini-c-arm", "Other");
//converting comma separated into array using explode function
$dbequipment = $event->proc_equipment_request; //array of values from database
$dbequipment= explode('|',$dbequipment);
foreach ($aEquipment as $equipment) {
if(in_array($equipment,$dbequipment)) {
echo "<input name=\"equipment[]\" type=\"checkbox\" value=\"$equipment\" CHECKED> $equipment ";
} else{
if($equipment == "Other"){
echo "<input name=\"equipment[]\" type=\"checkbox\" id=\"otherEquipment\" value=\"$equipment\"> $equipment";
echo "<input type=\"text\" id=\"otherEquipmentVal\" name = \"otherEquipment\" style=\"display:none; width:25%;\" placeholder=\"Equipment request\" />";
} else {
echo "<input name=\"equipment[]\" type=\"checkbox\" value=\"$equipment\"> $equipment";
}
}
}
I am able to check the checkboxes but unable to place the value of textbox.
You need to fill the value attribute of the text field, you have now:
echo "<input type=\"text\" id=\"otherEquipmentVal\" name = \"otherEquipment\" style=\"display:none; width:25%;\" placeholder=\"Equipment request\" />";
And need to have:
echo "<input type=\"text\" id=\"otherEquipmentVal\" name = \"otherEquipment\" style=\"display:none; width:25%;\" placeholder=\"Equipment request\" value=\"Your Value\" />";
Note the difference is the follow attribute at the end:
value=\"Your Value\"
<?php
$i=1;
$ph='php';
echo "<form method=\"post\" action=\"\">";
$query=mysql_query("select question,id from ques where category=\"$ph\" order by rand() limit 5",$con);
if(!$query)
{
echo mysql_error().'query failed';
}
$ans=1;
while($value1=mysql_fetch_array($query))
{
echo "Q-$i"." ";
echo $value1['question']."<br />";
$i++;
$qno=$value1['id'];
$query1=mysql_query("select id,option1,option2,option3,option4 from ques where id=$qno");
if(!$query1)
{
echo mysql_error().'query failed';
}
while($value2=mysql_fetch_array($query1))
{
$opt=$value2['option1'];
$opt1=$value2['option2'];
$opt2=$value2['option3'];
$opt3=$value2['option4'];
$id=$value2['id'];
echo "<input type=\"radio\" value=\"$opt\" name=\"$ans\">";
echo "<span class=\"margin\">$opt</h1>";
echo "<input type=\"radio\" value=\"$opt1\" name=\"$ans\">";
echo "<span class=\"margin1\">$opt1</h2>";
echo "<input type=\"radio\" value=\"$opt2\" name=\"$ans\">";
echo "<span class=\"margin2\">$opt2</h3>";
echo "<input type=\"radio\" value=\"$opt3\" name=\"$ans\">";
echo "<span class=\"margin3\">$opt3</h4>"."<br /><br />";
$ans++;
}
}
echo"<center>"."<input type=\"submit\" name=\"submit\" value=\"submit\">"."</center>";
echo "</form>";
if(isset($_POST['submit']))
{
$correct=$_POST[1];
$_SESSION['value']=$correct;
/*$correct1=$_SESSION['value'];
echo $correct1;*/
$correct2=$_POST[2];
$_SESSION['value1']=$correct2;
/*$correct3=$_SESSION['value1'];
echo $correct3;*/
$correct4=$_POST[3];
$_SESSION['value2']=$correct4;
/*$correct5=$_SESSION['value2'];
echo $correct5;*/
$correct6=$_POST[4];
$_SESSION['value3']=$correct6;
/*$correct7=$_SESSION['value3'];
echo $correct7;*/
$correct8=$_POST[5];
$_SESSION['value4']=$correct8;
/*$correct9=$_SESSION['value4'];
echo $correct9;
$j=1;
while($j<=$i-1)
{
$correct=$_POST[$j];
$_SESSION['$j']=$correct;
$j++;
$correct1=$_SESSION['$j'];
echo $correct1."<br />";
}*/
header("location:quizresult.php");
}
?>
I don't know how to send id of selected option to the next page.I want to retrieve the id of selected option in the next page because from this i can get the correct answer of the selected question or any other solution of this and i get the only one id on the next page
There are several ways how to carry over values to the next page. I used the following successfully before:
1. Use a $_SESSION variable
Save the option values in session variables and retrieve them on the next page.
2. Use hidden form fields
You can save them as part of the form but in a hidden form field and retrieve it on the next page via $_POST.
An excellent tutorial (4 pages) for HTML forms and its various inputs is here:
http://www.javascript-coder.com/html-form/html-form-tutorial-p1.phtml
3. Use a http query
You can append a http query to your link to the next page, containing the values, i.e.
http://www.example.com/next-page?option1=value1&option2=value2
On the next page you can use parse_url($url, PHP_URL_QUERY) to extract the values. I would recommend that only if you do not submit a form though. I use this option if I create action buttons that are linking to the next (or the same) page and a single action has to be performed before the page is displayed.
Some generic info about query strings: http://en.wikipedia.org/wiki/Query_string
$qno= $_SESSION['id'] = $value1['id'];
Id is being saved in $_SESSION['id'] and will be available on the ext page also.
retrieve it like this
$id = $_SESSION['id'];