在输入字段旁边显示错误消息

时间:2013-02-04 05:08:01

标签: php html

我想在输入字段旁边显示php错误消息。我可以这样做吗?

这是我在同一页面上的php和html代码。它只是简单的测试代码..任何帮助都会很棒

尝试将php放在输入旁边,但这不起作用..我把它放在一个值

<?php

if ( isset( $_POST['submit'] ) ) { 
$mysqli = new mysqli("localhost", "root", "", "test");


$fname = $_POST["fname"];
$lname = $_POST["lname"];

if (empty($henny)) { $errorhenny = '<div class="isa_error">Frist name is required<br></div>'; } 

if (empty($lname)) {
echo '<div class="isa_error">Last name is required<br></div>';
}

else{

$stmt = $mysqli->prepare("INSERT INTO test (Firstname, Lastname) VALUES (?, ?)");

$stmt->bind_param("ss", $fname, $lname);
$stmt->execute();

$stmt->close();

$mysqli->close();
}
}
?>


<html>
<body>
<form action="" method="post">
<input type="text" name="fname" value= "<?php echo $errorhenny; ?>">
<input type="text" name="lname">
<input type="submit" name="submit" value="Submit">
</form>
</html>

4 个答案:

答案 0 :(得分:0)

尝试使用ajax ...更容易和更好

答案 1 :(得分:0)

在php变量

中分配错误消息
if (empty($fname)) { 
     $errorfname= '<div class="isa_error">Frist name is required</div>'; 
} 
if (empty($lname)) { 
     $errorlname = '<div class="isa_error">Last name is required</div>'; 
} 

然后在html中打印变量

<html>
<body>
<form action="" method="post">
<input type="text" name="fname">
<?php if(isset($errorfname)) { echo $errorfname; } ?> // fname error message
<input type="text" name="lname">
 <?php if(isset($errorlname)) { echo $errorlname; } ?> //lname error message
<input type="submit" name="submit" value="Submit">
</form>

答案 2 :(得分:0)

试试这个:

<?php
$ferr = '';
$lerr = '';
if ( isset( $_POST['submit'] ) ) { 
$mysqli = new mysqli("localhost", "root", "", "test");


$fname = $_POST["fname"];
$lname = $_POST["lname"];

if (empty($fname)) {
$ferr = 'First name is required';
}

if (empty($lname)) {
$lerr = 'Last name is required';
}

else{

$stmt = $mysqli->prepare("INSERT INTO test (Firstname, Lastname) VALUES (?, ?)");

$stmt->bind_param("ss", $fname, $lname);
$stmt->execute();

$stmt->close();

$mysqli->close();
}
}
?>


<html>
<body>
<form action="" method="post">
<div>
    <input type="text" name="fname">
    <span class="isa_error"><?php echo $ferr;?></span>
</div>
<div>
    <input type="text" name="lname">
    <span class="isa_error"><?php echo $lerr;?></span>
</div>
<input type="submit" name="submit" value="Submit">
</form>
</html>

答案 3 :(得分:0)

<?php

if ( isset( $_POST['submit'] ) ) { 
  $mysqli = new mysqli("localhost", "root", "", "test");


  $fname = $_POST["fname"];
  $lname = $_POST["lname"];
  if(empty($fname) || empty($lname)){
    if (empty($fname)) { 
       $errorfname = '<div class="isa_error">Frist name is required<br></div>'; 
    } 

    if (empty($lname)) {
       $errorlastname = '<div class="isa_error">Last name is required<br></div>';
    }
  }
  else{

    $stmt = $mysqli->prepare("INSERT INTO test (Firstname, Lastname) VALUES (?, ?)");

    $stmt->bind_param("ss", $fname, $lname);
    $stmt->execute();

    $stmt->close();

    $mysqli->close();
  }
}
?>


<html>
<body>
<form action="" method="post">
<input type="text" name="fname" value= ""><?php echo isset($errorfname) ? $errorfname : ''; ?>
<input type="text" name="lname"><?php echo isset($errorlastname) ? $errorlastname : ''; ?>
<input type="submit" name="submit" value="Submit">
</form>
</html>