How to adress the right html input field with php - php

I would like to delete an user from a file. I generate the html form with the php code. Now, it reads the user from a file ann puts it in a email input field. When I click the delete button after the user I choose, it should delete him out of the file. But I don't know how to do it that it takes the right user, because I generate it with a for.
From where does the php choose if the button is clicked?
By the way the $liste is an Array with all the users in it.
echo '<form id=\"myform\" name=\"myform\">';
for ($j = 0; $j < count($liste); $j++) {
echo '<input id=\"email\" type=\"email\" name=\"email\" required=\"required\" value=' . $liste[$j][0] . '>';
echo '<select name="permission">
<option value="admin">admin</option>
<option value="user">user</option>
</select>';
echo '<input type=\'submit\' name=\'submit\'>';
echo '<input type=\'submit\' name=\'delete\' value=\'delete\'>';
echo '</form>';
if(isset($_GET['submit'])){
echo "Option doesn't work yet";
}}
if(isset($_GET['delete'])){
for ($i = 0; $i < count($liste); $i++){
if ($liste[$i][0] == $liste[j][0]){
echo $liste[$i][0].'deleted';
}
}
}

Ok for the beggining you having some syntax to fix in your form tag (missing method to get and action to some page)
for ($j = 0; $j < count($liste); $j++) {
echo "<form id=\"myform\" name=\"myform\" action=\"\" method=\"GET\">";
echo '<input id=\"email\" type=\"email\" name=\"email\" required=\"required\" value=' . $liste[$j][0] . '>';
echo '<select name="permission">
<option value="admin">admin</option>
<option value="user">user</option>
</select>';
echo '<input type=\'submit\' name=\'submit\'>';
echo '<input type=\'submit\' name=\'delete\' value=\'delete\'>';
echo '</form>';
}
if(isset($_GET['submit'])){
echo "Option doesn't work yet";
}
if(isset($_GET['delete'])){
for ($i = 0; $i < count($liste); $i++){
if ($liste[$i][0] == $liste[j][0]){
echo $liste[$i][0].'deleted';
}
}
}

Related

php form saves only the last input field value

I have a simple table "Exams" with the columns id, title and a form with a variable amount of exam input fields.
But when one submit the form the last value will be saved triple times.
I suppose it's because of the $sql_insert statement with the same value.
How can i change the code that the different values are submitted in that $sql_insert statement?
echo '<form action="" method="post">';
for ($i = 1; $i <= $student['passed_exams']; ++$i) {
echo '<label>Exams '.$i.' :</label>';
echo '<input type="text" id="id['.$i.']" name="title" placeholder="passed Exam" />';
echo '<br />';
}
echo '<input type="submit" value=" Submit " name="submit" /></form>';
if (isset($_POST['submit'])) {
for ($i = 0; $i < $student['passed_exams']; ++$i) {
$sql_insert = "INSERT INTO exams (title) VALUES ('".$_POST['title']."')";
$dbConnection->query($sql_insert);
}
$dbConnection->close();
}
Php needs name as an array
echo '<form action="" method="post">';
for ($i = 1; $i <= $student['passed_exams']; ++$i) {
echo '<label>Exams '.$i.' :</label>';
echo '<input type="text" id="id['.$i.']" name="title['.$i.']" placeholder="passed Exam" />';
echo '<br />';
}
echo '<input type="submit" value=" Submit " name="submit" /></form>';
if (isset($_POST['submit'])) {
for ($i = 0; $i < $student['passed_exams']; ++$i) {
$sql_insert = "INSERT INTO exams (title) VALUES ('".$_POST['title'][$i]."')";
$dbConnection->query($sql_insert);
}
$dbConnection->close();
}
You need to make the title array first as
name="title['.$id.']"
Then you have to save it as
$_POST['title'][$id]

Store data from a dynamically generated form in an array

