dave
dave

Reputation: 239

Can't send textfield value to PHP file with jQuery

I would like to send this textfield value to a php file using jQuery.

HTML code:

<div>
    <label for="email">Email: </label>
    <input type="text" id="email" name="email" />
    <span id="msgbox" class="msgbox"></span>
</div>

jQuery code:

$(document).ready(function()
{
    $("#email").blur(function()
    {
        $("#msgbox").removeClass().addClass('messagebox').text('Checking...').fadeIn("slow");

        $.post("availability.php", { email: $(this).val() }, function(data)
        {
            if(data == 'yes')
            {
                $("#msgbox").fadeTo(200, 0.1, function()
                { 
                    $(this).html('Email Already exists').addClass('messageboxerror').fadeTo(900,1);
                });  
            }
            else
            {
                $("#msgbox").fadeTo(200, 0.1, function()
                { 
                    $(this).html('Email available to register').addClass('messageboxok').fadeTo(900,1); 
                });
            }
        });
    });
});

PHP code:

include_once $_SERVER['DOCUMENT_ROOT'] . '/braddclient/includes/magicquotes.inc.php';
include $_SERVER['DOCUMENT_ROOT'] . '/braddclient/includes/db.inc.php';

$email = mysqli_real_escape_string($link, $_POST['email']);

$sql = "SELECT * FROM bradduser WHERE email='$email'";
$result = mysqli_query($link, $sql);

if(!$result)
{
    $error = 'Error fetching email from bradduser.';
    include 'error.html.php';
    exit();
}

if(mysqli_num_rows($result) > 0)
{ 
    echo ‘yes’; //email already exist
} 
else 
{
    echo ‘no’;
}

The problem with the codes is that that doesn't seem to be a communication between jquery and php. The data refused to be sent to php. Please help to see what's wrong with the codes.

Upvotes: 0

Views: 276

Answers (2)

Mark Bell
Mark Bell

Reputation: 29775

At first glance it looks correct, so a couple of things to look at:

  • Where is "availability.php"? Is it in the same folder as the page that the jQuery script is run on? If not you will need to alter the path e.g "/availability.php" if it's in the site root or "/folder/availability.php" if it's in a subfolder.
  • If you haven't already, get Firefox and Firebug; Firebug will show you any AJAX requests being made from the page, as well as what the server is returning—VERY useful.

Upvotes: 1

Mihai Iorga
Mihai Iorga

Reputation: 39724

echo ‘yes’; is not a valid sintax

try:

echo 'yes'; with single quote

your jQuery scripting is ok but I think you have problems with availability.php

  • check if it is in same folder, if not, put a relative path to it
  • check your database connection
  • check if you have ' instead of
  • try to access the availability.php with the arguments, and to test use $_REQUEST['email'] then switch back. (eg: http://www.example.org/[email protected])

Upvotes: 2

Related Questions