Yohan Blake
Yohan Blake

Reputation: 1318

javascript to send value to $_POST in PHP

What I want to do is to use a confirm box where if the user clicks OK, to delete a row from my SQL database. I have a js function I use to send the value "delete" to a php function on the same file like this:

<script>
    function myFunction() {
        if (confirm("Are you sure you want to delete?") == true) {
            document.getElementById("delete").name = "delete";
        }else{
            return false;
        }
    }
</script>

The button where the user will click to delete an image is this:

<button onclick="myFunction()">Delete Image</button>

I send the value to the PHP function like this:

<input type="hidden" name="" value="delete" id="delete">

This is my PHP function:

if(isset($_POST['delete'])){
    $img_path=$_POST['ipath'];
    $imgid=$_POST['imgid'];
    $link = mysqli_connect($host, $username, $password, $db);
    $delete = "DELETE FROM images_info
                WHERE Image_Id = $imgid";
    $result3 = mysqli_query($link, $delete);
    echo "Image Deleted : $imgid";
    mysqli_close($link);
}

What am I doing wrong? I believe it's gotta do with the javascript

Upvotes: 1

Views: 3520

Answers (6)

Abhishek Sharma
Abhishek Sharma

Reputation: 3340

in your html, input name attribute is empty,

<input type="hidden" name="" value="delete" id="delete">

it should be like

<input type="hidden" name="delete" value="delete" id="delete">

$_POST['delete'] will not be set until the name attribute is provided with the value 'delete'

Upvotes: 6

Manav
Manav

Reputation: 1367

Do not use onclick="myFunction()" in the <input> tag. Instead, ensure:

<form method ="post" onsubmit="return confirm("Are you sure you want to delete?")">
    <button name = "delete">Delete Image</button>
</form>

  <?php
 if(isset($_POST['delete']))
{
     $img_path=$_POST['ipath'];
     $imgid=$_POST['imgid'];
     $link = mysqli_connect($host, $username, $password, $db);
     $delete = "DELETE FROM images_info WHERE Image_Id = $imgid";
     $result3 = mysqli_query($link, $delete);
     echo "Image Deleted : $imgid";
     mysqli_close($link);
 }
   ?>

Upvotes: 1

Yohan Blake
Yohan Blake

Reputation: 1318

Sorry for all this mess guys, the actual problem was that I had another form inside the form. I didn't know that forms cannot contain forms inside them. Removed the other form, and it works

Upvotes: 1

zfj3ub94rf576hc4eegm
zfj3ub94rf576hc4eegm

Reputation: 1273

Doesn't seem like anyone wants to answer your question. If you'd like to do this with pure js, here ya go:

var url = 'YOUR URL POST DESTINATION HERE';
function myFunction(elem){
    var id = elem.target.id;
    var xmlHttp = new XMLHttpRequest();
    var params = 'delete=true&imgid=' + id;

    xmlHttp.open('POST', url, true);
    xmlHttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
    xmlHttp.send(params);

}

You will need to set imgid in your js programmatically. Execute myFunction() from your page like

<button onclick="myFunction(this)">Delete Image</button>

As a side note, it's a REALLY bad idea to let users post to a form and modify your DB. ANYONE can make a post request and delete your DB. But I decided to give you the benefit of the doubt and answer your question.

Upvotes: 1

Sona
Sona

Reputation: 490

Use it like this. here is PhpFiddle

PHP CODE

  <?php
     if(isset($_POST['delete']))
    {
        // PERFORM YOUR DELETE QUERY HERE
        print_r($_POST);
     }
   ?>

JAVA SCRIPT

      <script>
      function myFunction() {
      if (confirm("Are you sure you want to delete?") == true) {
         document.getElementById("delete").name = "delete";

       }else{
       return false;
       }
     }
   </script>

HTML

     <form method="post" action="">
         <button onclick="myFunction()">Delete Image</button>
         <input type="hidden" name="" value="delete" id="delete">
     </form>

Upvotes: 2

Evelyn Kokemoor
Evelyn Kokemoor

Reputation: 326

The variable $_POST is an associative array obtained from a form's <input> tags. The keys into $_POST are defined by the name attributes of an input tag, and the corresponding values in $_POST are defined by the value attribute in the same tag. Data in $_POST does not come from JavaScript unless you use an AJAX library like jQuery. If you want to control this data with pure JavaScript, you must directly set the attributes of your input tags.

You can keep the same JavaScript code you're using, just set the input tag's value attribute instead of its name, which should always be delete. Then in your PHP code, you can test the value of $_POST['delete'] to find out if the user really wanted to delete.

Upvotes: 1

Related Questions