Capture value confirm with php


My problem is that I press cancel or press accept in the confirm, it enters me anyway in the if.

$comparar = true;
$comparar_js = "<script type='text/javascript'>confirm('¿Borrar?')</script>";




   echo("No entra en el if");
asked by Jopimar 08.12.2017 в 12:47

2 answers


Your approach is wrong.

You can not directly pass the value of JS on the client side to PHP on the server side.

And it gives you true because you're comparing true == 'cadenaLlena' < = See online


When you press the delete button, the alert appears and you confirm it or not and it takes you to the page where you want the request to be executed:

<script type="text/javascript">
  function borrar() {
    return window.confirm( '¿Borrar?' );

<form action="<?php $_SERVER[ 'PHP_SELF' ]; ?>" onsubmit="return borrar()" method="post">
  <!-- tus otros inputs -->
  <input type="submit" name="submitBorrar" value="Borrar">

Then to check if the request has been made borrar (your condition):

// Comprueba si esta establecido el submit 'submitBorrar'
if ( isset( $_POST[ 'submitBorrar' ] ) ) {

  $txt = 'Borramos';
else {

  $txt = 'No entra en el if';

echo $txt;
answered by 08.12.2017 / 13:11

You can not do something like that.

This is a very typical error in people who go from programming desktop applications to web programming (I do not know if this is your case).

What you have to be very clear about in web programming is that you have two different code execution environments, which execute code in completely separate environments (most of the times even physically: server computer and client computer with the browser) and at different times.

You have to always keep in mind the life cycle of a web petition:

  • The browser makes a request for a web page to the server

  • The server code (in your case PHP) that generates the code (HTML, javascript, css) that should be sent to the client is executed on the server. So, basically, the PHP code is code that generates code. But it does not execute this generated code, but it sends it to the browser so that it is he who executes it.

  • The server sends the generated code (HTML, Javascript, CSS) to the browser

  • The browser interprets and executes the received code and displays the result

  • This is the reason why from your PHP code, which is running on the server at time 2, you can not access the result of the Javascript code that runs on the browser at time 4.

    In your case, the PHP code what you would do is create the following output to send to the browser:

    $comparar = true;
    $comparar_js = "<script type='text/javascript'>confirm('¿Borrar?')</script>";
    echo($comparar_js); // Salida: "<script type='text/javascript'>confirm(...
    if($comparar==$comparar_js){ // Si true=="<script type='text...."
        echo("Hola"); // Esto no se puede dar nunca
        echo("No entra en el if"); // Salida: "No entra en el if"

    What is sent to the browser is therefore:

    <script type='text/javascript'>confirm('¿Borrar?')</script>
    No entra en el if

    In other words, I will show a confirm dialog with the text ¿Borrar? with whose result nothing is done and shows in the browser yes or yes the message No entra en el if .

    I hope it helps you clarify some ideas.

    answered by 08.12.2017 в 13:24