I have created a for loop that generates a form div repeatedly:
for ($i = 1; $i <= $noOfTanks; $i++) {
echo '<div class="';
echo $col;
echo '"><img id="tank" src="img/tank.svg" alt="Tank"></br>
<label for="tankName';
echo $i;
echo '">Tank Name ';
echo $i;
echo '</label>
<input type="text" id="tankName';
echo $i;
echo '" name="tankName';
echo $i;
echo '"></br>
<label class="rightT" for="tankVolume';
echo $i;
echo '">Tank Volume ';
echo $i;
echo '</label>
<input class="rightT" type="text" id="tankVolume';
echo $i;
echo '" name="tankVolume';
echo $i;
echo '"><p>L</p>
</div>';}
The code generated a number of inputs. I want to gather all this input values into two arrays tankName[], tankVolume[]. I am struggling to understand how to use $_POST to do this.
Thanks for any help.
Use array of inputs.
Example:
<input class="rightT" type="text" id="tankVolume1" name="tankVolume[]"/>

Sticky Select Option

I have the following PHP code:
<?php
echo '<select name="transact_day" id="transact_day">';
echo"<option value=''>Select Day</option>";
for($i= 1; $i<=31; $i++){
echo "<option value=". $i ." ";
if(isset($day) == "$i"){
echo 'selected = "selected" ';
}
echo ">$i</option>\n";
}
echo '</select>';
?>
I want it to be sticky but when a date is selected (e.g 10) and the form is submitted it's only the value 31 that is sticky (irrespective of the selected date). I have tried different options, searched through similar questions but I couldn't figure out what was wrong with the above code. Any suggestion? Thank
isset($day) returns a boolean value, so comparing this to $i is incorrect. Check if $day is set, then compare $day with $i:
echo '<select name="transact_day" id="transact_day">';
echo"<option value=''>Select Day</option>";
for($i= 1; $i<=31; $i++){
echo "<option value=". $i ." ";
if(isset($day) && ($day == "$i")){
echo 'selected = "selected" ';
}
echo ">$i</option>\n";
}
echo '</select>';

calculating the sum of an array php

Hi im currently doing making a website that sells games as a project but im having problems calculating the sum for the price of the games
I have this loop which displays the games added into the basket
cart.php
<?php
$count = 0;
while ($count < $numrow)
{
$row = $results -> fetch_assoc();
extract($row);
echo"<div>";
echo"<div class='recommended_games'>";
echo "<img src='images/".$gameIMG."' />";
echo "</div>";
echo '<div class="price_tag">';
echo '<div class="price_tag" name="price" method="POST">£'.$gamePrice. '</div>';
echo'</div>';
echo '<div id="update_form"><form action="updatebasket.php" method="POST" name="updateform">';
echo '<select name="quantity" id="quantity" />';
echo '<option value="1">1</option>';
echo '<option value="2">2</option>';
echo '<option value="3">3</option>';
echo '<option value="4">4</option>';
echo '<option value="5">5</option>';
echo '</select>';
echo '<input type="hidden" value="'.$gameID.'" name="gameid" id="gameid" />';
echo '<input type="submit" value="update" />';
echo '</form>';
echo '<div class="quantity_update">';
echo '<form action="remove_item.php" method="POST">';
echo '<input type="hidden" value="'.$gameID.'" name="gameid" id="gameid" />';
echo '<input type="submit" value="Remove Item" />';
echo '</form>';
echo '</div>';
echo '</div>';
echo"<img class='box1' src='Images/Grey-Banners.png' />";
echo"</div>";
$count = $count + 1;
}
echo '<div id="delete_all">';
echo '<form action="delete_cart.php" method="POST">';
echo '<input id="hide_button" type="submit" value="Clear All" />';
$a=array($gamePrice);
echo array_sum($a);
echo '</form>';
echo '</div>';
?>
this is where im trying to calculate the total price
$a=array($gamePrice);
echo array_sum($a);
The reason this doesnt work, is because $gamePrice never is an array (unless you didnt provide all code). In the loop, it gets set to a new value, after the loop only the last one is stored.
Based on some hints in your code, I guessing this is a cart and you're looping through the cart. An easy way to get a total is like this:
$total = 0;
while( $itemsThatWeLoop){
// Some code here
$total+= $gamePrice*$quantity;
}
You add a variable which increments with the product's price
To explain the while-only-last-value-saved:
$i=0;
while( $i<=10){
$i= $i+1;
}
echo $i;
Will give 10. All other iterations $i gets set to a new value. The original value is not saved

update quantity with one button php

Hello i am currently doing a project to make a website that sells game however one of my problems right now is updating the quantity with a single button. i have got it working with multiple update buttons next to the items that i would like to update but to make it more realistic, i would like to just have one button that will update all the item quantities in the basket table in my database.
This is my cart file, i have commented out the bit where it works with multiple update buttons.
<?php
$count = 0;
while ($count < $numrow)
{
$row = $results -> fetch_assoc();
extract($row);
echo"<div>";
echo"<div class='recommended_games'>";
echo "<img src='images/".$gameIMG."' />";
echo "</div>";
echo '<div class="price_tag">';
echo '<div class="price_tag">£'.$gamePrice. '</div>';
echo'</div>';
echo '<div id="update_form"><form action="updatebasket.php" method="POST" name="updateform" id="update_all">';
echo '<select name="quantity" id="quantity" />';
echo '<option value="1">1</option>';
echo '<option value="2">2</option>';
echo '<option value="3">3</option>';
echo '<option value="4">4</option>';
echo '<option value="5">5</option>';
echo '</select>';
//echo '<input type="hidden" value="'.$gameID.'" name="gameid" id="gameid" />';
//echo '<input type="submit" value="update" />';
echo '</form>';
echo '</div>';
echo '</form>';
echo"<img class='box1' src='Images/Grey-Banners.png' />";
echo"</div>";
$count = $count + 1;
}
echo '<input type="hidden" value="'.$gameID.'" name="gameid" id="gameid" form="update_all"/>';
echo '<input type="submit" value="update" form="update_all"/>';
?>
This is my updatebasket file which updates the quantity in the database
<?php
session_start();
require "dbconnect.php";
$memberID = $_SESSION['id'];
$quantity = $_POST['quantity'];
$gameID = $_POST['gameid'];
$connect->query($query);
$query = "UPDATE basket SET quantity = ".$quantity." WHERE gameid = ".$gameID." AND id = ".$memberID."";
$results = $connect->query($query);
mysqli_commit($con);
header('Location: cart.php');
?>
If I understand you correctly, you need to work with an array for this to be achieved, by changing your form and you can use a foreach to assemble your query to update each entry in the cart. So one button will update each item in the cart with the respective quantities. I did not test this code, this is how I would approach it.
Eample of the HTML Form Changes:
echo '<div id="update_form"><form action="updatebasket.php" method="POST" name="updateform" id="update_all">';
echo '<select name="quantity[]" id="quantity" />';
echo '<option value="1">1</option>';
echo '<option value="2">2</option>';
echo '<option value="3">3</option>';
echo '<option value="4">4</option>';
echo '<option value="5">5</option>';
echo '</select>';
//echo '<input type="hidden" value="'.$gameID.'" name="gameid[]" id="gameid" />';
//echo '<input type="submit" value="update" />';
echo '</form>';
echo '</div>';
echo '</form>';
echo"<img class='box1' src='Images/Grey-Banners.png' />";
echo"</div>";
$count = $count + 1;
}
echo '<input type="hidden" value="'.$gameID.'" name="gameid[]" id="gameid" form="update_all"/>';
echo '<input type="submit" value="update" form="update_all"/>';
?>
Query Eample:
foreach ($_POST['gameid'] as $row=>$id) {
$gameid = $id;
$newquantity = ($_POST['quantity'][$row]);
$query = "UPDATE basket SET quantity = ".$newquantity." WHERE gameid = ".$gameID." AND id = ".$memberID."";
$connect->query($query);
}
One solution would be to give your quantity <select> inputs unique names with the Game ID attached to the end (eg. "quantity_32"). This makes it easy to know how much of each game is in the cart.
// POST Example
$_POST['quantity_32'] = 1;
$_POST['quantity_31'] = 3;
$_POST['quantity_37'] = 2;
Front-End Form Change
echo '<select name="quantity_<?=$gameID?>" id="quantity" />';
Back-End Processing
Then on the processing page loop through the $_POST variables and find the quantity fields and grab their Game ID's.
foreach ($_POST as $key => $quantity) {
// Ignore non-quantity fields
if (preg_match('/quantity_([0-9]+)/i', $key, $result) !== 1) continue;
$quantity = (int)$quantity;
$game_id = (int)$result[1];
// Update Cart Quantity in DB
// ...
}
Important!
Please ensure you SQL-Escape all values you save into the DB. Hackers could do some nasty stuff if you don't.
This example shows how you can keep things safe using MySQLi Prepared Statements.
http://mattbango.com/notebook/code/prepared-statements-in-php-and-mysqli/

Categories