【发布时间】:2014-04-03 19:35:50
【问题描述】:
我编写了这个简单的代码来根据手机号码(用户提供的手机号码)获取客户姓名和地址。我已使用 Fetch 按钮 成功完成此操作。但我需要没有按钮选项。
当用户在文本字段中输入手机号码时,他们将转到下一个字段(姓名)。如果数据库中已存在手机号码,则应从数据库中获取客户名称和地址。如果手机号码不存在,他们将输入剩余的两个字段。有什么帮助吗?
require('config.php');
if(isset($_POST['submit']))
{
$cmobile = $_POST['mobile'];
$cname = $_POST['name'];
$caddress = $_POST['address'];
try
{
$stmt = $conn->prepare("INSERT INTO autocomplete ( mobile, name, address ) VALUES ( :cmobile, :cname, :caddress )");
$conn->errorInfo();
$stmt->bindParam(':cmobile', $cmobile, PDO::PARAM_STR);
$stmt->bindParam(':cname', $cname, PDO::PARAM_STR);
$stmt->bindParam(':caddress', $caddress, PDO::PARAM_STR);
$stmt->execute();
}
catch(PDOException $e)
{
$e->getMessage();
}
if($stmt)
{
echo "success";
}
else
{
echo "error";
}
}
if(isset($_POST['fetch']))
{
$cmobile = $_POST['mobile'];
try
{
$stmt = $conn->prepare("SELECT * FROM autocomplete WHERE mobile = :cmobile");
$conn->errorInfo();
$stmt->bindParam(':cmobile', $cmobile, PDO::PARAM_STR);
$stmt->execute();
while($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
$aname = $row['name'];
$aaddress = $row['address'];
}
}
catch(PDOException $e)
{
$e->getMessage();
}
if($stmt)
{
echo "fetch success";
}
else
{
echo "fetch error";
}
}
和
<form method="post" name="form" action="">
mobile number : <input type="text" name="mobile" /><br />
name : <input type="text" name="name" value="<?= $aname ?>" /><br />
address : <input type="text" name="address" value="<?= $aaddress ?>" />
<br />
<input type="submit" name="submit" value="Submit" />
<button name="fetch">Fetch</button>
</form>
【问题讨论】: