为什么 jQuery Form Plugin 混合使用 NULL 和 0
我正在使用 jQuery 表单插件 http://jquery.malsup.com/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;
});
这是我用于测试的表格。
<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>
这是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;
这是我在 jQuery 表单插件中发现的问题。
案例一>如果我提交表单时没有为#fname输入任何内容,则返回结果如下:
"is_fname_empty":true,"is_fname_isset":false
Case II>如果我在 #fname 中输入 0 来提交表单,则返回的结果如下:
"is_fname_empty":true,"is_fname_isset":false
正如您所看到的,我似乎无法区分用户输入的内容。 我真的需要知道用户是否没有输入任何内容或者用户输入了 0。
有人可以帮忙吗?
谢谢
// Update based on comments from dconde //
大家好,
我设置了一个工作脚本,以便我可以轻松解释我的问题。
<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
可以看到,如果只考虑empty、isset和dconde提供的方法,两次提交没有区别。然而,与我上次实验不同的是,字符串长度可以帮助我有所作为。我不知道为什么上次对我不起作用。
谢谢大家。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
在 PHP 中测试这种空值的最佳方法是通过字符串长度,即。
The best way to test for this kind of emptiness in PHP is via string length, ie.
PHP 文档 指出 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 hope I can help
Good Luck!
您会得到
isset($fname) == false
,因此$fname
未定义。这是因为您通过$_POST
访问 PHP 中的 POST 值。正确的代码是: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:这段代码示例对我来说看起来很可疑:
第四行 $fname 似乎未定义。也许应该是
This code sample looks fishy to me:
on the 4th line $fname seems to be undefined. Maybe it should be
所以,换句话说,您使用了错误的方法来检查 $_POST 是否为空!
你可以这样做,只是一个例子:
注意使用==很重要,因为“0”和0是== 0 BUT “0”不是=== 0
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:
NOTE the use of == is important cause "0" and 0 are == 0 BUT "0" is not === 0
在 PHP 中,您可以使用常用的符号来测试变量值,例如:
都相同,但是您可以使用“===”,它不仅测试变量的值,还测试变量的强制转换。
上面的代码将为 true,但下面的代码将为 false。
因此,要测试变量是否为空而不是简单地设置为 0,请使用代码
TRIM 是必需的,以防有人输入“”(双空格)。
In PHP, you can test for a variables value by using the usual signs, for example:
are all the same, however you can use '===' which not only tests the value of the variable, but the cast of it as well.
The above code will be true however the code below will be false.
Therefore to test if the variable is empty and not simply set to 0, then use the code
The TRIM is required incase someone enters " " (double spaces).
如果使用 IF 语句,请记住它计算表达式
IF(0) returns FALSE
IF(null) returns FALSE
if($_POST['fname']===0) - 检查 if var=0
if($_POST[ 'fname']=='') - 检查 var=empty
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