字段旁边的相同页面验证

时间:2016-06-13 12:42:28

标签: php html forms validation field

我正在尝试将我的错误代码放在字段下方的同一页面上,我目前得到的是一个单独的页面打开,上面写着:'你没有填写必填字段'

以下是表单的HTML代码:

<!-- Header Code -->

<style type="text/css">
.OBJ-1 { margin:0;background:transparent url('wpimages/wp2ce4b072.png') no-repeat left top; }
.P-1 { text-align:center;line-height:1px;font-family:"Tahoma", sans-serif;font-style:normal;font-weight:700;color:#000000;background-color:transparent;font-variant:normal;font-size:18.0px;vertical-align:0; }
.C-1 { line-height:22.00px;font-family:"Tahoma", sans-serif;font-style:normal;font-weight:700;color:#000000;background-color:transparent;text-decoration:none;font-variant:normal;font-size:18.0px;vertical-align:0; }
.C-2 { line-height:18.00px;font-family:"Georgia", serif;font-style:normal;font-weight:normal;color:#626262;background-color:transparent;text-decoration:none;font-variant:normal;font-size:16.0px;vertical-align:0; }
.OBJ-2 { border:none;border-radius:10px / 10px;background:#ffffff;font-family:Georgia;text-align:left;font-size:18px;color:#000000; }
.OBJ-3 { border:none;border-radius:10px / 10px;background:#626262;font-family:Georgia;text-align:center;font-size:18px;color:#fdfdfd; }

<!-- Form form_14 -->


<form id="form_14"  action="dbtest2.php" method="post" target="_top" enctype="application/x-www-form-urlencoded" class="OBJ-1"  style="position:absolute;left:220px;top:42px;width:335px;height:104px;">

<div  style="position:absolute;left:4px;top:4px;width:327px;height:22px;overflow:hidden;">
    <p class="Body P-1"><span class="C-1">sign up</span></p>
</div>

<label for="">

    <div  style="position:absolute;left:4px;top:30px;width:327px;height:18px;overflow:hidden;">
        <p class="Body"><span class="C-2"><br></span></p>
    </div>

</label>


<!-- Form Edit Box -->


<input name="firstname" class="OBJ-2" maxlength="32" placeholder="firstname" style="position:absolute;left:4px;top:48px;width:327px;height:24px;">



<!-- Form Button -->


<input name="submit" class="OBJ-3" type="submit" value="submit"  style="position:absolute;left:4px;top:76px;width:327px;height:24px;">

这是我的PHP字段验证脚本:

<?php

$firstname = $_POST['firstname'];
if(empty($firstname))
{
echo "You did not fill out the required fields.";
}



$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="correoervhanski"; // Database name
$tbl_name="users"; // Table name

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or
die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");



?>

如果有人可以告诉我如何将验证消息的代码放在字段下方,将非常感激。

谢谢:)

1 个答案:

答案 0 :(得分:1)

当然,

基本上,您尝试在同一页面上运行PHP脚本 - 为此,您最好的策略是使用AJAX。这很容易实现,特别是如果你正在使用jquery库;

http://api.jquery.com/jquery.ajax/