1

I am working on one project right now and got stuck at one problem. I am trying code below....

<button onclick = 'Remove(<?php echo $uid ?>,<?php echo $name ?>,<?php echo $total ?>)' type="submit">Remove</button>

This is i am using to pass the values to php function and below code is function in php.

function Remove($id,$name,$total){
        include('dbconnect.php');
        $q = "DELETE FROM `cart` WHERE `uid` = $id AND `Name`= '$name'";
        $r = mysqli_query($connect, $q);
        if($r == TRUE){
        Updatedcart($id,$total);
        }
   }

Can please anyone help me with this problem.....

  • It's called "http request"... – Honk der Hase Dec 25 '20 at 14:23
  • **Warning:** You are wide open to [SQL Injections](https://php.net/manual/en/security.database.sql-injection.php) and should use parameterized **prepared statements** instead of manually building your queries. They are provided by [PDO](https://php.net/manual/pdo.prepared-statements.php) or by [MySQLi](https://php.net/manual/mysqli.quickstart.prepared-statements.php). Never trust any kind of input! Even when your queries are executed only by trusted users, [you are still in risk of corrupting your data](http://bobby-tables.com/). [Escaping is not enough!](https://stackoverflow.com/q/5741187) – Dharman Dec 25 '20 at 22:56

3 Answers3

0

Short answer: No

Longer Answer: Yes, but not the way you have tried.

You can achieve the desired result by wrapping your button with form:

<form method="post">
   <input type="submit" name="button_submit" value="Remove"/>
</form>

<?php
    if(array_key_exists('button_submit', $_POST)) { 
        Remove($uid, $name, $total); 
    } 
?>

This will call the Remove function, after form submission and page update. If, however, you want to call the method without refreshing the page, then I suggest you take a look into Ajax post requests.

Abbas Akhundov
  • 562
  • 6
  • 12
0

Yes, it's possible. But before we delve into the details, it's important to know that your Javascript runs in your browser and your PHP runs on your server, which are possibly separate computers located far from each-other.

So, in order to pass your JS values to a PHP function you will need to send a request from your browser to the server, which is essentially a technical message. On the server you will need to receive the request and call your PHP function and once all this is done, your server will eventually send a message to the browser as a response to the request. This is usually done in the HTTP (HyperText Transmit Protocol) protocol, or, better, HTTPS protocol, where the S stands for "secure".

Sending the request

You can send a request from your browser using forms or AJAX requests. If you are using a form for this purpose, then you will need to have an element in that form, having a name attribute, which will represent the key you identify your value with. If you send an AJAX request, then you will need to pass the parameter in conformity with the standards you can read at many places over the Internet. You will also need to make sure that your server serves the target where your request is sent to.

Identifying a parameter value

Depending on your request method, you will be able to detect the parameter value on the server via $_GET["paramname"] or $_POST["paramname"].

Calling the function

Assuming that you have the parameter value stored in a variable, like $myparam, you can call your function as

yourfunction($myparam)

and you have passed your parameter to the PHP function.

WebSocket

Alternatively, you can use WebSockets, which are duplex channels, but maybe before that you need to study HTTP requests, both via form and via AJAX.

Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
0

It depends whether you want to:

  1. Navigate to another script/page
  2. 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
    );
}
Steven
  • 6,053
  • 2
  • 16
  • 28