How can I remove all items from the cart in PHP? - php

I already have some items in the cart. I want to remove them all. So, I've created a submit button with post method called 'remove_all', following doesn't work, it says "The requested URL was not found on this server." Can please someone help me?
<?php
if (isset($_POST['remove_all'])){
unset($_SESSION['cart']);
echo "<script>window.location = 'cart.php'</script>";
}
?>
And here what I wrote in my index.php
<form action="remove_all" method="post" class="cart-items">
<button type="submit" value="Очистить всё" name="remove_all" class="btn btn-danger mx-2">Очистить всё</button>
</form>

It looks like you you not linking properly to your remove_all.php file. Try this:
<form action="/remove_all.php" method="post" class="cart-items">
<button type="submit" value="Очистить всё" name="remove_all" class="btn btn-danger mx-2">Очистить всё</button>
</form>
Also in your PHP file, you do not need to mix PHP and js - simply change header as following:
<?php
if (isset($_POST['remove_all'])){
unset($_SESSION['cart']);
// echo "<script>window.location = 'cart.php'</script>";
header("Location: cart.php");
}
?>

Related

delete button in PHP website behaving like static when clicked

i have a simple delete button in PHP which uses simple SQL query to delete the data from database, I have used the following code:
$myid = $this->uri->segment('3');
if (isset($_POST['submit'])){
$ret = mysqli_query($con,"delete * from smart_category where name='$myid'");
header("Location: https://cloudclassmate.com/jewelry-catalogue/admin/viewcategory");
}
<div style="margin-left:38%" class="clearfix">
<a href="https://cloudclassmate.com/jewelry-catalogue/admin/viewcategory">
<button type="button" class="cancelbtn bemine">Cancel</button></a>
<form action="" method="post" ><button type="submit" name="submit" class="deletebtn bemine">Delete</button></form>
</div>
the problem here is if I click the delete button, its not responding, nothing is happening. the button is like static. can anyone please tell me what could be wrong here
If any Javasscript is not involved, than just put form action.
Also send myid in the request or in URI.
please see below:
<div style="margin-left:38%" class="clearfix">
<button type="button" class="cancelbtn bemine">Cancel</button>
<form action="abc.php" method="post" ><button type="submit" name="submit" class="deletebtn bemine">Delete</button></form>
</div>
You should add an action url/path to where your PHP file is located and add a Method to your form. Also, I noticed you deleting an entry where "name" is equal to $myid please check if that is correct.
<div style="margin-left:38%" class="clearfix">
<button type="button" class="cancelbtn bemine">Cancel</button>
<form action="url-to-server.php" method="post" >
<button type="submit" name="submit" class="deletebtn bemine">Delete</button>
</form>
</div>
$myid = $this->uri->segment('3');
if (isset($_POST['submit'])){
$ret = mysqli_query($con,"delete * from smart_category where name='$myid'");
header("Location: https://cloudclassmate.com/jewelry-catalogue/admin/viewcategory");
}

Redirect to specific url on button click in php file instead of to default woocommerce shop

I'm currently trying to change on a default php button function that redirects users to webshop so that it can redirect users to a custom url instead. This is the code responsible for this
<?php $proceed_booking_text = efora_get_field('proceed_booking_text');
if(!empty($proceed_booking_text)){ ?>
<div class="form-group">
<?php if($product->is_in_stock()){ ?>
<button type="submit" class="tg-btn tg-btn-lg btn-cart-ajax"><span><?php echo esc_attr(efora_get_field('proceed_booking_text')); ?></span></button>
<?php } else{ ?>
<button type="submit" disabled="disabled" class="tg-btn tg-btn-lg"><span><?php esc_attr_e('Not Available','efora'); ?></span></button>
<?php } ?>
</div>
<?php } ?>
I have tried the the GOTO statement but only got a php error so I'm thinking I didn't use the statement properly.

How do I use variables after header() redirect?

