-2

I want execute below code in php;

var answer =window.confirm("Did You want to overwrite the file..."); 

Based on that "OK" or "CANCEL" ;

If that ok means I want execute below php code without using AJAX

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";
}
Donald Duck
  • 8,409
  • 22
  • 75
  • 99
siva gtk
  • 27
  • 1
  • 9
  • 1
    Short answer: without ajax, you can't do that. Long answer: you could create dynamic form and submit it then parse the answer... not a great idea. – Flash Thunder Mar 04 '17 at 11:03
  • `without using AJAX` - you can't ... http doesn't work that way – Jaromanda X Mar 04 '17 at 11:03
  • 1
    Why without AJAX? The javascript runs on the client and php runs on the server, so you're going to have to call back to the server to run it, and that means AJAX, or a hack. – Rik Lewis Mar 04 '17 at 11:03
  • 1
    You could use websockets, but that will be way more difficult than Ajax, and php would not be the best choice for it. – Lorenz Meyer Mar 04 '17 at 11:05

2 Answers2

0

One way I find is that you can pass a value to url then fetch it inside 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
    }

In our case it is id, if id is set to true then execute else whatever you want to do.

$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";
}

Credit for url parsing in javascript:

How can I get query string values in JavaScript?

Community
  • 1
  • 1
Abhishek Gurjar
  • 7,426
  • 10
  • 37
  • 45
0

You can't execute MySQL or PHP command inside javascript, what you can do instead is to make a PHP function that you can call by Ajax. The best way is by using jQuery or by redirecting the page with your PHP function in URL.

Please Tack a look : How to execute the code inside javascript confirm box

<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>
Community
  • 1
  • 1
Harshil Patel
  • 298
  • 2
  • 8