I am using jQuery Form 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's 'action' attribute
type: 'POST'
};
$('#myform').submit(function() {
$(this).ajaxSubmit(options);
return false;
});
This is the form I am using 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 with jQuery Form Plugin.
Case I> If I submit the form without entering anything for #fname, the returned result is as follows:
"is_fname_empty":true,"is_fname_isset":false
Case II> If I submit the form with entering 0 for #fname, the returned result is as follows:
"is_fname_empty":true,"is_fname_isset":false
As you can see, it seems that there is no way that I can differentiate what the user enters. I really need to know whether the user DOES NOT enter anything or the user enters 0.
Anyone can help?
Thank you
// Update based on comments from dconde //
Hello all,
I set up a working script so that I can explain my problem easily.
<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> I submit the form without entering any information.
$_POST: array('fname'=>'')
strlen(name): 0
empty(fname): TRUE
isset(fname): TRUE
$is_fname_empty: FALSE
2> I submit the form with 0.
$_POST: array('fname'=>'0')
strlen(name): 1
empty(fname): TRUE
isset(fname): TRUE
$is_fname_empty: FALSE
As you can see, there is NO difference between two submission if we only consider the empty, isset, and the method provided by dconde. However, on thing that is different from my last experiments is that the string length can help me make difference. I don't know why it doesn't work for me last time.
Thank you all.
The best way to test for this kind of emptiness in PHP is via string length, ie.
isset($_POST['fname']) && (strlen($_POST['fname']) !== 0)
The PHP documentation states that 0 is considered empty,so you may want to patch all the values that this function considers empty, something like:
//I believe these are the most important values to cover
$results['is_fname_empty'] = empty($fname) && $fname != 0 && $fname != '0';
I hope I can help Good Luck!
You get isset($fname) == false
, so $fname
is not defined. That's because you access POST values in PHP via $_POST
. Correct code would be:
$results['is_fname_empty'] = empty($_POST['fname']);
$results['is_fname_isset'] = isset($_POST['fname']);
This code sample looks fishy to me:
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');
on the 4th line $fname seems to be undefined. Maybe it should be
$is_fname_empty = empty($_POST['fname']) && $_POST['fname'] != 0 && $_POST['fname'] != '0';
strlen(fname)
- strlen get length of the string on success, and 0 if the string is empty.
empty(fname)
Returns FALSE if var has a non-empty and non-zero value. The following things are considered to be empty:
"" (an empty string)
- 0 (0 as an integer)
- "0" (0 as a string)
- NULL
- FALSE
- array() (an empty array)
- var $var; (a variable declared, but without a value in a class)without a value in a class)
isset(fname)
- Returns TRUE if var exists and has value other than NULL , FALSE otherwise.
So, in other word you are using the wrong way for check if a $_POST is empty!!
you can do like this, just an example:
$empty = false;
foreach ( $_POST as $key => $val ) {
if ( trim($val) == '' || $val == 0 ) {
$empty = true;
} else {
$post[$key] .= $val;
}
}
if ( false === $empty ) {
print_r($post);
}
NOTE the use of == is important cause "0" and 0 are == 0 BUT "0" is not === 0
In PHP, you can test for a variables value by using the usual signs, for example:
$a = 0;
($a == "0") || ($a == 0) || (empty($a))
are all the same, however you can use '===' which not only tests the value of the variable, but the cast of it as well.
$a = 0;
($a === 0)
The above code will be true however the code below will be false.
$a = 0;
($a === "0")
Therefore to test if the variable is empty and not simply set to 0, then use the code
$fname = trim($fname);
$results['is_fname_empty'] = ($fname !== 0) && (empty($fname)) ? TRUE : FALSE;
The TRIM is required incase someone enters " " (double spaces).
if you use a IF statement, remember that it evaluates the expression
IF(0) returns FALSE
IF(null) returns FALSE
if($_POST['fname']===0) - check if var=0
if($_POST['fname']=='') - check if var=empty
精彩评论