cameronjonesweb
cameronjonesweb

Reputation: 2506

Form Action Running Without Clicking Submit

I have a page where you view what is in your cart (a work in progress obviously) and there is a button to clear your cart, and it works, but moving to another page in the site, whether by clicking a link or typing in the address bar, runs the form action. It also does it on the product page to add to the cart. How do I stop this happening so the action only runs when the button is pressed?

<div id="">
     <?php echo "You have " . $_SESSION['cartItems'];
       if($_SESSION['cartItems'] == 1){
        echo " item";
       } else {
        echo " items";
       }
       echo " in your cart amounting to $" . $_SESSION['cartPrice'];?>
       <form action="<?php session_destroy();?>"><input type="submit" value="Clear Cart"></form>
</div>

Upvotes: 0

Views: 350

Answers (2)

David
David

Reputation: 218960

This:

<form action="<?php session_destroy();?>">

probably isn't doing what you think it's doing.

session_destroy() isn't the action performed by the form. It's performed when the page is loaded, any time the page is loaded, regardless of the form. You're mixing server-side and client-side code in a way that doesn't really make sense.

The server-side code is interpreted on the server while the page is being rendered. All of it. The result of that code is rendered to the page. So what's happening here is that you're running session_destroy() every time the page loads, and then using the output of session_destroy() as your form action.

The output is a boolean, so your form tag probably ends up looking like this:

<form action="true">

which doesn't really do anything meaningful.

If you want to destroy the session on the form submit, the submit action needs to be to a page that destroys the session. Calling session_destroy() on the page itself like this will result in always calling session_destroy() any time the page is loaded. Not when you navigate away, but when the page loads.

Upvotes: 1

Robert
Robert

Reputation: 20286

After some time I undestand what you want to do. You can't destroy the session in action attribiute but you can put in action attribute the page where session will be destroyed

destroy.php

<?php
session_start();
session_destroy();
echo 'destroyed';

?>

Your main file

<div>
 <?php 
   echo "You have " . $_SESSION['cartItems'].($_SESSION['cartItems'] > 1 ? 'items' : 'item');
   echo " in your cart amounting to $" . $_SESSION['cartPrice'];?>
   <form action="destroy.php"><input type="submit" value="Clear Cart"></form>
</div>

You can also put in action ="" and use some hidden param and check if it should be destroyed for example:

One file solution:

 <div>
 <?php 
   session_start();
   if(isset($_POST['destroy']) && $_POST['destroy'] == 1) session_destroy(); //destroying session

   echo "You have " . $_SESSION['cartItems'].($_SESSION['cartItems'] > 1 ? 'items' : 'item');
   echo " in your cart amounting to $" . $_SESSION['cartPrice'];?>
   <form method="POST" action="">
      <input type="submit" value="Clear Cart"><input type="hidden" value="1" name="destroy">
   </form>
</div>

Upvotes: 1

Related Questions