I've spent ages trying to figure this out after reading guide after guide and I can't get it. I have 3 files - user.php, map.php and newaddress.php. I've only included what I understand to be the important bits to this issue. Happy to provide more info if needed.
user.php passes a "mapnumber" to map.php.
user.php
<form action="map.php" method="post">
<input type="hidden" name="mapnumber" value="'. $row["mapnumber"].'"/>
<input type="hidden" name="process" value="process"/>
<button class="btn btn-primary" type="submit" name="getmap">Process</button>
</form>
map.php receives "mapnumber" and generates a list of addresses from the database with matching "mapnumber". From map.php, user can add new addresses which take the "mapnumber" value and is processed in newaddress.php.
map.php
$mapnumber = $mysqli->escape_string($_POST['mapnumber']);
<form action="newaddress.php" method="post">
<input class="" id="mapnumber" name="mapnumber" value="<?php echo $mapnumber ?>">
<button class="btn btn-primary" id="addaddress" type="submit">Add Address</button>
</form>
newaddress.php adds the address to database with the "mapnumber" value and then redirects back to map.php where it SHOULD generate the list of addresses based on "mapnumber" but map.php doesn't pick up the "mapnumber" from newaddress.php and hence the list of addresses isn't generated.
newaddress.php
$mapnumber = $mysqli->escape_string($_POST['mapnumber']);
header("Location: map.php");
exit;
Please help
You can set the get variable like this:
$mapnumber = $mysqli->escape_string($_POST['mapnumber']);
header("Location: map.php?nr=". $mapnumber);
exit;
Than in map.php you get get it by:
$mapnumber = $mysqli->escape_string($_GET['nr']);
You could use cookies to store values in the remote browser.
But the real problem you encounter is that the Location redirect is just that. It tells the browser to load another URL.
You can add get requests to that Location redirect URL like that:
header("Location: map.php?mapnumber=".urlencode($mapnumber));
But please keep in mind that the default Location redirect will be a permanent one (HTTP Status Code 301) and it is up to the browser to recheck if the redirecting url/document might have changed or not.
http://php.net/manual/en/function.header.php#78470
So you might want to consider using a 307 redirect:
header("Location: map.php?mapnumber=".urlencode($mapnumber), TRUE,307);
Anyway: passing variables from one page to another is generally not a good practice. Try to avoid it and try to dig into sessions.
http://php.net/manual/en/intro.session.php
I recommend you to use get method in map.php
Then it will receive map number like map.php?mapnumber=somenum
user.php
<form action="map.php" method="get">
<input type="hidden" name="mapnumber" value="'. $row["mapnumber"].'"/>
<input type="hidden" name="process" value="process"/>
<button class="btn btn-primary" type="submit" name="getmap">Process</button>
</form>
map.php
$mapnumber = $mysqli->escape_string($_GET['mapnumber']);
<form action="newaddress.php" method="post">
<input class="" id="mapnumber" name="mapnumber" value="<?php echo $mapnumber ?>">
<button class="btn btn-primary" id="addaddress" type="submit">Add Address</button>
</form>
newaddress.php
$mapnumber = $mysqli->escape_string($_POST['mapnumber']);
header("Location: map.php?mapnumber=$_POST['mapnumber']");
exit;

Codeigniter link button to another view not working?

<button onclick="location.href='<?php echo base_url();?>Guesser/guess'">Start!</button>
Just displays an empty web page with about:blank as the url
Try this:
<input type="button" onclick="location.href='<?php echo base_url();?>Guesser/guess'" value="Start!">

included php code is not executing on submit

I'm including a a php code with angular ui routing
the problem is if the millitary.php :
`
<div class="jumbotron">
<div class="page-header">
<h1>Millitary Loot Table</h1>
</div>
<?php
$MillitaryLoot = array(
'M4A4' => 47,
'AWP' => 2,
'Karambit' => 1,
'Famas' => 50
);
$newMillitaryLoot = array();
foreach ($MillitaryLoot as $item=>$value)
{
$newMillitaryLoot = array_merge($newMillitaryLoot, array_fill(0, $value, $item));
}
$myLoot = $newMillitaryLoot[array_rand($newMillitaryLoot)];
if (isset($_POST['submit']))
{
echo "\n" . "<h2 style='display:inline;'>Item: </h2>" . '<p class="text-center text-danger">' . $myLoot . '</p>' . "</br>";
}
?>
<form name="Gamble" action="" method="post" target="_self">
<button name="submit" type="submit" class="btn btn-primary">Gamble</button>
</form>
</br>
</div>
`
the include app.js
$stateProvider
.state('millitary', {
url: '/millitary',
templateUrl: 'inc/millitary.php'
})
is included into the index.php the submit button basicly not executes the code above the form
so basically i want the php code to execute in the index.php to keep the ui instead of jumping into the executing php file and losing the index.php linked styles and navigation
edit : if i run the code normaly it works but it doesn't if it gets included
edit2 : everything outside the if statement works basically if i add a echo "test"; it outputs but inside the if it does not so the problem is the form and the connection to the if statement
kind regards, daniel
Change your HTML to the following:
<form id="form" name="Gamble" method="post">
<button id="form" name="submit" type="submit" class="btn btn-primary">Gamble</button>
</form>
You can submit your form with a button with HTML5 but it must share the same ID of the form. Also keep in mind this will only be supported with browsers that support HTML5 so if not they won't be able to submit the form.
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button
The other alternative is to use...
<input name="submit" type="submit" class="btn btn-primary" value="Gamble">
as #gbestard suggested.
Change
<button name="submit" type="submit" class="btn btn-primary">Gamble</button>
to
<input name="submit" type="submit" class="btn btn-primary" value="Gamble" />
In order to submit you need a type submit input or use some client side script as javascript

Categories