0

I have a js function, which is passed a fileid to delete by a php script. I don't know how to convert the javascript parameter from JavaScript to PHP variable.

Here is what I have done so far:

<script>
    function deleteFile(file)
    {
        var r = confirm("Are you sure you want to delete this file?");
        if (r === true)
        {
            <?php
            $idfile = file; // How to convert it??
            unlink(mysql_result(
               mysql_query("SELECT filepath FROM
                  file where idfile='$idfile'"), 0, 0))
               or die("Could not delete file");
            mysql_query("DELETE FROM file WHERE fileid=$idfile")
               or die("Cannot delete file");
            echo "File has been deleted successfully.";
            ?>
        }
    }
</script>

I have a button also:

echo "<button onclick=\"deleteFile($fileid)\">Delete</button>";

UPDATE

function deleteFile(file)
{
    var r = confirm("Are you sure you want to delete this file?");
    if (r === true)
    {   // doesn't go to deletefile.php
        $.ajax({
            url: "/deletefile.php",
            method: 'POST',
            data: {
                id: file
            }
        });
    }
}

enter image description here

5
  • 1
    One is server side, one is client side. There is no converting it. You have to do a call to the server. Commented Feb 11, 2014 at 14:18
  • 1
    it seems you are confused on how server side and client side code is working. server side code could not know what is happening on client side unless you tell server side by using technologies like ajax. Commented Feb 11, 2014 at 14:18
  • 2
    PHP runs on the server when the page is being constructed. Javascript runs on the client after PHP is done. You can't turn JS variables into PHP variables, because PHP isn't running any more. You need to send something to the server, either by submitting a form, following a link, or using AJAX. Commented Feb 11, 2014 at 14:18
  • 1
    You should make yourself familiar with Ajax (maybe jQuery) then you'll see where's the problem Commented Feb 11, 2014 at 14:20
  • 1
    also, look into mysqli. Your mysql statements are deprecated. Commented Feb 11, 2014 at 14:21

4 Answers 4

5

That won't work. JavaScript and PHP are totally separate entities that execute at different times.

PHP is a server-side language. The PHP code executes on your server and returns a response to the web browser.

JavaScript is a client-side language. It executes when the user is interacting with the page in their browser, after the PHP code has executed.

You'll need to write a separate PHP script that takes the ID of the file to delete, then use AJAX to send the request to delete it with the specified file ID.

Sign up to request clarification or add additional context in comments.

2 Comments

I don't know what is AJAX yet. Could I display a confrim to the user first before delete? How? by calling another page deletefile.php?id=303 for example?
@user1234524521 You'd essentially replace the PHP code you have in the if statement with JavaScript code to make an AJAX request (like a regular request to the server but there isn't a page reload involved) to whatever PHP script would handle deleting the file. Something like deletefile.php?id=303 would work, yes; you'd just need to get the id parameter out, then run the SQL to delete the file. There's an introduction to AJAX on the MDN here: developer.mozilla.org/en/docs/AJAX
3

You can’t put a Javascript variable in PHP, but you can make an AJAX to send $id_file:

$.ajax({
        url: "/action.php",
        method: 'POST',
        data: {
          id: file,
       }
}); 

Then in the PHP action you can use the $_POST['id'] and make the query.

2 Comments

Will this line redirect me to action.php??
yes, but u need the function. put the $.ajax in ur function. Php also can return a message. $.ajax({ url: "/action.php", method: 'POST', data: { id: file, } }) .done(function( data ) { console.log(data) } Data is the message, u can see more in api.jquery.com/jQuery.ajax and u can return a json with json_encode
3

It would be better to use AJAX for example with jQuery. Code you created can't work, that way. Try this.

Generating button with id

<?php
  echo '<button class="delete_button" data-id="'.$id.'">delete me!</button>';
?>

Download jQuery from here, save it into your project folder.

Sending post request using jQuery

<script type="text/javascript" src="/path/to/jquery.min.js">
<script type="text/javascript">

$(".delete_button").click(function() {
  var id = $(this).data("id");
  $.post( "handler.php",{del: id}, function( data ) {
     if(data)
      {
      alert("deleted successfully!");
      window.location = "/your/desired/url"; // redirect after success
      }
  }
});  </script>

deleting in handler.php

if(array_key_exists("del", $_POST))
{
// delete in mysql
}

1 Comment

How do I include jQuery??
1
function deleteFile(file)
{
    var r = confirm("Are you sure you want to delete this file?");
    if (r === true)
    {   // doesn't go to deletefile.php
        $.ajax({
            url: "/deletefile.php",
            method: 'POST',
            data: {
                id: file
            }
        })
        .done(function( data ) {
         console.log(data);
       });
    }
}

Php

<?php
    $idfile = $_POST['id']; // How to convert it??
    unlink(mysql_result(
       mysql_query("SELECT filepath FROM
          file where idfile='$idfile'"), 0, 0))
       or die("Could not delete file");
    mysql_query("DELETE FROM file WHERE fileid=$idfile")
       or die("Cannot delete file");
?>

doesn't go to deletefile.php ? maybe the url is not the correct

3 Comments

The browser console says: ReferenceError: $ is not defined
you have jquery? U can download jquery.com/download or <script src="//code.jquery.com/jquery-1.10.2.min.js" type="text/javascript"></script> or try <script src="code.jquery.com/jquery-1.10.1.min.js"></script>
@user1234524521 The $ is from jQuery. Have you included a reference to jQuery in your page?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.