I've got the following code which now I would like to make to multiply $row['Price'] by quantity from form once the 'Buy' link has been clicked. I am a beginner so please don't judge, any help appreciated :) thanks
<table style="width:50%" id="table1" align="center">
<tr>
<th>Product Name</th>
<th>Price</th>
<th>Image</th>
<th>Quantity</th>
<th>Buy</th>
<?php
$query="SELECT * FROM products";
$result=mysqli_query($connection, $query);
while ($row=mysqli_fetch_assoc($result)){
echo "<tr>";
echo "<td>";
echo $row['ProductName'];
echo "</td>";
echo "<td>";
echo "$".$row['Price'];
echo "</td>";
echo "<td>";
echo ' <img src="./images/'.$row['Image'].'" style="width:50px;height:50px"/><br />';
echo "</td>";
echo "<td>";
echo "<form method='get' action='buy.php'>";
echo "<fieldset>";
echo "<input type='number' name='quantity' style='width:30px'/>";
echo "</td>";
echo "<td>";
echo 'Buy';
echo "</td>";
echo "</tr>";
echo "</fieldset>";
echo "</form>";
}
Short version:
You will need to post the quantity and the productID. than in your buy.php get the product price for that particular productID from the database and multiply that by the quantity. DO NOT POST THE PRICE IN YOUR FORM!!! please for the sake of programming do not post the product price in your form
Long version:
What you need:
1: A form (you have that already)
1b: A hidden form field that holds your product ID:
<input type='hidden' name="ProductID" value="<? echo $row['ProductID'];?>"/>
2: a submit button for your form:
<input type='submit' />
3: a page to recieve the form (you have that already: buy.php)
Now in your buy.php you can get the FORM variables like this:
echo $_POST['quantity'] ;
echo $_POST['ProductID'] ;
Now you need to get that product form the database to get the price
$query="SELECT * FROM products where ProductID=$_POST['ProductID']";
than multiply the price with the quantity
Do NOT send the price in the form... because people can edit that value!
thats why we send only the product ID and get the price from the database!!!!!
Also note that in this brief explanation i did not take form validation in consideration. But obviously you need to make sure that data you get from the is valid and not dangerous (google SQL injection to learn more)
for example your form is something like that
<form method='post' action='thispage.php'>
<input type="text" name="quantity" vlaue="1"> <!--you can get this value from form input no problem -->
</form>
<!-- now on thispage.php write the script you have posted on the question and on line Buy link you have to do something like that-->
<?php
#$q = $_POST['quantity'];
$value= $q*$row['Price'];
?>
echo 'Buy';
now you can get this value on page buy.php something like that
$value = $_GET['price'];
echo $value;
Related
I am working on a project where I want users to choose their space for parking and book it. I have a MySQL database which holds information about parking slots. I am fetching those values and display it using table and form. I have put checkboxes to make choice. and once they make choice they should be directed to payment page.
I am having problem with checkbox. I can see that in value fields it has values from database but when I hit submit button it doesn't pass any values to next page.
below is my code
<body>
<form method="POST" action="book.php">
<?php
//we create a table
echo "<table>";
// create table th
echo "<tr > <th> Parking Slot No </th> <th> Status </th>";
$sql=" select ParkingSlotNo,Status from fleming_dwing ";
$st=$conn->prepare($sql);
$st->execute();
$total=$st->rowCount();//get the number of rows returned
if($total < 1 ){//if no row was returned
echo "<tr> <td style> No Data: DataBase Empty </td> ";//print out error message
echo "<td> No Data: DataBase Empty </td> ";//print out error message
$ing = "<img src='img/occupied.png'/>" ;
}
else{
while($res = $st->fetchObject()){//loop through the returned rows
echo "<tr>";
if($res->ParkingSlotNo and $res->Status=='OCCUPIED')
{echo "<td> $res->ParkingSlotNo </td> ";
echo "<td> <img src='img/occupied.png'/> </td>";
echo"<td><a href=''> </a> </td>";
}
else
{
echo "<td> $res->ParkingSlotNo </td> ";
echo "<td> <img src='img/vacant.png'> </td>";
echo"<td><input type='checkbox' value='$res->ParkingSlotNo'></td>";
}
echo"</tr>";
}
}
?>
</table>
<input type="submit" value="Submit">
</form>
</body>
</html>
and this is the code for booking page
<?php
require_once("dbconfigpdo.php");
print_r($_POST);
?>
The checkboxes do not have name attributes. A form control can't be successful (included in the name=value pairs of data that are submitted) without one.
Any input must have a name attribute.
By name you can use a value. So, you need to add name="your name" to your checkboxes.
You need to set an attribute name to your input field, or it will not be processed.
Something like:
<input name='parkingslot' type='checkbox' value='$res->ParkingSlotNo'>
I fetch data from mysql in php. List all items with checkbox and quantity (rqty) as input value where user can enter quantity. But I'm not able to relate checkbox with corresponding input value (rqty) in an array. If I select first checkbox and enter quantity it works fine but if select any other checkbox and enter quantity, it's not getting updated in database. My code is as below:
<?php
If (isset($_REQUEST['submit1'])!='') {
foreach($_POST['itm'] as $key=>$itm) {
$odate=date("Y-m-d H:i:s");
$rqty=$_POST['rqty'][$key];
$itm=$_POST['itm'][$key];
$sql1="insert into table (itemtype,itemname,qty ,retdate,reason) select itemtype, itemname, '$rqty', '$odate', 'Return' from stock where itemname='$itm';
mysql_query($sql1);
}
}
?>
<p>
<form action="" method="post" name="form1">
<?php
$query3 = "SELECT id, category, itemtype, itemname, qty, status FROM stock where location='godown'";
$comments3 = mysql_query($query3);
$qty = mysql_num_rows($comments3);
echo "Consumable Items: " . $qty;
print "<table class='blue'>
<thead>
<tr>
<td width=100>Return</td>
<td width=100>Category</td>
<td width=100>Item Type</td>
<td width=100>Item Name</td>
<td width=100>Available Qty.</td>
<td width=100>Return Qty.</td>
<td width=100>Status</td>
</tr>
</thead>";
while($row1 = mysql_fetch_assoc($comments3))
{
$it=$row1['itemname'];
print "<tbody>";
print "<tr>";
print "<td><input type='checkbox' name='itm[]' value='$it'></td>";
print "<td>" . $row1['category'] . "</td>";
print "<td>" . $row1['itemtype'] . "</td>";
print "<td>" . $row1['itemname'] . "</td>";
print "<td>" . $row1['qty'] . "</td>";
print "<td><input type='text' name='rqty[]' placeholder='Enter Qty' /></td>";
print "<td>" . $row1['status'] . "</td>";
print "</tr>";
print "</tbody>";
}
print "</table>";
?>
<input type='submit' name='submit1' value='Return' />
In inspect I can see that all rqty[] are getting posted with blank data. But in case of itm, only itm which is selected is getting posted which is correct.
enter image description here
Since I did not receive any solution for this, I changed the approach. Now handling each checkbox separately and not using any array. Added submit button in form list and posting required values to other php page where it's processed further.
That's because of your loop:
foreach($_POST['itm'] as $key=>$itm) {...
and input "itm" is checkbox: <input type='checkbox' name='itm[]' value='$it'></td>
For instance, if you have 5 checkboxes in the form-table but only 2 of them was checked - then you'll have only 2 items in array $_POST['itm'], not 5.
To fix this issue you need to loop through $_POST['rqty'] instead of $_POST['itm']
I have a form with 2 selects, when you send the first, the second select charges the values that are called on my oracle bd with a query, then when i send the second select, it generates a table with checkboxes:
if(isset($idTActi)){
$stallTableTarifas=oci_parse($conn, "SELECT TARIFAS.ID, TARIFAS.ID_TIPO_ACTIVIDAD, TARIFAS.TIPO, TIPO_ACTIVIDAD.TEMPS_KM, TARIFAS.PRECIO
FROM TARIFAS, TIPO_ACTIVIDAD
WHERE TARIFAS.ID_TIPO_ACTIVIDAD = TIPO_ACTIVIDAD.ID
AND TARIFAS.ID_TIPO_ACTIVIDAD = $idTActi");
oci_execute($stallTableTarifas);
echo "<div class='divPrecios'>";
echo "<table>";
echo "<tr class='tabPreciosTitles'>";
echo "<td>Tipus Tarifa</td>
<td>Temps/Km</td>
<td>Preu</td>
<td><input type='submit' class='carrito' value=''></td>";
echo "</tr>";
while (($row=oci_fetch_array($stallTableTarifas,OCI_BOTH))!=false){
echo "<tr>";
echo "<td>".$row['TIPO']."</td>";
echo "<td>".$row['TEMPS_KM']."</td>";
echo "<td>".$row['PRECIO']."</td>";
echo "<td><input type='checkbox' name='checkbox[]' value='".$row['ID']."'/></td>";
echo "</tr>";
}
echo "</table>";
echo "</div>";
}
echo "</form>";
The variable $idTActi it's the id that i return from the second select, so when i click on the checkboxes and i send it on the button named class='carrito', that's an sprite that i generate on css, i see on the bottom another table with the information that i selected on the previous table:
echo "<div class='divPrecios'>";
echo "<table>";
echo "<tr class='tabPreciosTitles'>";
echo "<td>Nom Activitat</td>
<td>Nom Tipus Activitat</td>
<td>Tipus Tarifa</td>
<td>Temps/km</td>
<td>Preu</td>";
echo "</tr>";
foreach($_POST['checkbox'] as $item){
$stallTableCarrito=oci_parse($conn, "SELECT ACTIVIDAD.NOM AS NOM_ACTIVIDAD, TIPO_ACTIVIDAD.NOM AS NOM_TACTIVIDAD, TARIFAS.TIPO, TIPO_ACTIVIDAD.TEMPS_KM, TARIFAS.PRECIO
FROM TARIFAS, ACTIVIDAD, TIPO_ACTIVIDAD
WHERE TARIFAS.ID = $item
AND TARIFAS.ID_TIPO_ACTIVIDAD = TIPO_ACTIVIDAD.ID
AND TIPO_ACTIVIDAD.ID_ACTIVIDAD = ACTIVIDAD.ID");
oci_execute($stallTableCarrito);
$array=array(
0=>array(),
1=>array(),
2=>array(),
3=>array(),
4=>array()
);
while (($row=oci_fetch_array($stallTableCarrito,OCI_BOTH))!=false){
array_push($array[0],$row['NOM_ACTIVIDAD']);
array_push($array[1],$row['NOM_TACTIVIDAD']);
array_push($array[2],$row['TIPO']);
array_push($array[3],$row['TEMPS_KM']);
array_push($array[4],$row['PRECIO']);
}
for ($x=0;$x<count($array[0]);$x++){
echo "<tr>";
echo " <td>".$array[0][$x]."</td>";
echo " <td>".$array[1][$x]."</td>";
echo " <td>".$array[2][$x]."</td>";
echo " <td>".$array[3][$x]."</td>";
echo " <td>".$array[4][$x]."</td>";
echo " <td><input type='submit' class='carritoElim' value=''></td>";
echo "</tr>";
}
}
echo "</table>";
echo "</div>";
Basically that's a shopping form.
And where is the problem? When i send the pushed checkboxes with the button class='carrito', the form by default refresh the page and clears my array, what can i do?
In your first part of code, is your form tag open ? (I guess it is if this one works)
In the second part, is your <input type='submit' class='carritoElim' value=''> tag in a form ?
Because if it's not, you gonna have a bad time ;-)
Maybe in the last form you should generate hidden input with same names as your first form and same values.
If you don't I guess your variable $idTActi won't be set anymore and it won't succeed the first test if(isset($idTActi)). That could be why you get a cleared page.
If you have a multi step form in the same php page, for this kind of html code :
<form method=POST url="myURL">
<select name="select1">[...]</select>
<select name="select2">[...]</select>
<!-- VARIOUS PART : may not be displayed -->
<div id="checkboxes">
<input type="hidden" name="boxStep" value="1"/>
<input type="checkbox" name="cb1" value="1"/>
[...]
</div>
<!-- END OF VARIOUS PART -->
</form>
Then you need php tests in this order :
// if post request
if (isset($_POST)) {
if (isset($_POST['boxStep'])) {
// behavior when checkboxes values are sent
} else {
if (isset($_POST['select2'])) {
// behavior when second select is filled
// display "VARIOUS PART"
} else {
// behavior when only first select is filled
// Do not display "VARIOUS PART"
}
}
} else {
// default behavior (no select filled)
// Do not display "VARIOUS PART"
}
Apolo
Hey Guys, I have a question for you.
Imagine that I wanted to be able to keep track of how many miles I've ran every week, so that I could
compare it to the goals I've set for each week. So i've created this table by the use of mysql_fetch_row.
$result=mysql_query("SELECT * FROM randomtable ORDER BY week ASC");
echo "<Table id='result' cellspacing='0'>
<tr class='toprow'>
<th>Week</th>
<th>Goal</th>
<th>Actual Number of Miles</th>
</tr>";
while($row = mysql_fetch_row($result))
{
echo "<tr class='standardrow'>";
echo "<td>$row[0]</td>";
echo "<td>$row[1]</td>";
echo "<td><form><input method='post' type='number'></form></td>";
echo "</tr>";
}
echo "</table>";
This piece of code resultet in a table with 10 weeks with 10 goals - and a column for the actual number of miles. This column should include 10 input forms where the actual number of miles can be submitted. But how do I relate the input from the submit form to the row in which the submit form is positioned?
The primary key is the week - so this would be the one to relate to.
Hope you understand what my problem is:)
To do this you would use a hidden input field.
When you echo each row, and the form in that row, you would simply add an extra line:
`<input type="hidden" name="row_id" value="' . $row['id_column'] . '" />';
In full, your code would be:
$result=mysql_query("SELECT * FROM randomtable ORDER BY week ASC");
echo "<Table id='result' cellspacing='0'>
<tr class='toprow'>
<th>Week</th>
<th>Goal</th>
<th>Actual Number of Miles</th>
</tr>";
while($row = mysql_fetch_row($result))
{
echo "<tr class='standardrow'>";
echo "<td>$row[0]</td>";
echo "<td>$row[1]</td>";
echo "<td>
<form>
<input method='post' type='number'>
<input type='hidden' name='row_id' value='" . $row['id_column'] . "' />
</form>
</td>";
echo "</tr>";
}
echo "</table>";
I think there should be some modifications that has to be done in loop.
echo "<td><form method='post'><input type='number' value='".$rows['col_name']."'><input type='submit' ></form></td>";
This code adds a submit button to each row. But, this shouldn't be what I think.
It should be rather this way,
echo "<form method='post'> ";
while($row = mysql_fetch_row($result))
{
echo "<tr class='standardrow'>";
echo "<td>$row[0]</td>";
echo "<td>$row[1]</td>";
echo "<td><input type='number' value='".$rows['col_name']."'></td>";
echo "</tr>";
}
echo "<input type='submit' ></form>";
Or make the input field look like this.
'<input type="text" name="row['.$row['id_column'].'][miles]" />';
It will return you an array when you post it.
foreach($_POST['row'] as $key => $value){
// $key is your primary key
// $value['miles'] is your input value
}
So basically I don't really know how to explain what i need or if the title is even correct but i need some help.I'll explain what I'm trying to do the best I can.
This is a picture of the page I'm working on and referring to
http://i40.tinypic.com/14m5euh.png .
SO basically what I need to do is,when the user uploads a file and clicks on upload it will upload to a database along with the job number,so far I've managed to get it to update into the database.
But I can't get the job number in the database because my information is all in an array from the database I tried to store the information into a session variable,its loops and gets replaced by the next one,so basically what ever the last job number is will be stored in the database which of cause is wrong?
The last problem I've is,how do I get the users answer from the drop down box into a variable ?
My code:
$con = mysqli_connect("localhost", "root", "","fixandrun") or die(mysqli_error());
$data = mysqli_query($con,"SELECT * FROM bookjob") or die(mysql_error());
Print "<table border cellpadding=10>";
Print "<table border='2'>";
echo "<table border='2' cellpadding=10>
<tr>
<th> Job number</th>
<th> Job details</th>
<th> Pc number </th>
<th> Job status</th>
<th> Upload report</th>
<th> Update Job</th>
</tr>";
while($row= mysqli_fetch_array($data))
{
echo "<tr>";
echo "<td>".$_SESSION['JOBNUM'] = $row['jobnumber'] . "</td>";
echo "<TD width=20% height=100>" . $row['jobdetails'] . "</td>";
echo "<td>" . $row['pcnumber'] . "</td>";
echo "<td> <select name='jobprogress'>
<option Value='pending'>pending</option>
<option value='Completed'>Completed</option>
<option value='In progress'>In progress</option>
<option value='Need more information'>Need more information</option>
</select> </td>";
echo "<td> <form action='reportupload.php' method='post' enctype='multipart/form-data' name='uploadform'>
<input type='hidden' name='MAX_FILE_SIZE' value='350000'>
<input name='report' type='file' id='reportupload' size='50'>
<input name='upload' type='submit' id='upload' value='Upload'>
</form>
</td>";
echo "<td> <a href='updateadmin.php'>Update information</a></td>";
echo "</tr>";
}
echo "</table>";
echo "<br>";
There are many problems with your code. It would be extremely time consuming to go through them all, so I'll run down some things you need to look at to get this going.
FIrstly, you can make as many forms as you want, and with the help of PHP you can do this dynamically as youre doing. The problem is for every job, youre creating a new form with the same exact name. You need to find a way to make them the name of each form dynamic if youre going to do that.
Secondly, you probably shouldnt make a new form, so take all your opening and closing tags out of the loop, put the opening form tag before the while, and the ending form tag after the while loop.
I would suggest you go read about HTML forms a little more and PHP while loops and fetching information from a database with embedded forms before messing with this more. No offence, but I think you need a little more knowledge on this before fiddling with it.
I would suggest making a test database and table with maybe 3 fields, then making a small form with a couple field and seeing if you can fetch the data and update it with your form first.
i think i have done what you ment, is this right now?
$cnt = 0;
while($row= mysqli_fetch_array($data))
{
echo " <form action='test.php' method='post' enctype='multipart/form-data' name='uploadform' . $cnt>";
echo "<tr>";
echo "<td>".$_SESSION['JOBNUM'. $cnt] = $row['jobnumber'] . "</td>";
echo "<TD width=20% height=100>" . $row['jobdetails'] . "</td>";
echo "<td>" . $row['pcnumber'] . "</td>";
echo "<td> <select name='jobprogress'>
<option Value='pending'>pending</option>
<option value='Completed'>Completed</option>
<option value='In progress'>In progress</option>
<option value='Need more information'>Need more information</option>
</select> </td>";
echo "<td>
<input type='hidden' name='MAX_FILE_SIZE' value='350000'>
<input name='report' type='file' id='reportupload' size='50'>
</td>";
echo "<td><input name='upload' type='submit' id='upload' value='Upload'></td>";
echo "</tr>";
echo "</form>";
++$cnt;
}
echo "</table>";
echo "<br>";
?>