我在php中有简单的代码输入数据库的名称和地址。如果数据库中已经存在名称,则打印消息说它已经存在,如果没有将数据输入数据库。但我想要消息必须是显示在保存按钮附近
<form method="POST" action="" >
<table>
<tr>
<td><br>Name t</td><td><br><input type="text" name="Name" id="wgtmsr" required/></td>
</tr>
<tr>
<td><br>Address</td>
<td><br/><textarea rows="3" cols="33" name="Address" id="inc_address" required></textarea></td>
</tr>
<tr>
<td><br></td><td><input type="submit" class="sav" name="rep" value="Save" id="btnsize" /></td>
</tr>
</table>
<?php
if (isset($_POST['rep']))
{
$Name=$_POST['Name'];
$Address=$_POST['Address'];
try{
$test=/* query check if name already present in database*/
if(!$test)
{
/*then insert into database*/
}else{
?> <div id="msg">
<?php echo "alreday present";?>
</div>
<?php
}
}catch(Exception $e) {
//echo "Error ".$e->getMessage();
}
}
但div正在高于桌面。请给出建议,以便我如何在保存按钮附近设置它 式:
#msg
{
color:#EA6262;
font-size:14pt;
margin-left:27% ;
margin-bttom:5% ;
}
答案 0 :(得分:0)
在保存按钮之后插入div,如
<input type="submit" class="sav" name="rep" value="Save" id="btnsize" /><span class="err" id="add_err"></span>
您可以使用javascript打印错误。
document.getElementById("add_err").innerHTML = "Already having name!";
我尝试了类似的工作
<?php
/**
* Created by PhpStorm.
* User: root
* Date: 3/9/15
* Time: 4:03 PM
*/
?>
<html>
<head>
<title></title>
</head>
<body>
<form>
<input type="submit" name="submit" /><span class="adderr" name="adderr" id="adderr"></span>
</form>
<?php
$var=0;
if($var==0)
{?>
<script>
document.getElementById("adderr").innerHTML = "Already having name!";
</script>
<?php
}
?>
</body>
</html>