It depends whether you want to:
- Navigate to another script/page
- Pass the variable to a script without reloading the page
Either way we can use the GET
superglobal. With the second method, you could also use the POST
superglobal.
Back End: PHP
include('dbconnect.php');
// Use these if GET method was used
$uid = $_GET["uid"] ?? NULL;
$name = $_GET["name"] ?? NULL;
$total = $_GET["total"] ?? NULL;
// Use these if POST method was used
# $uid = $_POST["uid"] ?? NULL;
# $name = $_POST["name"] ?? NULL;
# $total = $_POST["total"] ?? NULL;
// Check values were submitted
if( $uid && $name && $total){
$sql = "DELETE FROM `cart` WHERE `uid` = ? AND `name`= ?";
$query = $mysqli->prepare($sql);
$query->bind_param("is", $uid, $name);
$query->execute();
// Check something was deleted
if( $mysqli->affected_rows ){
Updatedcart($id, $total);
}
}
else {
echo "Nothing submitted";
}
Front End: GET
: Method 1.1
Change your button to an a
tag:
<a href="/scriptPage.php?uid=<?=$uid?>&name=<?=$name?>&total=<?=$total?>">Remove</a>
Front End: GET
: Method 1.2.1
<form method="GET" action="scriptPage.php">
<input type="hidden" value="<?=$uid?>" name="uid">
<input type="hidden" value="<?=$name?>" name="name">
<input type="hidden" value="<?=$total?>" name="total">
<input type="submit" value="Remove">
</form>
Front End: POST
: Method 1.2.2
<form method="POST" action="scriptPage.php">
<input type="hidden" value="<?=$uid?>" name="uid">
<input type="hidden" value="<?=$name?>" name="name">
<input type="hidden" value="<?=$total?>" name="total">
<input type="submit" value="Remove">
</form>
Front End: GET
: Method 2.1
<button type="button" onclick='remove_item(<?=$uid?>, <?=$name?>,<?=$total?>)'>Remove</button>
function remove_item(id, name, total){
// Create the HTTP Request object
var xhr = new XMLHttpRequest();
// Set connection parameters and query string
xhr.open('GET', "scriptPage.php?uid="+id+"&name="+name+"&total="+total);
// Handle success
xhr.onload = function(){
if(this.status == 200){
// Do something
}
};
// Send request
xhr.send();
}
Front End: POST
: Method 2.2
<button type="button" onclick='remove_item(<?=$uid?>, <?=$name?>,<?=$total?>)'>Remove</button>
function remove_item(id, name, total){
// Create the HTTP Request object
var xhr = new XMLHttpRequest();
// Set connection parameters and query string
xhr.open('POST', "scriptPage.php");
// Handle success
xhr.onload = function(){
if(this.status == 200){
// Do something
}
};
// Set content type
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
// Send request
xhr.send(
"uid=" + id +
"&name=" + name +
"&total=" + total
);
}