简体   繁体   English

将JavaScript函数参数转换为php变量

[英]Converting JavaScript function argument to php variable

I have a js function, which is passed a fileid to delete by a php script. 我有一个js函数,该函数传递了一个fileid以通过php脚本删除。 I don't know how to convert the javascript parameter from JavaScript to PHP variable. 我不知道如何将JavaScript参数从JavaScript转换为PHP变量。

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. JavaScript和PHP是完全独立的实体,它们在不同的时间执行。

PHP is a server-side language. PHP是一种服务器端语言。 The PHP code executes on your server and returns a response to the web browser. PHP代码在您的服务器上执行,并将响应返回到Web浏览器。

JavaScript is a client-side language. JavaScript是一种客户端语言。 It executes when the user is interacting with the page in their browser, after the PHP code has executed. 在PHP代码执行之后 ,当用户与浏览器中的页面进行交互时,它将执行。

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. 您需要编写一个单独的PHP脚本,该脚本使用要删除的文件ID,然后使用AJAX发送具有指定文件ID的删除请求。

You can't put a Javascript variable in PHP, but you can make an AJAX to send $id_file : 您不能在PHP中放置Javascript变量,但是可以使AJAX发送$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. 然后,在PHP操作中,您可以使用$_POST['id']进行查询。

It would be better to use AJAX for example with jQuery. 最好将AJAX与jQuery一起使用。 Code you created can't work, that way. 这样创建的代码无法正常工作。 Try this. 尝试这个。

Generating button with id 产生ID的按钮

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

Download jQuery from here , save it into your project folder. 这里下载jQuery,将其保存到您的项目文件夹中。

Sending post request using jQuery 使用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 在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 p

<?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 ? 不去deletefile.php吗? maybe the url is not the correct 网址可能不正确

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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