0

I am using javascript confirm withing PHP if statement. I want if I click OK, execution should get out of the current if and move to execute the next code, which is an else if. It is not working. is there a way to do it?

if(.....){....}


else if(!isset($_POST['cpage']) || empty($_POST['cpage'])){
    echo '<script> 
    
    if(confirm("Do you want to proceed?")){
       break;//*both break and continue do not work!*
    }
    else{
    
         history.go(-1);
    }
   
    
    </script>';
}
    
else if(//*from break, evaluate this*){
//execute
}
  • `break;//*both break and continue do not work!*`...well in the **JavaScript** there's nothing to break out of. PHP runs on your server and has already finished by the time your Javascript is executing in your browser. – ADyson Sep 22 '22 at 09:47
  • Also, even this made any sense, in PHP `break` does not move you from an `if` to an `else if`...try reading the [manual](https://www.php.net/manual/en/control-structures.break.php). – ADyson Sep 22 '22 at 09:48
  • If you want to capture user input in this way in order to decide what to do next in PHP, you need to get that input in the browser, and use to generate a new HTTP request to the server, which will then start a PHP script to do whatever action is required. – ADyson Sep 22 '22 at 09:49
  • @ ADyson, Am not an expert, so I was just looking for how to move from that part, to the next else if when OK is clicked in the confirm box. – سامي أوكتش Sep 22 '22 at 10:06
  • well you can't because as I explained already, Javascript and PHP do not run at the same time. They don't even run in the same place. You may not be an expert but surely you noticed that these are two entirely separate programming languages? – ADyson Sep 22 '22 at 10:17
  • Anyway, in my previous comment I described the approach you need to take. Remember that a web application is a disconnected, stateless, client-server environment. The client and the server communicate via HTTP requests. So if you want to do something in the server (PHP) as a result of something that happened in the client (user / browser / JavaScript), you must send a HTTP request from the client to the server. From a browser you can do that by submitting a form, navigating to a URL or sending an asynchronous AJAX request. If you want to get really clever, you can consider websockets as well. – ADyson Sep 22 '22 at 10:19
  • Thank you. Let me see how I can redo it, or get rid of the confirm() – سامي أوكتش Sep 22 '22 at 10:31

0 Answers0