0

I figured how i can get a dialog and seems like i need to use javascript but this doesnt let me follow up with php code depending on the answer unless there is a way?

<?php


$ime = "Marko";

echo <<< EOT
<SCRIPT language="JavaScript">

var hi= confirm("Do you really want to deactivate $ime?");
if (hi== true){
    <?php ?>
    alert("$ime Deactivated!");
    // I need to do php code here...
}else{
    alert("Skipping $ime");
    // I need to do php code here...
}


</SCRIPT>


EOT;

?>
Marko29
  • 1,005
  • 4
  • 14
  • 25
  • you cannot mix php and javascript like that, you would have to have javascript either load the next php page, or return some data through ajax. – Patrick Evans Jan 26 '14 at 14:51
  • I am probably doing it wrong i know so i am looking for a way to dialog yes or no and do proper action in php depending on answer or there is no way for this? – Marko29 Jan 26 '14 at 14:52
  • no you would have to show a dialog on one page and then submit the answer (through like a form) – Patrick Evans Jan 26 '14 at 14:54

5 Answers5

1

PHP is a server-side scripting language. You can not trigger it with JavaScript.

stashymane
  • 81
  • 1
  • 10
  • I dont care about javascript really, its just example of what i tried to do. – Marko29 Jan 26 '14 at 14:53
  • This can only be possible if you make a hidden iFrame (or similar) and direct it to a PHP file that executes the required code. – stashymane Jan 26 '14 at 15:00
1

Improving on @papirtiger's answer, you could use AJAX to pass onto the PHP code seamlessly without a page reload.

All AJAX really is is the way you can pass data from javascript to a PHP file. Because PHP is server side, any PHP script that needs to be executed MUST be on the server. PHP generates the script and sends it to the client and the connection between the client and the server is done when the generated page downloads. Javascript can continue its work as it is client side. But since it's on the client's machine, it can't run a PHP script inside it as it can't be interpreted. AJAX takes data from PHP and sends it like a Form would (using POST or GET variables), executes a separate PHP file on the server, grabs the response from the script and puts it into the javascript without the entire page reloading.

Here is what your script could look like.

<script>
    if (window.confirm("Are you sure?")){
        // Begin the AJAX function
        $.ajax({
            // Declare the type (GET or POST)
            type = "POST",
            // The local server location to the PHP script to get the response from
            url = "yes.php",
            // Data to send (in this case nothing needs to be sent)
            data = "",
            // Get the response if a script is executed successfully
            success: function(response) {
                // Display the response from the script in an alert box
                alert(response);
            }
        )};
    } else {
        // Rinse and repeat using another file
        $.ajax({
            type = "POST",
            url = "no.php",
            data = "",
            success: function(response) {
                alert(response);
            }
        )};
    }
</script>

You would also need to include the jQuery library in the head of your HTML otherwise this AJAX markdown will not work and you would have to result to traditional (and ugly/messy) pure javascript AJAX executions.

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>

Hope this helps! :)

SteppingHat
  • 1,199
  • 4
  • 19
  • 50
0

@StashCat´s answer is totally correct, however the pattern you are looking for is something like this:

<script>
if (window.confirm("Are you sure")){
    window.location("/yes.php");
} else {
    window.location("/no.php");
}
</script>

Note that the script that is running will continue until it exits. The user is then presented with the web page and the confirm box. When they click the yes or no they will be redirected and a new script will be run.

max
  • 96,212
  • 14
  • 104
  • 165
0

in a nutshell, its not a good solution for languages mix like this. According to a code you have, i can declare a global varaibale with php like <?php <script> var some = "123"; </script> ?> and then use it in client javascript code, but i'll repeat its bad approach. You'd better to refactor your code.

Evgeniy
  • 2,915
  • 3
  • 21
  • 35
0

Php code runs on Server, while javascript code runs on client i.e., your browser. So, you can't write code to display a dialog box at server side, But you can dynamically remove and add HTML elements using Ajax. You can code your dialog box in javascript and call an Ajax block. A typical example of Ajax(Ajax--> Async Javascript) code,

    <!DOCTYPE html>
    <html>
    <head>
    <script>
    function loadXMLDoc()
    {
    var xmlhttp;
    if (window.XMLHttpRequest)
    {// code for IE7+, Firefox, Chrome, Opera, Safari
      xmlhttp=new XMLHttpRequest();
    }
    else
    {// code for IE6, IE5
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange=function()
    {
     if (xmlhttp.readyState==4 && xmlhttp.status==200)
     {
     document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
     }
    }
    xmlhttp.open("GET","ajax_info.txt",true);
    xmlhttp.send();
    }
    </script>
    </head>
    <body>

    <div id="myDiv"><h2>Let AJAX change this text</h2></div>
    <button type="button" onclick="loadXMLDoc()">Change Content</button>

    </body>
    </html>
Sravan U
  • 67
  • 1
  • 7