Why jQuery Form Plugin mixes NULL and 0

I am using the jQuery Form Plugin plugin http://jquery.malsup.com/form/to submit my form.

Here is the code that is used to submit the form.

var options = { 
    beforeSubmit:  showRequest,  // pre-submit callback 
    success:       showResponse,  // post-submit callback 
    url:           'recaptcha.php',  // override for form 'action' attribute 
    type:          'POST'
}; 
$('#myform').submit(function() { 
    $(this).ajaxSubmit(options); 
    return false; 
});

This is the form I use for testing.

<form action="" method="post" style="margin:10px 0;" id="myform">
<div id="recaptcha_image"></div>
<input type="text" name="recaptcha_response_field" id="recaptcha_response_field" size="30"  />
<input type='file' name='filename' size='10' />
First name: <input type="text" id="fname" name="fname" />
<input type="submit" value="Submit" id="submit_button" /> 
</form>

Here is the code snippet in recaptcha.php

$results['success'] = true;
$results['msg'] = 'Security code is valid!';
$results['is_fname_empty'] = empty($fname);
$results['is_fname_isset'] = isset($fname);
echo json_encode( $results );
return; 

Here is the problem I found using jQuery Form Plugin.

Case I> If I submit a form without entering anything for #fname, the returned result will be as follows:

"is_fname_empty":true,"is_fname_isset":false

Case II> If I submit a form with an input of 0 for #fname, the returned result will be as follows:

"is_fname_empty":true,"is_fname_isset":false

As you can see, it seems that I cannot distinguish what the user enters. I really need to know if the user is typing something or the user is typing 0.

Anyone can help?

thank

// Update based on comments from dconde //

Hello everybody,

I installed a working script so that I can easily explain my problem.

<html> // testAjaxForm.php
<head> 
    <script type="text/javascript" src="js/jquery/jquery-1.4.2.js"></script> 
    <script type="text/javascript" src="js/jquery/jquery.form.js"></script> 
    <script type="text/javascript"> 
        $(document).ready(function() { 
            $('#myForm').submit(function() { 
                $(this).ajaxSubmit(); // called first
                return false; 
            });        
        }); 
    </script> 
</head> 

<body>
  <form id="myForm" action="verify.php" method="post"> 
      Name: <input type="text" name="fname" /> 
      <input type="submit" value="Submit Comment" /> 
  </form>
</body>
</html>

<?php // verify.php
require_once('./FirePHPCore/FirePHP.class.php');
require_once('./FirePHPCore/fb.php');
FB::setEnabled(true);
ob_start(); // avoid 'headers already sent error'

FB::log($_POST,      '$_POST');
FB::log(strlen($_POST['fname']), 'strlen(name)');
FB::log(empty($_POST['fname']), 'empty(fname)');
FB::log(isset($_POST['fname']), 'isset(fname)');
$is_fname_empty = empty($_POST['fname']) && $_POST['fname'] != 0 && $_POST['fname'] != '0';
FB::log($is_fname_empty, '$is_fname_empty');

?>

Here is the printed information from FirePHP.

1 > - .

$_POST: array('fname'=>'')
strlen(name): 0
empty(fname): TRUE
isset(fname): TRUE
$is_fname_empty: FALSE

2 > 0.

$_POST: array('fname'=>'0')
strlen(name): 1
empty(fname): TRUE
isset(fname): TRUE
$is_fname_empty: FALSE

, , , isset , dconde. , , , . , .

.

+3
7

PHP - , ..

isset($_POST['fname']) && (strlen($_POST['fname']) !== 0)
+3

PHP , 0 , , , -

//I believe these are the most important values to cover
$results['is_fname_empty'] = empty($fname) && $fname != 0 && $fname != '0';

, !

+3

isset($fname) == false, $fname . , POST- PHP $_POST. :

$results['is_fname_empty'] = empty($_POST['fname']);
$results['is_fname_isset'] = isset($_POST['fname']);
+2

:

FB::log(strlen($_POST['fname']), 'strlen(name)');
FB::log(empty($_POST['fname']), 'empty(fname)');
FB::log(isset($_POST['fname']), 'isset(fname)');
$is_fname_empty = empty($fname) && $fname != 0 && $fname != '0'; // from dconde
FB::log($is_fname_empty, '$is_fname_empty');

4- $fname undefined. ,

$is_fname_empty = empty($_POST['fname']) && $_POST['fname'] != 0 && $_POST['fname'] != '0';
+1
strlen(fname)
  • strlen 0, .
empty(fname)
  • FALSE, var . :

  • " ( )

  • 0 (0 )
  • " 0" (0 )
  • NULL
  • FALSE
  • array() ( )
  • var $var; ( , ) )
isset(fname)
  • TRUE, var , NULL, FALSE.

, , $_ POST!

, :

$empty = false;
foreach ( $_POST as $key => $val ) {
if ( trim($val) == '' || $val == 0 ) {
$empty = true;
} else {
$post[$key] .= $val;
}
}

if ( false === $empty ) {
print_r($post);
}

== "0" 0: == 0 "0" === 0

+1

PHP , , :

$a = 0;
($a == "0") || ($a == 0) || (empty($a))

, '===', , .

$a = 0;
($a === 0)

, .

$a = 0;
($a === "0")

, , , 0,

$fname = trim($fname);
$results['is_fname_empty'] = ($fname !== 0) && (empty($fname)) ? TRUE : FALSE;

TRIM , - "" ( ).

0

IF, ,

IF (0) FALSE

IF (null) FALSE

if ($ _ POST ['fname'] === 0) - , var = 0

if ($ _ POST ['fname'] == '') - , var = empty

0

Source: https://habr.com/ru/post/1762393/


All Articles