Using Javascript / Ajax to run a php script

dotKn0ck

I have a button at the bottom of my form, I am trying to disable it if the email that the user tries to use is already in use. The email the user supplies is $email

Here is my front-end code..

        <script type="text/JavaScript">
        var email = '<?php echo $email; ?>';//Get the value in the username textbox
        $.ajax({  //Make the Ajax Request
            type: "POST",  
            url: "ajax_check_email.php",  //file name
            data: "email="+ email,  //data
            success: function(server_response){  

           $("#availability_status").ajaxComplete(function(event, request){ 

            if(server_response == '0')//if ajax_check_username.php return value "0"
            { 
             alert("Server response recieved: 0");
            $("#availability_status").html('<img src="/img/icon-success.png" align="absmiddle" style="height: 15px; width: auto; margin-top: -2px;"> <font color="#c9dc54" style="padding-top: 5px;"> Available </font>  ');
            $("#btn-submit-2").html('<button type="submit"  class="btn btn-danger btn-lg btn-block"   id="b"  style="font-family: "klavikaRegular"; letter-spacing: 1px;" name="skip" value="0">CONTINUE</button>');
            //add this image to the span with id "availability_status"
            }  
            else if(server_response == '1')//if it returns "1"
            {  
             alert("Server response recieved: 1");
             $("#availability_status").html('<img src="/img/icon-error.png" align="absmiddle" style="height: 15px; width: auto; margin-top: -2px;"> <font color="#b11116" style="padding-top: 5px;">Not Available </font>');
             $("#btn-submit-2").html('<a class="btn btn-default btn-lg btn-block" style="font-family: "klavikaRegular"; letter-spacing: 1px;">CONTINUE</a>');
            }  

           });
           } 

          }); 

        }
        else
        {
        alert("failed query");
        $("#availability_status").html('<font color="#b11116">Username too short</font>');
        $("#btn-submit").html('<a class="btn btn-default btn-lg btn-block" style="font-family: "klavikaRegular"; letter-spacing: 1px;">USERNAME TOO SHORT</a>');
        //if in case the username is less than or equal 3 characters only 
        }

        </script>

Here is my php file source

    <?php

    include('database_connection.php');
    //Include The Database Connection File 
    if(isset($_POST['email']))//If a username has been submitted 
    {
    $email = mysql_real_escape_string($_POST['email']);//Some clean up :)

    $check_for_email = mysql_query("SELECT user_login FROM wp_users WHERE user_email='$email'");
    //Query to check if username is available or not 

    if(mysql_num_rows($check_for_email))
    {
    echo '1';//If there is a  record match in the Database - Not Available
    }
    else
    {
    echo '0';//No Record Found - Username is available 
    }

    }

    ?>

Is there an issue with my javascript syntax?

$email is hard coded as something I know should fail..

Rasclatt

Try doing the success writing in the php page:

Display Page

<div id="availability_status"></div>
<script type="text/JavaScript">
    var email = '<?php echo $email; ?>';//Get the value in the username textbox

    $(document).ready(function() {
            $.ajax({  //Make the Ajax Request
                    url: "ajax_check_email.php?email="+email,  //data
                    cache: false,
                    success: function(server_response){
                            $("#availability_status").html(server_response);
                        }
                });
        });
</script>

** ajax_check_email.php**

<?php
    include('database_connection.php');
    //Include The Database Connection File 
    if(isset($_GET['email'])) {
            if(filter_var($_GET['email'], FILTER_VALIDATE_EMAIL)) {
                    $email              =   mysql_real_escape_string($_POST['email']);//Some clean up :)
                    $check_for_email    =   mysql_query("SELECT user_login FROM wp_users WHERE user_email='$email'");
                    //Query to check if username is available or not 
                    if(mysql_num_rows($check_for_email)) { ?>
        <img src="/img/icon-error.png" align="absmiddle" style="height: 15px; width: auto; margin-top: -2px;">
        <font color="#b11116" style="padding-top: 5px;">Not Available </font>
                <?php   }
                    else { ?>

        <img src="/img/icon-success.png" align="absmiddle" style="height: 15px; width: auto; margin-top: -2px;"> <font color="#c9dc54" style="padding-top: 5px;"> Available </font>
        <button type="submit"  class="btn btn-danger btn-lg btn-block"   id="b"  style="font-family: "klavikaRegular"; letter-spacing: 1px;" name="skip" value="0">CONTINUE</button>
                <?php   }
                }
            else { ?>
            <h2>Invalid Email Address.</h2>
            <?php }
        } ?>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Using Javascript to run a php script to check username

From Dev

Using Ajax to run a php script every X seconds

From Dev

Run php script with AJAX and POST

From Dev

Using AJAX to execute a PHP script through a JavaScript function

From Dev

Using any Jquery AJAX requests to load PHP script cause script to be returned and not run by server

From Dev

Calling a PHP script using ajax

From Dev

Calling a PHP script using ajax

From Dev

AJAX (jquery) to run PHP script not working

From Dev

AJAX (jquery) to run PHP script not working

From Dev

Run Javascript script from ajax response

From Dev

Ajax - Issue - Sending JavaScript Var to PHP script

From Dev

Unable to run php script using cron

From Dev

Run a php script on localhost using cron job

From Dev

Simple call to php script using AJAX not working

From Dev

Pass an array to a php script using AJAX

From Dev

Sending Information to a PHP Script using Ajax

From Dev

Calling a php script using ajax not working

From Dev

Simple call to php script using AJAX not working

From Dev

Sending FileList using Ajax to PHP script

From Dev

Using Ajax and JQuery to call PHP Script

From Dev

call Perl script from javascript using AJAX

From Dev

PHP script not able to read ajax post value in order to run the query

From Dev

Run AJAX script

From Dev

Using $.post, $.ajax or AJAX properly in CodeIgniter to call a PHP script

From Dev

PUSH using PHP, Ajax and Javascript/jQuery?

From Dev

Unable to pass javascript array to php using ajax

From Dev

Call PHP function using javascript ajax

From Dev

JavaScript Variable to PHP using Ajax Not working

From Dev

Using post method with php,ajax and javascript

Related Related

HotTag

Archive