Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/275.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP:如何在javascript确认框后运行PHP代码?_Javascript_Php_Html_Ajax - Fatal编程技术网

PHP:如何在javascript确认框后运行PHP代码?

PHP:如何在javascript确认框后运行PHP代码?,javascript,php,html,ajax,Javascript,Php,Html,Ajax,我想在php中执行下面的代码 var answer =window.confirm("Did You want to overwrite the file..."); 基于“确定”或“取消” 如果这表示我希望在不使用AJAX的情况下执行下面的php代码 if($uploadedby==$name) { move_uploaded_file($file_loc,$folder.$file); $sql="update pubfiles SET filename='$file'

我想在php中执行下面的代码

var answer =window.confirm("Did You want to overwrite the file..."); 
基于“确定”或“取消”

如果这表示我希望在不使用AJAX的情况下执行下面的php代码

if($uploadedby==$name)
{
    move_uploaded_file($file_loc,$folder.$file);
    $sql="update  pubfiles SET filename='$file',owner_name='$name',upload_time='$file_time',size='$file_size' WHERE content='$unique';";

    $qry=mysql_query($sql,$conn); 
    if($qry>0)$check="yes";
}

我发现的一种方法是,您可以将一个值传递给url,然后在php中获取它

function getParameterByName(name, url) {
        if (!url) {
          url = window.location.href;
        }
        name = name.replace(/[\[\]]/g, "\\$&");
        var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
            results = regex.exec(url);
        if (!results) return null;
        if (!results[2]) return '';
        return decodeURIComponent(results[2].replace(/\+/g, " "));
    }

    var id = getParameterByName('id');

    if(!id)
    {
        var id =window.confirm("Did You want to overwrite the file...");        
        window.location.href = "test.php?id="+id
    }
在我们的例子中,它是id,如果id设置为true,那么执行else,不管您想做什么

$id = $_GET['id'];

if($uploadedby==$name && $id == true)
{
    move_uploaded_file($file_loc,$folder.$file);
    $sql="update  pubfiles SET filename='$file',owner_name='$name',upload_time='$file_time',size='$file_size' WHERE content='$unique';";

    $qry=mysql_query($sql,$conn); 
    if($qry>0)$check="yes";
}
javascript中url解析的信誉:


您不能在javascript中执行MySQL或PHP命令,您可以做的是创建一个可以由Ajax调用的PHP函数。最好的方法是使用jQuery,或者使用URL中的PHP函数重定向页面

请看一看:


if(确认(“这似乎是重复的。是否要继续?”)
{
$.ajax({
url:'your_path_to_php_function.php',
键入:“POST”、//或您喜欢的任何HTTP方法
数据:{data:'any_external_data'}
})
.完成(功能(数据){
//做某事
});
}
其他的
{
window.location='your_url?yoursecialtag=true'
}

简短回答:没有ajax,您就无法做到这一点。长答案:您可以创建动态表单并提交它,然后解析答案。。。这不是一个好主意。
不使用AJAX
-您不能。。。http不能这样工作为什么没有AJAX?javascript在客户机上运行,php在服务器上运行,因此您必须调用服务器来运行它,这意味着AJAX或黑客。您可以使用WebSocket,但这比AJAX困难得多,php不是最佳选择。
<script type="text/javascript">  
    if (confirm("This seems to be Duplicate. Do you want to continue ?"))
    {
        $.ajax({
            url: 'your_path_to_php_function.php',
            type: 'POST', // Or any HTTP method you like
            data: {data: 'any_external_data'}
        })
        .done(function( data ) {
            // do_something()
        });
    }
    else
    {
        window.location = 'your_url?yourspecialtag=true'
    }
 </script>