I found the problem.  I was missing the dataType option:

$(document).ready(function(){
        $("form#submit").submit(function() {
          var email = $('#email').attr('value');
          $.ajax({
            type: "POST",
            url: "ajax.php",
            data: "email="+ email,
            dataType: "json",
            success: function(res){
              if(res.result == 1) {
                $('form#submit').hide();
                $('div.error').hide();
                $('div.success').fadeIn('medium');
              } else {
                $('div.error').show();
              }
            }
          });
          return false;
        });
      });

It works just fine now.

Cheers,

Rob.

On Jan 4, 10:12 pm, rob303 <r...@cube33.com> wrote:
> After a little more twiddling ... This works:
>
> $(document).ready(function(){
>         $("form#submit").submit(function() {
>           var email = $('#email').attr('value');
>           $.ajax({
>             type: "POST",
>             url: "ajax.php",
>             data: "email="+ email,
>             success: function(res){
>               if(res == "{\"result\":1}") {
>                 $('form#submit').hide();
>                 $('div.error').hide();
>                 $('div.success').fadeIn('medium');
>               } else {
>                 $('div.error').show();
>               }
>             }
>           });
>           return false;
>         });
>       });
>
> The json object contains what I need but I can only test it as if it
> were a string.  res.result is undefined. Why?
>
> Thanks in advance for any help!
>
> Rob.
>
> rob303 wrote:
> > Hi again,
>
> > I tried that but got the same results.
>
> > if(res.result == '1') {
> >   $('form#submit').hide();
> >   $('div.success').fadeIn('medium');
> > } else {
> >   $('div.error').show();
> > }
>
> > if(res.result == 1) {
> >   $('form#submit').hide();
> >   $('div.success').fadeIn('medium');
> > } else {
> >   $('div.error').show();
> > }
>
> > res.result is always false even though firebug says the response is
> > {"result":1}.
>
> > Rob.
>
> > On Jan 4, 7:01 pm, donb <falconwatc...@comcast.net> wrote:
> > > On another thread, someone pointed out to me that json_encode was
> > > supposed to 'know' if the data values were numeric or string and only
> > > quote the latter. That was not my experience, and perhaps not yours,
> > > either. Try '1' as the value you compare to instead of 1.
>
> > > On Jan 4, 1:05 pm, rob303 <r...@cube33.com> wrote:
>
> > > > Interestingly, in the Firebug console I'm seeing the correct
> > > > responses:
>
> > > > {"result":1}
>
> > > > or
>
> > > > {"result":0}
>
> > > > How can I access these inside my $.ajax() call?
>
> > > > Many thanks again for all the help!
>
> > > > Rob.
>
> > > > On Jan 4, 5:57 pm, rob303 <r...@cube33.com> wrote:
>
> > > > > Yes, the missing semicolons are an error in the post. I tried to echo
> > > > > the json_encode() call but I still can't get it to work. If I leave
> > > > > the email input blank no email is sent. If I enter a string the email
> > > > > is sent. The 'div.error' is always displayed regardless what's posted.
>
> > > > > Here's my full code again:
>
> > > > > ------------------
> > > > > HTML / Ajax
> > > > > ------------------
> > > > > <?php
>
> > > > > require_once("../inc/site_config.php");
>
> > > > > require_once(SITE . "/includes.php");
> > > > > ?>
> > > > > <html>
> > > > > <head>
> > > > > <script src="js/jquery.js" type="text/javascript"></script>
> > > > > <script type="text/javascript">
> > > > > $(document).ready(function(){
> > > > > $("form#submit").submit(function() {
> > > > > var email = $('#email').attr('value');
> > > > > $.ajax({
> > > > > type: "POST",
> > > > > url: "ajax.php?v=1",
> > > > > data: "email="+ email,
> > > > > success: function(res){
> > > > > if(res.result == 1) {
> > > > > $('form#submit').hide();
> > > > > $('div.success').fadeIn('medium');
> > > > > } else {
> > > > > $('div.error').show();
> > > > > }
> > > > > }
> > > > > });
> > > > > return false;
> > > > > });
> > > > > });
> > > > > </script>
> > > > > </head>
> > > > > <body>
> > > > > <form id="submit" method="post">
> > > > > Email:
> > > > > <input id="email" class="text" name="email" size="20"
> > > > > type="text" />
> > > > > <input type="submit" value="send mail" />
> > > > > </form>
> > > > > <div class="success" style="display:none;">
> > > > > Email sent.
> > > > > </div>
> > > > > <div class="error" style="display:none;">err</div>
> > > > > </body>
> > > > > </html>
>
> > > > > --------------
> > > > > PHP
> > > > > --------------
> > > > > <?php
>
> > > > > require_once("includes.php");
>
> > > > > $message = $_POST['email'];
>
> > > > > if($message != '') {
> > > > > mail('some-em...@email.com', 'Ajax test', $message);
> > > > > $json_data = array('result' => 1);} else {
>
> > > > > $json_data = array('result' => 0);
>
> > > > > }
>
> > > > > echo json_encode($json_data);
>
> > > > > ?>
> > > > > $message = $_POST['email'];
>
> > > > > if($message != '') {
> > > > > mail('....@cube33.com', 'Ajax test', $message);
> > > > > $json_data = array('result' => 1);} else {
>
> > > > > $json_data = array('result' => 0);
>
> > > > > }
>
> > > > > echo json_encode($json_data);
>
> > > > > ?>
> > > > > --------------
> > > > > On Jan 4, 5:44 pm, donb <falconwatc...@comcast.net> wrote:
>
> > > > > > You must 'echo' the JSON output. Also you indicate periods ending
> > > > > > code lines, instead of semicolons but perhaps that's just an error 
> > > > > > in
> > > > > > your post, not the actual code?
>
> > > > > > On Jan 4, 10:38 am, rob303 <r...@cube33.com> wrote:
>
> > > > > > > Hi,
>
> > > > > > > Many thanks for the help. I had a go at implementing what you
> > > > > > > suggested but I'm clearly still missing something.
>
> > > > > > > Here is my $.ajax() call:
> > > > > > > ------------------
> > > > > > > $(document).ready(function(){
> > > > > > > $("form#submit").submit(function() {
> > > > > > > var email = $('#email').attr('value');
> > > > > > > $.ajax({
> > > > > > > type: "POST",
> > > > > > > url: "ajax.php",
> > > > > > > data: "email="+ email,
> > > > > > > success: function(del){
> > > > > > > if(del.result == 1) {
> > > > > > > $('form#submit').hide();
> > > > > > > $('div.success').fadeIn('medium');
> > > > > > > } else {
> > > > > > > $('div.error').show();
> > > > > > > }
> > > > > > > }
> > > > > > > });
> > > > > > > return false;
> > > > > > > });
> > > > > > > });
> > > > > > > ------------------
>
> > > > > > > And here is my json array in PHP:
> > > > > > > ------------------
> > > > > > > $message = $_POST['email'];
>
> > > > > > > if($message != '') {
> > > > > > > mail('some-em...@email.com', 'Ajax test', $message);
> > > > > > > $json_data = array('result' => 1).} else {
>
> > > > > > > $json_data = array('result' => 0, 'error' => 'This is an error').
>
> > > > > > > }
>
> > > > > > > json_encode($json_data);
> > > > > > > ------------------
>
> > > > > > > I'm obviously not accessing del.result correctly because $
> > > > > > > ('div.error').show(); is always executed regardless of the value 
> > > > > > > of
> > > > > > > result. Where am I going wrong?
>
> > > > > > > Thanks in advance,
>
> > > > > > > Rob.
>
> > > > > > > On Jan 4, 2:41 pm, donb <falconwatc...@comcast.net> wrote:
>
> > > > > > > > Return a JSON object.
>
> > > > > > > > Construct a PHP array such as $json_data = array('result' => 0,
> > > > > > > > 'error' => 'This is an error'). End your PHP script with 
> > > > > > > > json_encode
> > > > > > > > ($json_data). Then you can reference del.result and del.error 
> > > > > > > > (I'm
> > > > > > > > referring to your definition of the' success' function from your
> > > > > > > > sample code)
>
> > > > > > > > On Jan 4, 7:16 am, rob303 <r...@cube33.com> wrote:
>
> > > > > > > > > Hi,
>
> > > > > > > > > I'm new to ajax and jquery but I'm not new to PHP. The 
> > > > > > > > > following
> > > > > > > > > example seems to work okay to a point but I can't figure out 
> > > > > > > > > how to
> > > > > > > > > handle data validation errors generated in my PHP.
>
> > > > > > > > > In my example I want to post some data to a script called 
> > > > > > > > > ajax.php.
> > > > > > > > > That script will check the data for validity and then return 
> > > > > > > > > true or
> > > > > > > > > false depending on the outcome of the checks. It will also 
> > > > > > > > > set an
> > > > > > > > > appropriate error message.
>
> > > > > > > > > How can I handle the returned data and display an error 
> > > > > > > > > message if
> > > > > > > > > needed in ajax?
>
> > > > > > > > > ----------------------
> > > > > > > > > HTML / Ajax - test.php
> > > > > > > > > ----------------------
> > > > > > > > > <?php
>
> > > > > > > > > require_once("includes.php");
>
> > > > > > > > > ?>
> > > > > > > > > <html>
> > > > > > > > > <head>
> > > > > > > > > <script src="js/jquery.js" type="text/javascript"></script>
> > > > > > > > > <script type="text/javascript">
> > > > > > > > > $(document).ready(function(){
> > > > > > > > > $("form#submit").submit(function() {
> > > > > > > > > var email = $('#email').attr('value');
> > > > > > > > > $.ajax({
> > > > > > > > > type: "POST",
> > > > > > > > > url: "ajax.php",
> > > > > > > > > data: "email="+ email,
> > > > > > > > > success: function(del){
> > > > > > > > > $('form#submit').hide();
> > > > > > > > > $('div.success').fadeIn('medium');
> > > > > > > > > // what if the data failed validation in PHP?
> > > > > > > > > // we need
>
> ...
>
> read more »

Reply via email to