Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy jQuery Form Plugin mixes the NULL and 0
    primarykey
    data
    text
    <p>I am using jQuery Form Plugin <code>http://jquery.malsup.com/form/</code> to submit my form.</p> <p>Here is the code that is used to submit the form.</p> <pre><code>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; }); </code></pre> <p>This is the form I am using for testing.</p> <pre><code>&lt;form action="" method="post" style="margin:10px 0;" id="myform"&gt; &lt;div id="recaptcha_image"&gt;&lt;/div&gt; &lt;input type="text" name="recaptcha_response_field" id="recaptcha_response_field" size="30" /&gt; &lt;input type='file' name='filename' size='10' /&gt; First name: &lt;input type="text" id="fname" name="fname" /&gt; &lt;input type="submit" value="Submit" id="submit_button" /&gt; &lt;/form&gt; </code></pre> <p>Here is the code snippet in recaptcha.php</p> <pre><code>$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; </code></pre> <p>Here is the problem I found with jQuery Form Plugin.</p> <p>Case I> If I submit the form without entering anything for #fname, the returned result is as follows:</p> <pre><code>"is_fname_empty":true,"is_fname_isset":false </code></pre> <p>Case II> If I submit the form with entering 0 for #fname, the returned result is as follows:</p> <pre><code>"is_fname_empty":true,"is_fname_isset":false </code></pre> <p>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.</p> <p>Anyone can help?</p> <p>Thank you</p> <pre><code>// Update based on comments from dconde // </code></pre> <p>Hello all,</p> <p>I set up a working script so that I can explain my problem easily.</p> <pre><code>&lt;html&gt; // testAjaxForm.php &lt;head&gt; &lt;script type="text/javascript" src="js/jquery/jquery-1.4.2.js"&gt;&lt;/script&gt; &lt;script type="text/javascript" src="js/jquery/jquery.form.js"&gt;&lt;/script&gt; &lt;script type="text/javascript"&gt; $(document).ready(function() { $('#myForm').submit(function() { $(this).ajaxSubmit(); // called first return false; }); }); &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;form id="myForm" action="verify.php" method="post"&gt; Name: &lt;input type="text" name="fname" /&gt; &lt;input type="submit" value="Submit Comment" /&gt; &lt;/form&gt; &lt;/body&gt; &lt;/html&gt; &lt;?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']) &amp;&amp; $_POST['fname'] != 0 &amp;&amp; $_POST['fname'] != '0'; FB::log($is_fname_empty, '$is_fname_empty'); ?&gt; Here is the printed information from FirePHP. </code></pre> <p>1> I submit the form without entering any information.</p> <pre><code>$_POST: array('fname'=&gt;'') strlen(name): 0 empty(fname): TRUE isset(fname): TRUE $is_fname_empty: FALSE </code></pre> <p>2> I submit the form with 0.</p> <pre><code>$_POST: array('fname'=&gt;'0') strlen(name): 1 empty(fname): TRUE isset(fname): TRUE $is_fname_empty: FALSE </code></pre> <p>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.</p> <p>Thank you all.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload