简体   繁体   中英

Converting JavaScript function argument to php variable

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
            }
        });
    }
}

在此处输入图片说明

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.

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.

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
}
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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM