Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-25-2013, 06:04 AM   PM User | #1
gkjr1
New Coder

 
Join Date: Jan 2013
Location: Texas, USA
Posts: 35
Thanks: 5
Thanked 0 Times in 0 Posts
gkjr1 is an unknown quantity at this point
Error object Object on return from post

I'm trying to post a field value to php, check in my db to see if it exists, then return and handle on the form according with a pop up saying what to do. I do also have it backwards, as the registration is invite only so you'll notice in the php that you do in fact have to have your email there.

It returns "empty" and handles it exactly fine when there is nothing in the field. If I put anything at all in the input though it gives the error and I have no idea why...

java part, and like it said, it works when its 'empty'
PHP Code:
$('form.register_form input[name=email]').bind("oninput", function ()
            {
                var 
messageBox = $('form.register_form span#msgbox2');
                var 
input = $(this);
                
                
messageBox.removeClass().addClass('messagebox').text('Validating....').fadeIn(1000);

                var 
getData = $.post('process_availability.php', { email:input.val() });
                
getData.done(function(data) {
                    
alert(data);
                    
//if correct login detail
                    
if (data == 'empty')
                    {
                        
// blank input
                        
messageBox.fadeTo(200,0.1,function() { 
                            
//add message and change the class of the box and start fading
                            
messageBox.text('Please put in a valid email!').addClass('messageboxerror').fadeTo(900,1);
                        });
                        
alert('Please put in a valid email!');
                    }
                    if (
data == 'yes')
                    {
                        
// email is available
                        
messageBox.fadeTo(200,0.1,function() { 
                            
//add message and change the class of the box and start fading
                            
messageBox.text('This email is available to register!').addClass('messageboxok').fadeTo(900,1);
                        });
                        
alert('This email is available to register!');
                    }
                    if (
data == 'no')
                    {
                        
// email is not in database
                        
messageBox.fadeTo(200,0.1,function() { 
                            
//add message and change the class of the box and start fading
                            
messageBox.text('This email could not be found in our database!').addClass('messageboxerror').fadeTo(900,1);
                        });
                        
alert('This email could not be found in our database!');
                    }
                    if (
data == 'already')
                    {
                        
// email has already been registered
                        
messageBox.fadeTo(200,0.1,function() { 
                            
//add message and change the class of the box and start fading
                            
messageBox.text('This email is already registered!').addClass('messageboxerror').fadeTo(900,1);
                        });
                        
alert('This email is already registered!');
                    }
                });
                
//.fail(function(jqXHR, textStatus, errorThrown) { alert(textStatus+ ': ' +errorThrown); }).always(function() { alert("finished"); });
            
}); 
the php to check
PHP Code:
<?php
//check email availability
if ($_POST['email'] != null) {
    
//require necessary files
    
require('config/dbconfig.php');
    include(
'classes/class.mysqli.php');
    
    
//change post to variable
    
$email $_POST['email'];
    
    
//set up bind for query
    
$bind = array(
        
":email_bind" => "$email"
    
);
    
    
//query email to see if it is in database
    
$results $db->select('users','email=:email_bind',$bind,'*','','');
    
    
//use boolean to check results
    
$trueFalse = (count($results) > 0) ? 1;
    if (
$trueFalse == 0)
    {
        foreach(
$results as $row)
        {
            if (
$row['regcode'] == null)
            {
                echo 
'already';
            }else
            {
                echo 
'yes';
            }
            exit;
        }
    } else {
        
//email is blank
        
echo 'no';
        exit;
    }
}else
{
    echo 
'empty';
}
?>
gkjr1 is offline   Reply With Quote
Old 01-25-2013, 06:47 AM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,579
Thanks: 62
Thanked 4,064 Times in 4,033 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
May I suggest you double check the PHP code, first?

Easy to do.

Temporarily change the two uses of $_POST there to $_GET.

Then just hit that page from the browser using a URL such as
Code:
   process_availability.php?email=abc@xyz.com
That is, passing in both email addresses in the db table and not, already registered and not. Do you get the results you expect?

If not, fix the PHP code.
If so, then indeed the but is in the JS code.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Old 01-25-2013, 03:08 PM   PM User | #3
gkjr1
New Coder

 
Join Date: Jan 2013
Location: Texas, USA
Posts: 35
Thanks: 5
Thanked 0 Times in 0 Posts
gkjr1 is an unknown quantity at this point
No I get a HTTP 500 error. Cannot display the webpage.

That's my db right? Or something else? If i take the ?email=abc@xyz.com part out it does load fine, and says 'empty'.

I'm using a PDO wrapper to execute the sql queries. The config for the db looks fine too, and this wrapper worked with other things in the past.

Found it. Might be important to make sure you make a connection first. xD Sorry guys.

Last edited by gkjr1; 01-25-2013 at 03:21 PM..
gkjr1 is offline   Reply With Quote
Old 01-25-2013, 08:13 PM   PM User | #4
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,579
Thanks: 62
Thanked 4,064 Times in 4,033 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Quote:
Originally Posted by gkjr1 View Post
Found it. Might be important to make sure you make a connection first.
LOL! Shame on you. I have certainly never made a silly mistake like that!

Never today, that is. But, then, I haven't started coding yet today.

Give me time, I'll do it at least once or twice today.

*********

Seriously, if nothing else you now know how to debug the server side of AJAX problems. I use the change-POST-to-GET trick all the time. It has saved me hours of headaches.

Best of luck!
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:52 AM.


Advertisement
Log in to turn off these ads.