Simple Form Submission Doesn't Work

The file test2.php consists of these simple forms:

<?php

for($i=0; $i<=5; $i++){

echo "<form action='reset4.php' method='get' name='yourForm'>";

echo "<button type='submit' value='delete' name='remove_" . $i . "' class='deletebtn'>X</button>";

echo "</form>";

}

?>

It submits to reset4.php which is this simple code:

<?php
header("Location: test2.php");
exit;


for($i=0; $i<=5; $i++){
if (isset($_REQUEST["remove_$i"])){
echo "Deleted";
}}

?>

But it doesn’t work. The $_REQUEST doesn’t populate the address field and it apparently never gets submitted to reset4.php like it should. This is such a simple program, I can’t imagine why it doesn’t work.

Looks like you are redirecting directly to test2.php as soon as test4 is loaded:

header("Location: test2.php");
exit;

That is it exactly. OP was also given the answer on another forum.

When I remove exit it still doesn’t work.

Who cares if you are removing exit() …

the line before it you are REDIRECTING your browser back to the first php script/page…

Why dont you try to explain what it is you want or are trying to do?

…none of this makes sense…

What does your line of ‘DELETED’ supposed to mean?

Script 1:

for($i=0; $i<=5; $i++){
	echo "<form action='reset.php' method='post' name='yourForm'>";
	echo "<button type='submit' value='remove_" . $i . "' name='remove_" . $i . "' class='deletebtn'>X</button>";
	echo "</form>";

}

Script 2:

for($i=0; $i<=5; $i++){
	if (isset($_REQUEST["remove_$i"])){
		//echo "Deleted";
		echo "Received Value: " . $_REQUEST["remove_$i"];
	}
}

In script 1 you are creating (several) forms… each with its own submit button.

The only ‘value’ you are are passing is the name/value of that submit button…

In script 2, you are receiving those variables/$_POST data…

(and doing nothing with it at all)… except looping through it… and (now) displaying it.

This is part of a bigger program that unsets the session when the button is clicked but it’s too much code so I just included the basic logic behind it.

The whole session?

a specific value in your session data? (be more clear)

for($i=0; $i<=5; $i++){
	if (isset($_REQUEST["remove_$i"])){
		//remove session data here (whatever that is)
	}
}

this one:
for($i=0; $i<=5; $i++){
if (isset($REQUEST["remove$i"])){
unset($SESSION["element_id$i"]);
}}

Are you not looking at the answers given?

1.) In your initial script… you have no unique values?? (this was updated in my answer)

2.) You arent even following your OWN naming conventions? (why?)


