0

Basically i want a form that retrieves the value 1 or 0 to see whether an email exists within the form, and i also want to retrieve the name of the 'username' column which is on the same row as the email.

Here is the javascript/php script ( i start here ) : http://pastebin.com/zB0Umyyb

<!DOCTYPE html>
<html>
<head>
  <link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/>
  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5/jquery.min.js"></script>
  <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>

  <script>
  $(document).ready(function() {
    $("#tabs").tabs();
  });
  </script>
<script type="text/javascript">


  $(document).ready(function() {


                //the min chars for username
                var min_chars = 3;

                //result texts
                var characters_error = 'Minimum amount of chars is 3';
                var checking_html = 'Checking...';

                //when button is clicked
                $('#check_email_availability').click(function(){
                        //run the character number check
                        if($('#email').val().length < min_chars){
                                //if it's bellow the minimum show characters_error text
                                $('#email_availability_result').html(characters_error);
                        $(checking_html).hide();
                        }else{                 
                                //else show the cheking_text and run the function to check
                                $('#email_availability_result').html(checking_html);
                                check_availability();
                                $(checking_html).hide();
                        }
                });


  });

//function to check username availability      
function check_availability(){

                //get the username
                var email = $('#email').val();
                //use ajax to run the check
                $.post("check_email.php", { email: email },
                        function(result){
                                //if the result is 1
                                if(result == 1){
                                        //show that the username is available
                                        $('#email_availability_result').html('<span class="is_available"><b>' +email + '</b> is Available</span>');
                                }else{
                                        //show that the username is NOT available
                                        $('#email_availability_result').html('<span class="is_not_available"><b>' +email + '</b> has already been refered</span>');
                                }
                });

}  
</script>
</head>
<body style="font-size:62.5%;">
<?
require ("config/config.php");
$result = mysql_query('select * from refer where email = "[email protected]" AND referred = "true"');
$row = mysql_fetch_array($result);
$usercheck = $row['username'];
?>
<div id="tabs">
    <ul>
        <li><a href="#fragment-1"><span>Refer</span></a></li>
        <li><a href="#fragment-2"><span>Referal History</span></a></li>
        <li><a href="#fragment-3"><span>Rewards</span></a></li>
    </ul>
    <div id="fragment-1">
<form>
  <p>Minecraft User:
  <input type='text' id='username'>
    </p>
  <p>Email Address:
  <input type='text' id='email'> <input type='button' id='check_email_availability' value='Check Availability'>
  </p>
</form>
  <script>
  $(document).ready(function() {
    $("button").button();
  });
  </script>
  <button> Refer! </button>
  </br>
  <div id='username_availability_result'></div>
  </br>
<div id='email_availability_result'></div>
  </div>
    <div id="fragment-2">
<table border="1">
<tr>
<th>User</th>
<th>Referal Request</th>
</tr>
<tr>
<td>MrMan121</td>
<td>Pending</td>
</tr>
<tr>
<td>olimoli123</td>
<td>Accepted</td>
</tr>
</table>
    </div>
    <div id="fragment-3">
Rewards include:
dadadad.
    </div>
</div>
</body>
</html>

And here is the php script it talks to: http://pastebin.com/tQppn84s

<?php
//connect to database
require ("config/config.php");

//get the username
$email = mysql_real_escape_string($_POST['email']);

//mysql query to select field username if it's equal to the username that we check '
$result = mysql_query('select email from refer where email = "'. $email .'" AND referred = "true"');
$row = mysql_fetch_array($result);
$usercheck = $row['username'];
//if number of rows fields is bigger them 0 that means it's NOT available '
if(mysql_num_rows($result)>0){
        //and we send 0 to the ajax request
        echo 0;
}else{
        //else if it's not bigger then 0, then it's available '
        //and we send 1 to the ajax request
        echo 1;
}

?>

and $usercheck should check for the username.

2
  • This is a too localized question as it will help noone esle Commented Mar 11, 2012 at 1:04
  • Olivier, to get upvotes in future make sure your posts are formulated as a question. That way people reading through can instantly find out which aspect you're having trouble with. Commented Mar 11, 2012 at 1:08

1 Answer 1

1

You just need to change the formatting of the response, and modify how it's handled on receipt. For instance, instead of just echo 0; you could do something like (I wasn't sure how you wanted username used, so that was just a guess)

echo '{"available":false, "username":"'.$usercheck.'"}';

then, in your HTML page, you would

$.post("check_email.php", { email: email },
function(result){
    response=jQuery.parseJSON(result);
    //if the result is 1
    if(response.available){
        //show that the username is available
        $('#email_availability_result').html('<span class="is_available"><b>' +email + '</b> is Available</span>');
    }else{
        //show that the username is NOT available
        $('#email_availability_result').html('<span class="is_not_available"><b>' +email + '</b> has already been referred by user '+response.username+'</span>');
});

and, for the IS available case, you'd

echo '{"available":true}';

Sign up to request clarification or add additional context in comments.

1 Comment

I see that you had a different comment earlier, do you have any questions on what is being done? In this updated version, your web services is returning JSON formatted data, and then the HTML page is parsing that into an object you pull the various parts from. You could also use XML in a similar fashion

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.