[jQuery] Re: How to handle responses from PHP with $.ajax()

2009-01-22 Thread Agile Consulting

Its a nice group


[jQuery] Re: How to handle responses from PHP with $.ajax()

2009-01-19 Thread Agile Consulting

What is difference between Oracle and SQL Server


[jQuery] Re: How to handle responses from PHP with $.ajax()

2009-01-04 Thread rob303

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  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  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  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  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
> > > > > --
> > > > > 
> > > > > require_once("../inc/site_config.php");
>
> > > > > require_once(SITE . "/includes.php");
> > > > > ?>
> > > > > 
> > > > > 
> > > > > 
> > > > > 
> > > > > $(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;
> > > > > });
> > > > > });
> > > > > 
> > > > > 
> > > > > 
> > > > > 
> > > > > Email:
> > > > >  > > > > type="text" />
> > > > > 
> > > > > 
> > > > > 
> > > > > Email sent.
> > > > > 
> > > > > err
> > > > > 
> > > > > 
>
> > > > > --
> > > > > 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  wrote:
>
> > > > > > You must 'echo' the JSON output. Also you indicate periods ending
> > > > > > code lines, inst

[jQuery] Re: How to handle responses from PHP with $.ajax()

2009-01-04 Thread rob303

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  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  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  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
> > > > --
> > > >  >
> > > > require_once("../inc/site_config.php");
> >
> > > > require_once(SITE . "/includes.php");
> > > > ?>
> > > > 
> > > > � 
> > > > � � � � 
> > > > � � 
> > > > � � � $(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;
> > > > � � � � });
> > > > � � � });
> > > > � � 
> > > > � 
> > > > � 
> > > > � � � 
> > > > � � � � Email:
> > > > � � � �  > > > type="text" />
> > > > � � � � � � 
> > > > � � � � � 
> > > > � � � 
> > > > � � � � Email sent.
> > > > � � � 
> > > > � � � err
> > > > � 
> > > > 
> >
> > > > --
> > > > 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  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  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

[jQuery] Re: How to handle responses from PHP with $.ajax()

2009-01-04 Thread rob303

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  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  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  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
> > > --
> > > 
> > > require_once("../inc/site_config.php");
>
> > > require_once(SITE . "/includes.php");
> > > ?>
> > > 
> > >   
> > >         
> > >     
> > >       $(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;
> > >         });
> > >       });
> > >     
> > >   
> > >   
> > >       
> > >         Email:
> > >          > > type="text" />
> > >             
> > >           
> > >       
> > >         Email sent.
> > >       
> > >       err
> > >   
> > > 
>
> > > --
> > > 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  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  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 a

[jQuery] Re: How to handle responses from PHP with $.ajax()

2009-01-04 Thread donb

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  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  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
> > --
> > 
> > require_once("../inc/site_config.php");
>
> > require_once(SITE . "/includes.php");
> > ?>
> > 
> >   
> >         
> >     
> >       $(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;
> >         });
> >       });
> >     
> >   
> >   
> >       
> >         Email:
> >          > type="text" />
> >             
> >           
> >       
> >         Email sent.
> >       
> >       err
> >   
> > 
>
> > --
> > 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  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  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  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  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 

[jQuery] Re: How to handle responses from PHP with $.ajax()

2009-01-04 Thread rob303

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  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
> --
> 
> require_once("../inc/site_config.php");
>
> require_once(SITE . "/includes.php");
> ?>
> 
>   
>         
>     
>       $(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;
>         });
>       });
>     
>   
>   
>       
>         Email:
>          type="text" />
>             
>           
>       
>         Email sent.
>       
>       err
>   
> 
>
> --
> 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  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  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  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  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
> > > > > --
> > > > > 
> > > > > require_once(

[jQuery] Re: How to handle responses from PHP with $.ajax()

2009-01-04 Thread rob303

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
--


  


  $(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;
});
  });

  
  
  
Email:


  
  
Email sent.
  
  err
  


--
PHP
--
 1);
} else {
  $json_data = array('result' => 0);
}

echo json_encode($json_data);

?>
$message = $_POST['email'];

if($message != '') {
  mail('r...@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  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  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  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  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
> > > > --
> > > > 
> > > > require_once("includes.php");
>
> > > > ?>
> > > > 
> > > >   
> > > >     
> > > >     
> > > >       $(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 to show 'div.error".
> > > >             }
> > > >           });
> > > >           return false;
> > > >         });
> > > >       });
> > > >     
> > > >   
> > > >   
> > 

[jQuery] Re: How to handle responses from PHP with $.ajax()

2009-01-04 Thread donb

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  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  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  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
> > > --
> > > 
> > > require_once("includes.php");
>
> > > ?>
> > > 
> > >   
> > >     
> > >     
> > >       $(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 to show 'div.error".
> > >             }
> > >           });
> > >           return false;
> > >         });
> > >       });
> > >     
> > >   
> > >   
> > >     
> > >       Email:
> > >        > > type="text" />
> > >       
> > >     
> > >     
> > >       Email sent.
> > >     
> > >     
> > >       
> > >     
> > >   
> > > 
> > > --
> > > PHP - ajax.php
> > > --
> > > 
> > > require_once("includes.php");
>
> > > $message = $_POST['email'];
>
> > > if($message != '') {
> > >   mail('some-em...@email.com', 'Ajax test', $message);
> > >   return 1;} else {
>
> > >   $userError = "Please enter your email address";
> > >   return 0;
>
> > > }
>
> > > ?>
>
> > > I've been searching the web for an answer but can't find one
> > > anywhere!  Any help would be greatly appreciated.


[jQuery] Re: How to handle responses from PHP with $.ajax()

2009-01-04 Thread rob303

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  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  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
> > --
> > 
> > require_once("includes.php");
>
> > ?>
> > 
> >   
> >     
> >     
> >       $(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 to show 'div.error".
> >             }
> >           });
> >           return false;
> >         });
> >       });
> >     
> >   
> >   
> >     
> >       Email:
> >        > type="text" />
> >       
> >     
> >     
> >       Email sent.
> >     
> >     
> >       
> >     
> >   
> > 
> > --
> > PHP - ajax.php
> > --
> > 
> > require_once("includes.php");
>
> > $message = $_POST['email'];
>
> > if($message != '') {
> >   mail('some-em...@email.com', 'Ajax test', $message);
> >   return 1;} else {
>
> >   $userError = "Please enter your email address";
> >   return 0;
>
> > }
>
> > ?>
>
> > I've been searching the web for an answer but can't find one
> > anywhere!  Any help would be greatly appreciated.


[jQuery] Re: How to handle responses from PHP with $.ajax()

2009-01-04 Thread donb

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  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
> --
> 
> require_once("includes.php");
>
> ?>
> 
>   
>     
>     
>       $(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 to show 'div.error".
>             }
>           });
>           return false;
>         });
>       });
>     
>   
>   
>     
>       Email:
>        type="text" />
>       
>     
>     
>       Email sent.
>     
>     
>       
>     
>   
> 
> --
> PHP - ajax.php
> --
> 
> require_once("includes.php");
>
> $message = $_POST['email'];
>
> if($message != '') {
>   mail('some-em...@email.com', 'Ajax test', $message);
>   return 1;} else {
>
>   $userError = "Please enter your email address";
>   return 0;
>
> }
>
> ?>
>
> I've been searching the web for an answer but can't find one
> anywhere!  Any help would be greatly appreciated.