1

我知道这个问题可能听起来很愚蠢,但我需要你的帮助。我的问题是我想在表单内打印出这个“错误”消息,而不是在表单外打印出来。目前,“错误”消息打印在 .

代码如下:

过程:

    if(!$name || !$email || !$comment){

//if not display an error message

echo "<center>All fields are required</center>";
}
else
{

mysql_select_db($database_connection, $connection);

$insert_query = sprintf("INSERT INTO contacts (name, email, comment, date, ip) VALUES (%s, %s, %s, NOW(), %s)",
                        sanitize($name, "text"),
                        sanitize($email, "text"),
                        sanitize($comment, "text"),
                        sanitize($ip, "text"));

$result = mysql_query($insert_query, $connection) or die(mysql_error());
}

形式:

<form id="contact" name="contact" action="index.php" method="post">

<fieldset>
               <li><input class="rounded" type="text" name="name" id="name" placeholder="Your Name" required />
                </li>
                <li>
                    <input class="rounded" type="text" name="email" id="email" placeholder="Your Email" required />
                </li>
                <li>
                    <textarea class="rounded" name="comment" id="comment" cols="35" rows="6" placeholder="Your Message" required ></textarea>
                </li>
                <input type="hidden" id="action" name="action" value="submitform" />
                <li>
                <input id="submit" name="submit" type="submit" value="Send" class="submitform" />
                </li>
</fieldset>
</form>

任何帮助将不胜感激!

4

3 回答 3

3

在 PHP 代码中:

if(!$name || !$email || !$comment){
//if not display an error message
    $eror_msg =  "<center>All fields are required</center>";
}

在 HTML 代码中:

<form id="contact" name="contact" action="index.php" method="post">
<?php if( isset($eror_msg) && $eror_msg != '' ) { echo $eror_msg; } ?>
<fieldset>
----------------------------
-------------------------------
于 2013-07-08T11:29:29.950 回答
2

使用以下代码: 在 PHP 代码中:

$nameError = "";
$emailError = "";
$commentError = "";
if(!$name || !$email || !$comment){
if(!$name){
  $nameError = "Name is required";  
}
if(!$email){
  $emailError = "Email is required";
}
if(!$comment) {
  $commentError = "Comment is required";
}

}
else
{

mysql_select_db($database_connection, $connection);

$insert_query = sprintf("INSERT INTO contacts (name, email, comment, date, ip) VALUES (%s, %s, %s, NOW(), %s)",
                        sanitize($name, "text"),
                        sanitize($email, "text"),
                        sanitize($comment, "text"),
                        sanitize($ip, "text"));

$result = mysql_query($insert_query, $connection) or die(mysql_error());
}

在 HTML 代码中:

<form id="contact" name="contact" action="index.php" method="post">

<fieldset>
               <li><input class="rounded" type="text" name="name" id="name" placeholder="Your Name" required />
                </li>
            <span><?php echo ((isset($nameError) && $nameError != '') ? $nameError : ''); ?> </span> //do that with all the error where you want to place
                <li>
                    <input class="rounded" type="text" name="email" id="email" placeholder="Your Email" required />
                </li>
                <li>
                    <textarea class="rounded" name="comment" id="comment" cols="35" rows="6" placeholder="Your Message" required ></textarea>
                </li>
                <input type="hidden" id="action" name="action" value="submitform" />
                <li>
                <input id="submit" name="submit" type="submit" value="Send" class="submitform" />
                </li>
</fieldset>
</form>
于 2013-07-08T11:24:31.347 回答
1
$error_msg = "";  
if(!$name || !$email || !$comment){
    $error_msg = "All fields are required";
}  

在要打印错误消息的位置添加以下代码:

<div id="error_msg">  
    echo $error_msg;
</div>
于 2013-07-08T11:32:22.477 回答