0

我想在输入字段旁边显示 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

4 回答 4

0

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

于 2013-02-04T05:24:34.437 回答
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>

于 2013-02-04T05:25:29.367 回答
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>
于 2013-02-04T05:26:13.673 回答
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>
于 2013-02-04T05:29:04.157 回答