首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在不刷新页面的情况下使用数据库查找插入表单值

在不刷新页面的情况下使用数据库查找插入表单值
EN

Stack Overflow用户
提问于 2013-11-23 15:40:27
回答 1查看 1.5K关注 0票数 0

我有一个有5个字段的表格。姓名、出生日期、职业、出生地点

当用户填写名称和姓氏时,我希望从数据库中填充表单的其余部分,而无需刷新页面或用户进行任何操作。

我使用的是php和jquery。

这是我的html页面:

代码语言:javascript
复制
<html>
<head>
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
</head>
<body>
<input type="text" name="name" id="name">
<input type="text" name="lastname" id="lastname">
<input type="text" name="occupation" id="occupation">
<input type="text" name="date_of_birth" id="date_of_birth">
<input type="text" name="place_of_birth" id="place_of_birth">
<script type="text/javascript">

$('#lastname').blur(function () 
    {
        var name = $("#name").val();
        var lastname = $("#lastname").val();

    $.ajax({
        url: "get.php",
        type: "POST", 
        data: "name="+name+"&lastname="+lastname,
        success: function(data)
        {
            if (data == "Error")
            {
                alert("error-1"+data);
            }
            else
            {
                var fields = data.split(",");
                if (fields.length < 3) 
                {
                    alert("error-2"+data);  
                }
                else
                {

                    $("#occupation").val(fields[0]);
                    $("#date_of_birth").val(fields[1]);
                    $("#place_of_birth").val(fields[2]);

                }
            }
        },
        error: function(xhr, status, error) 
        {
               alert("error-3"+data);    
        }   
    });
});
</script>
</body>
</html>

下面是php页面:

代码语言:javascript
复制
<?php
$name= $_REQUEST['name'];
$lastname= $_REQUEST['lastname'];
if($name == "mike" && $lastname = "brown")
{
    echo  "cook,1980-10-10,NYC";
}

?>

现在起作用了。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-11-23 15:46:52

--编辑#1也许这个示例可以帮助您理解如何使用它:

代码语言:javascript
复制
$.ajax({
    url: "phppage.php",
    type: "POST", // Or get as you like
    data: "name="+name+"&lastname="+lastname,
    success: function(data)
    {
        // As i said, here in "data" you have what the server returned
        // You can return the last field delimited with a , and do something like:
        if (data == "Error")
        {
            // If something went wrong in your Database or invalid "name" or "last name"
            // You can return "Error" in the PHP page so the Javascript know something is wrong
            // and handle this error
        }
        else
        {
            var fields = data.split(",");
            if (fields.length < 3) {
                // As above.
                // Something went wrong or invalid response
            }
            else
            {
                // In fields array you have what server said.
                            // Here you can reload the page, change page do what you want
            }
        }

    },
    error: function(xhr, status, error) 
    {
        // Error here
    }   
});

它将名称和姓氏传递给服务器,并等待响应,如: field1、field2、field3

PHP页面应该是..。

代码语言:javascript
复制
<?php

// Connect to the server
// Send query to the server

if ($isEverythingOK) {
    echo $field1 . "," . $field2 . "," . $field3;
}
else {
    echo "Error";
}

?>

Ajax - jQuery Ajax + PHP页面

在php页面中传递5个字段,将其添加到数据库中,如果一切正常,则返回" OK“之类的内容,如果发生错误,则返回一个错误。

示例

代码语言:javascript
复制
$.ajax({
    url : "URL to PHP page",
    success : function (data) {
          if (data === "OK") { /* all ok, here you can update the page to show the new field */ alert("OK"); }
          else { /* Error */ alert("Error"); }
    },
    error : function (xhr, status, error) {
        // Request error
        alert("Error");
    }
});
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20164255

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档