Home > Blockchain >  isset($_GET(var)) is true even if var is null or undefined
isset($_GET(var)) is true even if var is null or undefined

Time:10-21

I am sending multiple unset variables to am isset() statement, but the result is true.

So I have a form and I am sending the data via a listener and meanwhile I am printing the variables to the web console

  formQuote.addEventListener('submit', (event) => {
    event.preventDefault();
    fromDate = formQuote.elements["fromDate"].value;
    toDate = formQuote.elements["toDate"].value;
    apptmnt = $('input[name="apptmnt"]:checked').val();
            
    console.log(fromDate);
    console.log(toDate);
    console.log(apptmnt);

    $('#priceDisplay').load('forms/quote.php?fromDate='   fromDate   '&toDate='   toDate   '&apptmnt='   apptmnt);
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

As I haven't entered anything, the console shows 2 empty lines (fromDate, toDate) and undefined, upon clicking submit. The data is catched by a php file

if (isset($_GET['fromDate'], $_GET['toDate'], $_GET['apptmnt'])) {
    echo 'true';
}
else {
    echo 'false'; 
}

In return I see true!?

My understanding based on the documentation is that isset returns true only if all arguments are true, so isset($a, $b) is the same as isset($a)*isset($b)

Any ideas what ca cause this?

CodePudding user response:

A sample code show difference between isset and empty

<?php
$a = array('blank'=>'');
var_dump(isset($a['blank']));  // bool(true)
var_dump(!empty($a['blank'])); // bool(false)

CodePudding user response:

You are sending those parameters in the query string even if they are null or undefined. You should only send parameters when they are not empty. isset checks if variable is present and not null but in this case as you are sending it thorough JS even if they are null in JS it might sent them as empty string which is not null, so that's why isset returns true. So you should check it before calling your page.

if (fromDate && toDate && apptmnt) {
    $('#priceDisplay').load('forms/quote.php?fromDate='   fromDate   '&toDate='   toDate   '&apptmnt='   apptmnt);
} else {
    $('#priceDisplay').load('forms/quote.php');
}
  • Related