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'>
Related
i have made a form in php and inside form there is a loop that extracts multiple values from database(img,name,availability,..) for multiple books i have made a table to display those values to user and after displaying these data in table i have made an issue button inside loop so that every book has its issue button.
My problem is that i have to only retrieve id of that book for which user click issue button. i tried storing it in cookie but it send the id of the first book displayed then i tried get method but that results in sending the last book that is displayed on screen id. but i want is that it should send the id of book which is selected by user
display books
echo "<form action='issue.php' method='get'>";
while ($row= mysqli_fetch_array($result)) {
echo "<div id='img_div' style='background-color:#fff;'> ";
echo "<img src='books/".$row['image']."'>";
// echo "</div>";<div id='text'>
$isbn=$row['isbn'];
echo "<input type='hidden' name='isb' value='$isbn' />";
echo " <table>";
echo "<tr><td> NAME</td><td> ".$row['name']."</td></tr>";
echo "<tr><td> AVAILABILITY</td><td> ".$row['availabilty']."</td></tr>";
echo "<tr><td> CATEGORY</td><td> ".$row['category']."</td></tr>";
echo "<tr><td colspan='2'>
<button type='submit' name='issue'>issue</button></td></tr>";
echo "</table>";
echo "</div><br/>";
if (isset($_GET['issue'])) {
# code...
$bookid=$isbn;
setcookie("bid",$bookid);
if(!isset($_COOKIE['bid'])){
echo "COOKIE NOT SET";
}
else{
echo "COOKIE SET SUCCESSFULLY";
}
}
issue.php(in which i want to send id)
if(isset($_GET['issue'])){
$bookid=$_GET['isb'];
$dbser="localhost";
$use="[redacted]";
$pasw="[redacted]";
$db="[redacted]";
$con=mysqli_connect($dbser,$use,$pasw,$db);
mysqli_select_db($con,$db)or die("db not connected");
$userid=$_COOKIE['id'];
$id=$_SESSION['user']['username'];
$query = "select id from user_account where username='$id'";
$result=mysqli_query($con,$query);
$row= mysqli_fetch_assoc($result);
$uid=$row['id'];
echo "$uid";
echo "<br/>";
echo "$bookid";
$query = "INSERT INTO issue (bookid, userid)
VALUES ('$bookid', '$uid')";
mysqli_query($con, $query)or die(mysqli_error($con));
What you need is a way to select a single row then submit that with the form. That can be accomplished by adding a Radio button to your table inside the form. The user will check the radio button for the item they want then click the submit button.
Here is an example of what that code could look like for your page.
display books
<form action='issue.php' method='get'>
while ($row= mysqli_fetch_array($result)) {
$isbn=$row['isbn'];
echo " <table>";
echo "<tr>";
echo "<td><input type=\"radio\" name=\"optradio\" value=\"".$isbn."\"></td>";
echo "<td> NAME</td><td> ".$row['name']."</td>";
echo "<td> AVAILABILITY</td><td> ".$row['availabilty']."</td>";
echo "<td> CATEGORY</td><td> ".$row['category']."</td>";
echo "</tr>";
echo "</table>";
}
echo "<button type='submit' name='issue'>issue</button>";
echo "</form>";
Here is a HTML snippet so you can see what that PHP code would output in HTML. Click "Run snippet code" below to see the preview.
<form action='issue.php' method='get'>
<table>
<tr>
<td><input type="radio" name="optradio" value="1"></td>
<td> NAME</td>
<td> name1</td>
<td> AVAILABILITY</td>
<td> availability1</td>
<td> CATEGORY</td>
<td> category1</td>
</tr>
<tr>
<td><input type="radio" name="optradio" value="2"></td>
<td> NAME</td>
<td> name2</td>
<td> AVAILABILITY</td>
<td> availability2</td>
<td> CATEGORY</td>
<td> category2</td>
</tr>
</table>
<button type='submit' name='issue'>issue</button>
</form>
Then in issue.php you would look for $_GET['optradio'] to get the selected value.
if(isset($_GET['issue'])){
$bookid=$_GET['optradio'];
$dbser="localhost";
...
...
...
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;
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
heloo i have an ajx call function which brings information from a dropdown populated into a table with text inputs by ajax.
i was wondering if there was anyway that i could update the record in the database by using these text fields and the UPDATE function i am relativity new and the internet didnt bring much to light.
i have a button appearing in this table from a drop down but as far as i am aware you cannot use forms within php and the page this would have been submitted from is already submitting a php function and 2 can not be submitted at once.
i was wondering if it was possible that when the data in the textboxes below is changed when the user clicks the button those details are updated in the database?
im new to ajax and php so help would be amazing.
ps. i know this isnt secure i want it to be functional first and before it goes live i will secure it.
here is the code:
<?php
$q = $_GET['q'];
$con = mysqli_connect('server','uid','pwd','dbname');
if (!$con)
{
die('Could not connect: ' . mysqli_error($con));
}
mysqli_select_db($con,"account.php");
$sql="SELECT * FROM account WHERE name = '".$q."'";
$result = mysqli_query($con,$sql);
echo "<table border='1'>
<tr>
<th>Your Name</th>
<th>Your Email</th>
<th>Your Password</th>
<th>Your User Level</th>
<th>Save Changes</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td> <input type='text' name='txt_yourname' id='txt_yourname' value='" .$row['name']."' required='required' /> </td>";
echo "<td> <input type='text' name='txt_email' id='txt_email' value='" .$row['email']."' required='required' /> </td>";
echo "<td> <input type='text' name='txt_password' id='txt_password' value='" .$row['password']."' required='required' /> </td>";
echo "<td> <input type='text' name='txt_userLevel' id='txt_userLevel' value='" .$row['user_level']."' required='required' /> </td>";
echo "<td> <input type='button' name='btn_user' id='txt_user' type='submit' value='cheese'/> </td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
These statements execute user input, opening you up to a SQL Injection attack. You'll want to not do this.
$q = $_GET['q'];
$sql="SELECT * FROM account WHERE name = '".$q."'";
$result = mysqli_query($con,$sql);
To answer your question, in order to update a row like you want you'll need a way to identify in the database the row that has changed. One way to do it would be to include the row's primary key as a field/attribute in the HTML table row, but I'll leave it to someone more well versed in this area to say whether that's a good idea.
You're also going to want to escape and check the type of all of the fields the user can input when you go to do the update.
I'm trying to create a checkout page in PHP. The page that lists the items is called shop.php. On this page, I display a number items to be sold with a checkbox beside each item. I'm pulling the item name and price information from the database. Here's my main code.
$row = mysql_fetch_array($query);
while($row) {
echo "<TR>";
echo "<TD><B>$row[Name] </B></TD>";
echo "<TD><B>"."$"."$row[Price] </B></TD>";
echo "<TD><input type=checkbox name=foods[] value='$row[Price]' /></TD>";
echo "</TR>";
$row = mysql_fetch_array($query);
}
<input type="submit" name="buy" value="Buy Now" />
So I am setting the name of the checkboxes to foods[] which'll be an array that'll contain values of all checkboxes submitted. When the user clicks on the "Buy Now" button, they'll be redicted to the cart.php page, that'll display the name and price of the item they've purchased. I know that I can output the prices by looping through the $items array. The problem I have is that I don't understand how would I display the item names. I thoght of doing the following:
$row = mysql_fetch_array($query);
while($row) {
echo "<TR>";
echo "<TD><B>$row[Name] </B></TD>";
echo "<TD><B>"."$"."$row[Price] </B></TD>";
echo "<TD><input type=checkbox name='$row[Name]' value='$row[Price]' /></TD>";
echo "</TR>";
$row = mysql_fetch_array($query);
}
This time, both the name and the price of the items are included in the checkbox as its name and value. But again, the problem is how would I display them on the cart.php page without hard coding. What I means is that how would I display the item name and its price without doing something like this:
(Assume that one of the items name is Burger which was populated as the name of the checkbox.)
if (isset($_POST['Burger'])){
echo "<td>$_POST['Burger']</td>";
}
I'd appreciate any suggestions/tips on this question. Thank you.
If you name the checkboxes like:
<input type='checkbox' name='foods[{$row['Name']}]' value='{$row['Price']}' />
You can access the array $_POST['foods'] and the key/value will be Name/Price. So you can do:
foreach($_POST['foods'] as $name => $price) {
echo "<tr><td>$name</td><td>$price</td></tr>";
}
Set name to each checkbox as foods[$row[name]]. You should have following code:
$row = mysql_fetch_array($query);
while($row) {
echo "<TR>";
echo "<TD><B>$row[Name] </B></TD>";
echo "<TD><B>"."$"."$row[Price] </B></TD>";
echo "<TD><input type=checkbox name='foods[{$row[Name]}]' value='$row[Price]' /></TD>";
echo "</TR>";
$row = mysql_fetch_array($query);
}
<input type="submit" name="buy" value="Buy Now" />
On POST you will have associated array of item names and prices.