-2

有一个代码:

<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>

那就是: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 " %";
}
?>

那么,是否可以在不提交表单并重定向到新页面的情况下在数据库中获取请求(本例中为 step2.php)?我的意思是,查询结果会立即显示出来。我想到了 onBlur() 方法,但我不知道如何使用 JavaScript 创建类似的请求。也许使用 AJAX 是可能的?

我将不胜感激任何建议。多谢。

4

3 回答 3

0

我想这就是你想要实现的

<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>

然后在javascript中放置这个函数

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;
}

这将使用发布数据电子邮件对给定的操作 php 脚本进行 ajax 调用

在服务器上,您可以继续使用现有代码

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

哦,是的,不要忘记在您的 html 页面中包含 Jquery。

快乐编码:)

于 2013-07-24T07:13:53.433 回答
0

是的,您可以通过在模糊上调用 ajax 函数或通过验证提交来做到这一点,并且在您收到 ajax 的响应之前不要提交表单。

例如,当您单击提交时,调用函数

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

在验证功能中,将ajax请求发送到不同的页面,并根据您当前电子邮件字段中的值做您需要的事情。您可以使用 jquery 或 javascript 获取电子邮件字段的值。

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

或者

<code>
     $('#email-field-id').val();
</code>
于 2013-07-24T07:17:03.703 回答
0

在你的<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>

而是在你的身体里

<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>
于 2013-07-24T07:18:34.650 回答