Why does false work as true?

  • 0
    $.ajax({
                url: '/action_ajax_form.php',
                type: "POST",
                data:  $("#feedback-form_03").serialize(),
                success: function(response) {
    				if (response) {console.log(response);
    				console.log('<div id="message_03">Ваше сообщение успешно отправлено. Спасибо!<br />Мы свяжемся с вами ближайшее время!</div>');
    				}
                }
    }
            })


    How is it possible false comes, but
    if (response) {console.log(response);
    console.log('<div id="message_03">Ваше сообщение успешно отправлено. Спасибо!<br />Мы свяжемся с вами ближайшее время!</div>');

    a message is sent to the console

    in the php file the following code
    $url = 'https://www.google.com/recaptcha/api/siteverify';
    $params = [
        'secret' => 'sdfgsdfgsdfgsdfg-VV',
        'response' => $captcha_token,
        'remoteip' => $_SERVER['REMOTE_ADDR']
    ];
     
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
     
    $response = curl_exec($ch);
    if(!empty($response)) $decoded_response = json_decode($response);
     
    $success = false;
     
    if ($decoded_response && $decoded_response->success && $decoded_response->action == $captcha_action && $decoded_response->score > 0) {
        $success = $decoded_response->success;
        // обрабатываем данные формы, которая защищена капчей
    } else {
        // прописываем действие, если пользователь оказался ботом
    }
    JavaScript Anonymous, May 8, 2020

  • 3 Answers
  • 0
    String false, of course converted to true.

    In PHP it will be exactly the same, the if ('false') condition will be executed.



    And in order for false to be perceived as a boolean value, and not as a string, you either need to tell your javascript that it is waiting for specially encoded data, or PHP - that it does not send HTML, but some other format .



    In general, the abundance of commentators with "finger-to-the-sky" advice clearly shows the average user's level of understanding of their actions.



    In general, the direction of thought is correct - the system perceives the transmitted value as a string, and not as a boolean value. But the thought stops there.

    Although, with some effort, one could go on and reason - that since the HTTP protocol is purely textual , no boolean values ​​can be passed through it by default. As well as objects, arrays, and other data types.

    And in order to make this possible, a special encoding format was invented. Which allows you to transfer data of various types, and which the author, undoubtedly, already uses (as a result, in fact, the string false is obtained from the boolean value).



    That is, the only missing piece in this puzzle is the context , depending on which javascript will treat the string false as either a string or a boolean value. That is, you need a way for PHP and Javascript to agree on the format in which they exchange data. The standard way is the HTTP Content-type header. Which will tell the browser that the data is not in the default HTML format, but in another.



    Or, as an option, you can explicitly set the format in which the data will come directly when calling the ajax function. If you attend to studying the documentation for this function.
    Max Salinas

  • 0
    You have false as a string coming in, not as a boolean. In php, do

    echo json_encode ($ success);
    Ivy McCullough

  • 0
    Most likely due to the fact that false comes in the form of a string, and the condition is satisfied accordingly, because any string is already true, try something like this:

    $.ajax({
    url: '/action_ajax_form.php',
    type: "POST",
    data: $("#feedback-form_03").serialize(),
    success: function(response) {
    if (response !== "false") {
    console.log(response);
    console.log('<div id="message_03">Ваше сообщение успешно отправлено. Спасибо!<br />Мы свяжемся с вами ближайшее время!</div>');
    }
    }
    }
    })
    Anonymous

Your Answer
To place the code, please use CodePen or similar tool. Thanks you!