That is an excellent idea; I agree completely. There's nothing inefficient
about it either.

It also leaves the door open to return additional data without breaking your
existing code.

{ "valid":true }

{ "valid":false }

Now if you want to add other properties to that data, you don't have to
change the code that does an "if( json.valid )" test.

-Mike

> From: donb
> 
> This is probably a good reason to make it a habit to return a 
> JSON object, so that the data is not simply a string of 
> characters with the associated ambiguities.  I realize this 
> leaves me open to counterarguments of 'it's not a efficient' 
> but really that's a minor worry (to me that is).
> 
> On Jan 2, 5:23 pm, "Michael Geary" <m...@mg.to> wrote:
> > The 'if' statement in JavaScript is reliable.
> >
> > If your "if( data == 'no' )" is taking the "else" path, the most 
> > likely reason is that the data variable is indeed not equal to "no".
> >
> > Even though the console.log showed a value of "no", are you 
> sure there 
> > isn't a newline at the end? What does 
> "console.log(data.length)" show?
> >
> > You can also use the Fiddler2 debugging proxy, or the Net tab in 
> > Firebug, to see the actual length of the body of the 
> response from the PHP script.
> >
> > My suspicion is that there's an extra newline at the end of 
> your PHP script.
> > Your PHP script actually looks like this, doesn't it?
> >
> > <?php
> > ...your code...
> > ?>
> >
> > Depending on the version of PHP, a single newline after the 
> ?> may not 
> > add a newline to the output, but if there are two newlines 
> after the 
> > ?> you'll definitely get an extra newline in the output - and it 
> > wouldn't necessarily be obvious from looking at the PHP 
> code in a text editor.
> >
> > Remember that anything before the opening <?php or after 
> the closing 
> > ?> is rendered directly as part of the HTML output.
> >
> > Because of this, best practice in a pure PHP script is to 
> *omit* the 
> > closing ?>. The language does not require it, and omitting 
> the closing 
> > ?> insures that you won't get any spurious newlines in your output.
> >
> > If this isn't it, post a link to a test page that 
> demonstrates the problem.
> >
> > -Mike
> >
> > > From: wattsup
> >
> > > I am having some problems with a jquery script that I am writing. 
> > > The problem is after I receive the data from the php file jquery 
> > > seems to skip the if statement.  I am logging to console 
> and it is 
> > > showing yes/ no depends on email address. I am enclosing 
> my scripts 
> > > in case someone can help me.
> >
> > > <script src="javascripts/jquery-1.2.6.js" type="text/javascript"
> > > charset="utf-8"></script>
> > > <script src="javascripts/jquery.validate.js" 
> type="text/javascript"
> > > charset="utf-8"></script>
> > > <script src="javascripts/jquery.corner.js" type="text/javascript"
> > > charset="utf-8"></script>
> > > <script src="javascripts/jquery.lightbox.packed.js"
> > > type="text/ javascript"></script>
> >
> > > $(document).ready(function(){
> > >    $("#email").blur(function(){
> > >            var post_string = $(this).val();
> >
> > >            $("#msgbox").removeClass().addClass('messagebox').text
> > > ('Checking...').fadeIn(1000);
> > >                    $.post("php/email.php",{ email_check:
> > > post_string} ,function(data){
> > >                            console.log(data); // show 
> data value in 
> > > console
> > >                             if(data=='no') {  // email 
> not avaiable
> > > <------------------------- Skipping this area
> >
> > > $("#msgbox").fadeTo(200,0.1,function(){
> >
> > > $(this).html('email address already exists').addClass
> > > ('messageboxerror').fadeTo(900,1)
> > >                                    });
> > >                            } else { // email avaiable
> > > <-------------------------- Always gos here no matter what
> >
> > > $("#msgbox").fadeTo(200,0.1,function(){
> >
> > > $(this).html('email available to register').addClass 
> > > ('messageboxok').fadeTo(900,1);
> > >                                    });
> > >                            } // end if
> > >                    }); // end post
> > >    }); // end email blur
> > > }); // end doc ready
> >
> > > PHP FILE
> >
> > > if(isset($_POST['email_check'])) {
> > >    $email = $_POST['email_check'];
> >
> > >    require_once('config.inc.php');
> > >    require(MYSQL);
> >
> > >    $result = @mysql_query("SELECT * FROM members WHERE 
> (user_email = 
> > > '$email')");
> > >    $row_num = mysql_num_rows($result);
> >
> > >    if( $row_num > 0){
> > >            // "unavailable to register";
> > >            $valid = "no";
> > >    }else{
> > >            // "available to register";
> > >            $valid = "yes";
> > >    };
> >
> > >    echo $valid;
> > >    mysql_close();
> > > }
> >
> > > I have been working on this all day and can not figure 
> out why the 
> > > data is skipping the if statement. if you have any ideals 
> please let 
> > > me know.
> 

Reply via email to