if (isset($_REQUEST["remove_$i"])){

vs


if (isset($_REQUEST["remove$i"])){

Pick ONE approach and stick with it… and use that same naming style in all references.

The underscore was part of my naming convention. Somehow it got suppressed when I posted.

but not in your other posts?

and you didnt even have a VALUE at all in your initial posts…

When I use the following, it still doesn’t work:
<?php

for($i=0; $i<=5; $i++){

echo "<form action='' method='get' name='yourForm'>";

echo "<button type='submit' value='remove_" . $i . "' name='delete' class='deletebtn'>X</button>";
echo "</form>";

}
for($i=0; $i<=5; $i++){
if (isset($_REQUEST["remove_$i"])){
echo "delete";
echo "Received Value: " . $_REQUEST["remove_$i"];
}
}

?>

Now it destroys the entire session not just the ones that were unset.

Using this:

<?php
session_start();

for($i=0; $i<=5; $i++){
	
if (isset($_REQUEST["remove_$i"])){

$_SESSION["quantity"] = array();
$_SESSION["element_id"] = array();


unset($_SESSION["quantity_$i"]);
unset($_SESSION["element_id_$i"]);
}
}
header("Location: cart5.php");

//exit;
?>

You are setting (or resetting) the $_SESSION array… why? (especially within the for() loop?)

this session data should be set elsewhere…

Where is this session data being set to begin with?

All this is so wrong… (sorry)

Look at this example:

session_start();
//lets create am array to be used in our session data
$tempSessionArray = ['value0', 'value1', 'value2', 'value3', 'value4', 'value5'];

//set session data with -something- for this exercise
$_SESSION['element_id'] = $tempSessionArray;

//pre-check:
echo 'PRE-SESSION CHECK: <br>';
var_dump($_SESSION['element_id']);
echo '<br><br>';


for($i=0; $i<=5; $i++){
	if (isset($_POST["remove_$i"])){
		//echo "Deleted";
		//echo "Received Value: " . $_REQUEST["remove_$i"];
		echo 'TARGET INDEX TO BE REMOVED: ' . $_SESSION["element_id"][$i] . '<br><br>';
		unset($_SESSION["element_id"][$i]);
	}
}

//re-order array  (not sure if needed/desired?)
$_SESSION['element_id'] = array_values($_SESSION['element_id']);

//post-check:
echo 'POST-SESSION CHECK: <br>';
var_dump($_SESSION["element_id"]);
echo '<br><br>';

My SESSION is defined like this:

for($i=0; $i<=5; $i++){
if (isset($_REQUEST["element_id_$i"]) ) {
$_SESSION["element_id_$i"] = $_REQUEST["element_id_$i"];
$id = $_SESSION["element_id_$i"];	
array_push($_SESSION["element_id"],$id);
}
$id = $_SESSION["element_id"];
}

and the form submits to:

if (isset($_REQUEST["remove_$i"]) ){

unset($_SESSION["quantity[$i]"]);
unset($_SESSION["element_id[$i]"]);
var_dump($_SESSION["element_id"]);
var_dump($_SESSION["quantity"]);
echo "Received variable " . $_REQUEST["remove_$i"];
echo 'TARGET INDEX TO BE REMOVED: ' . $_SESSION["element_id[$i]"] . '<br><br>';
}

The output is:

array(1) { [0]=> string(1) “1” } array(1) { [0]=> string(1) “1” } Received variable remove_1
Notice: Undefined index: element_id[1] in C:\xampp\htdocs\TopView\cart5.php on line 147
TARGET INDEX TO BE REMOVED:

You already removed session var… you cant echo it out if it does not exist

these:

unset($_SESSION["element_id[$i]"]);
var_dump($_SESSION["element_id"]);

Are not the same things

you are unsetting a var…

then var_dumping the array…

  • you should stick to one approach either a but of session var… or keep everything in one session array

You are already looping to display the contents of the cart, with a delete/remove/x button next to each item. The post method form for each delete button needs a hidden field that indicates that the operation is to delete an item from the cart, and a hidden field with the item id. You don’t need an extra for(){} loop, you don’t need to concatenate values, and in fact by using the incremental position of the items in the cart, rather than the item id, if someone has more than one tab open in their browser and submits a form in each tab, the wrong items will get removed from the cart. Keep It Simple (KISS.)

The following example code shows how to implement the add to cart and delete from cart operations. Learn from, copy/paste, or ignore any or all of it as you see fit -

<?php

// add to cart - insert new item in cart if it doesn't exist or add quantity if it does exist
// delete from cart

session_start();

// create an empty cart if cart doesn't exist
if(!isset($_SESSION['cart']))
{
	$_SESSION['cart'] = [];
}
	
// post method form processing code
if($_SERVER['REQUEST_METHOD'] == 'POST')
{
	// add to cart
	if($_POST['action'] == 'add')
	{
		// you need to define what should happen if an item is already in the cart - ignore, replace, or add quantities?
		// this example adds the quantities

		// cast input(s) to integers (non-integers will become zeros)
		$item_id = (int)$_POST['item_id'];
		$qty = (int)$_POST['qty']; // positive, negative, or zero values are valid at this point

		// if the input(s) are valid, use them
		if($item_id > 0)
		{
			// if item is not in the cart, create it with a zero quantity (actual quantity is set next)
			if(!isset($_SESSION['cart'][$item_id]))
			{
				$_SESSION['cart'][$item_id] = 0;
			}
			// add the submitted quantity to the quantity in the cart
			$_SESSION['cart'][$item_id] += $qty;
		} else {
			// a negative or zero item id indicates either a programming mistake or someone/something submitting their own values
			// code to log all the information about the current request/user and the improper value would go here...
		}
	}

	// delete from cart
	if($_POST['action'] == 'delete')
	{
		// cast input(s) to integers (non-integers will become zeros)
		$item_id = (int)$_POST['item_id'];

		// if the input(s) are valid, use them
		if($item_id > 0)
		{
			unset($_SESSION['cart'][$item_id]);
		} else {
			// a negative or zero item id indicates either a programming mistake or someone/something submitting their own values
			// code to log all the information about the current request/user and the improper value would go here...
		}		
	}
	
	// code for other actions would go here...

	// filter cart to only keep quantities > 0
	$_SESSION['cart'] = array_filter($_SESSION['cart'],
		function($val){
			return $val > 0;
		});
}
?>

<?php
// display cart
if(empty($_SESSION['cart']))
{
	echo 'The cart is empty.';
} else {

	// using the id's from the cart (see array_keys()) you would retrieve the other item information - names, descriptions, and prices needed to display the cart
	// for demo purposes, the following code just displays the id/qty from the cart
	foreach($_SESSION['cart'] as $item_id => $qty)
	{
		echo "ID: $item_id, Qty: $qty";
		// produce a delete form for each item
		?>
		<form method='post' style='display:inline;'>
		<input type='hidden' name='action' value='delete'>
		<input type='hidden' name='item_id' value='<?php echo $item_id; ?>'>
		<button type='submit'>X</button>
		</form>
		<br>
		<?php
	}
}

// display a simple item selection form with 10 made up items. you would instead retrieve item information and display it with a separate add to cart form for each item.
?>
<form method='post'>
<input type='hidden' name='action' value='add'>
<select name='item_id'>
<option value=''>Select an item</option>
<?php
foreach(range(1,10) as $item_id)
{
	echo "<option value='$item_id'>Item $item_id</option>";
}
?>
</select>
Quantity: <input type='number' name='qty' value='1'>
<input type='submit' value='Add To Cart'>
</form>
Sponsor our Newsletter | Privacy Policy | Terms of Service