-2

I have a PHP code which does bunch of things so to keep things simple I have removed the unnecessary code from it.

In the below if block I need to create a pop up window with that $message on it and it should have ok and cancel button on it. Once I click ok button I need to execute some other php code in the same file if possible (or it can be another PHP file as well) and if I click cancel button then it should not do anything.

Below is my test.php file-

<?PHP
    // ..... some other code

    if (!isset($_SESSION['admin'])) {
        $text = "hello world";
        // create a pop up window with $message on it along with "ok" and "cancel" button
        echo "<script type='text/javascript'>confirm('$text');</script>";
    }

    // ..... some other code
?>

I can have either JS modal, or an HTML/CSS modal anything works as long as I can have some actions on ok and cancel button. Is this possible to do? I tried looking around but not able to figure it out on how to do this.

Update:

Below is my updated hello.php file where I used Oliver suggestion. I tried with below code where I have a javascript function secondUser but it shows popup as Sad :( - invalid session once I click ok button.

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js" type="text/javascript"></script>
<script>
function secondUser() {
    $.post( "ajax/test.php", function( data ) {
        if (data.success) {
            alert(data.message);
        } else {
            alert("Sad :( - invalid session");
        }
    });
}
</script>

<?PHP
    // ..... some other code

    if (!isset($_SESSION['admin'])) {
        $text = "hello world";
        // create a pop up window with $message on it along with "ok" and "cancel" button
        echo "<script type='text/javascript'>if (confirm('$text')) { secondUser(); };</script>";
    }

    // ..... some other code
?>

Now I am not able to figure it out what is wrong in my above code? I just want to execute my test.php using ajax in my other php file.

18
  • so you make an Ajax call or submit a form or navigate to a link. Commented Sep 8, 2020 at 1:25
  • a codeblock in the same file? probably not, but in another php file you could, just put a simple ternay on the confirm and simply add a location.href pointing to it. if it's a post you'd need a form or make an xmlhttprequest using post. Commented Sep 8, 2020 at 1:25
  • @epascarello I am wondering if you can give an example how to do this ? Commented Sep 8, 2020 at 1:32
  • @user1950349 you mean a ternary? its just a shorthand if else confirm() ? redirect : '' Commented Sep 8, 2020 at 1:34
  • @kevin I meant to say if you can provide me an example in the fiddle 3v4l.org Little bit code will work. Commented Sep 8, 2020 at 1:42

1 Answer 1

1

You can use jQuery library to make a request using AJAX. First thing is registering the button.

<button id="cool-btn">My button</button>

Once you have the button running, you can add this script to the html or JS file.

<script>
$(document).ready(function() {
    $("#cool-btn").click(function(){
        attemptRequest();
    });
});
</script>

Now you need to create a function called attemptRequest which makes an AJAX request for you to your PHP form.

function attemptRequest() {
    $.post( "ajax/test.php", function( data ) {
      if (data.success) {
          alert(data.message);
      } else {
          alert("Sad :( - invalid session");
      }
    });
}

Then you need to do the session checking in your PHP script called test.php in the ajax folder and return the appropriate data.

 $data = new stdClass();
 $data->success = false;
 $data->message = "";
 if (isset($_SESSION['pageadmin'])) {
     $data->message = "hello world";
     $data->success = true;
 }
 
 return json_encode($data);

Hope thats helpful :)

Sign up to request clarification or add additional context in comments.

5 Comments

Hi Oliver, I tried your code and in the sources (inspect) section I am seeing the following code On clicking Ok button, it says Sad :( - invalid session and its not going inside second_user.php file although its going inside secondUser() javascript function. I am wondering what changes I need to make in the fiddle so that it goes inside second_user.php file.
Hi user (you should change your name :) ) Could you show me how your files are now laid out?
I created a fiddle already and it's the same setup I have. I just have only one php file (admin.php) and in that php file I have everything (php+js). I know its not a best practice but I will try to change the structure later. I also updated the question with what I have tried.
I also updated my question with the details of what I tried.
Hi @user1950349 for this example could you please separate the PHP and JS/HTML files accordingly? I will gladly teach you to do it in one file in another question once we get the basics working. Apologies for the late response, ironically i usually am on stackoverflow on working days :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.