数据库中的异步请求[关闭]

There is a code:

<form name="step1" action="step2.php" method="POST">
Email: <input type="text" name="email" class="input-medium">
<button class="btn btn-small btn-warning" type="submit">Check</button> 
</form>

And that: step2.php

<?php
$email = $_POST['email'];
$result = mysql_query("SELECT email,discount FROM buyers WHERE email='$email'");
if (mysql_num_rows($result) <= 0)
{
echo "Are you a new client is't it? <br>";
echo "Your discount is 0%<br>";
}
else{
echo "<br/>Nice to see you again! <br/>";
$getSalary = mysql_fetch_array($result); 
echo "You discount is already: ";
echo $getSalary[discount];
echo " %";
}
?>

So, is it possible to get request in database without submit the form and redirect to new page (step2.php in this example)? I mean, query result is shown immediately.. I think about onBlur() method, but I don't know how create similar request with JavaScript. Maybe it is possible with AJAX?

I would be grateful for any advice. Thx a lot.

in your <head>

<script src="http://code.jquery.com/jquery-1.9.1.js"></script>

<script>
function jqAjaxForm(data2send, file2send, dataConteiner) { 

    if(data2send.indexOf("#") != -1){
        data2send = jQuery(data2send).serialize();
    }

    jQuery(dataConteiner).html("<option>Loading...</option>");
    jQuery.ajax({
        type    : "POST",
        url     : file2send,
        data    : data2send,
        success : function(data){ jQuery(dataConteiner).html(data); },
        error   : function(data){ jQuery(dataConteiner).html(data); },
        cache   : false
    });

}
</script>

instead in your body

<body>
    <div id="target"><div>
    <form name="step1" action="step2.php" method="POST" id="form">
        Email: <input type="text" name="email" class="input-medium">
        <button class="btn btn-small btn-warning" type="submit" on click = "jqAjaxForm('#form', 'step2.php','#target')">Check</button> 
    </form>
</body>

i guess this is what you want to achieve

<form name="step1" action="step2.php" method="POST" onsubmit = "return postFormAsynchronous(this)" >
Email: <input type="text" name="email" class="input-medium">
<button class="btn btn-small btn-warning" type="submit">Check</button> 
</form>

then in javascript put this function

function postFormAsynchronous(frm) {
    frm = $(frm);
    $.ajax({
        type: frm.attr('method'),
        url: frm.attr('action'),
        data: frm.serialize(),
        success: function (data, status, xhr) {

       //your data from server will appear in data then do what ever with it

        }
    });

    return false;
}

this will make an ajax call to your given action php script with post data email

on server you can carry on with your existing code

    $email = $_POST['email'];
      ........
      ......

oh yeah dont forget to include Jquery in your html page.

happy coding :)

Yes you can do either by calling ajax function on blur OR by validating submit, and do not submit the form until you have reponse from ajax.

For example when you click on submit, call function

<code>
onsubmit = "return Validate()"
</code>

In Validate function, send ajax request to different page, and do what you need on the basis of the value in your email field at the moment. You can get value of email field using either jquery or javascript.

<code>
     document.getElementById('email-field-id').value;
</code>

or

<code>
     $('#email-field-id').val();
</code>