PHP联系表单网页

时间:2016-05-24 08:19:09

标签: php html forms validation session

我对联系方式有一个很大的问题。 我已经很长时间试图获得表格并运行whit php。 我还有一个验证码功能,我现在正在查看。

但我需要帮助惠特形式和PHP部分:  1.检查所有表单字段并在输入字段不正确时给出错误消息(右侧是输入字段的红色)。  2.如果检查表单字段后一切正常,则发送电子邮件。并且发出电子邮件已发送的消息。

contactform.php

public class ArrivalRecord {


    final StringProperty bid;
    final StringProperty firstName;
    final StringProperty lastName;
    final StringProperty arrival;
    final StringProperty checkout;
    final StringProperty roomNumber;
    final StringProperty roomType;
    final StringProperty nights;
    final StringProperty breakfast;
    final StringProperty amount;
    final StringProperty comment;


    ArrivalRecord(String bid, String firstName, String lastName,
            String arrival, String checkout, String roomNumber,
            String roomType, String nights, String breakfast,
            String amount, String comment) {

        this.bid = new SimpleStringProperty(bid);
        this.firstName = new SimpleStringProperty(firstName);
        this.lastName = new SimpleStringProperty(lastName);
        this.arrival = new SimpleStringProperty(arrival);
        this.checkout = new SimpleStringProperty(checkout);
        this.roomNumber = new SimpleStringProperty(roomNumber);
        this.roomType = new SimpleStringProperty(roomType);
        this.nights = new SimpleStringProperty(nights);
        this.breakfast = new SimpleStringProperty(breakfast);
        this.amount = new SimpleStringProperty(amount);
        this.comment = new SimpleStringProperty(comment);
    }


    public String getBid() {
        return bid.get();
    }


    public String getFirstName() {
        return firstName.get();
    }


    public String getLastName() {
        return lastName.get();
    }


    public String getArrival() {
        return arrival.get();
    }


    public String getCheckout() {
        return checkout.get();
    }


    public String getRoomNumber() {
        return roomNumber.get();
    }


    public String getRoomType() {
        return roomType.get();
    }


    public String getNights() {
        return nights.get();
    }


    public String getBreakfast() {
        return breakfast.get();
    }


    public String getAmount() {
        return amount.get();
    }


    public String getComment() {
        return comment.get();
    }

    public StringProperty getBidProperty() {
        return bid;
    }


    public StringProperty getFirstNameProperty() {
        return firstName;
    }


    public StringProperty getLastNameProperty() {
        return lastName;
    }


    public StringProperty getArrivalProperty() {
        return arrival;
    }


    public StringProperty getCheckoutProperty() {
        return checkout;
    }


    public StringProperty getRoomNumberProperty() {
        return roomNumber;
    }


    public StringProperty getRoomTypeProperty() {
        return roomType;
    }


    public StringProperty getNightsProperty() {
        return nights;
    }


    public StringProperty getBreakfastProperty() {
        return breakfast;
    }


    public StringProperty getAmountProperty() {
        return amount;
    }


    public StringProperty getCommentProperty() {
        return comment;
    }


}

Send_form_email.php 如果我使用它,此代码正常工作:

然后用户被迫远离表单。 并且所有错误消息都在空白的php页面上给出,而不是以验证码的形式给出。

<?php session_start();
if (isset($_POST['Submit'])) {
    if (empty($_POST["first_name"])) {
        $nameError = "Name is required";
    }
    else {
        $name = test_input($_POST["first_name"]);    
        // check name only contains letters and whitespace
        if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
            $nameError = "Only letters and white space allowed";
        }
    }

    // code for check server side validation
    if(empty($_SESSION['6_letters_code'] ) || strcasecmp($_SESSION['6_letters_code'], $_POST['6_letters_code']) != 0){  
        $msg="<span style='color:red'>The Validation code does not match!</span>";// Captcha verification is incorrect.     
    }else{// Captcha verification is Correct. Final Code Execute here!      
        $msg="<span style='color:green'>The Validation code has been matched.</span>";      
    }
}   
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
  <meta name="generator" content="CoffeeCup HTML Editor (www.coffeecup.com)">
  <meta name="dcterms.created" content="sø, 22 maj 2016 18:08:14 GMT">
  <meta name="description" content="">
  <meta name="keywords" content="">
  <title></title>    
</head>

<body>    
    <form action="" method="post" name="contactform" id="contactform" <?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">     
        <table width="550px">     
            <tr>
        <td valign="top">     
                <label for="first_name">First Name *</label>
            </td>     
                <td valign="top">
                <input  type="text" name="first_name" id="first_name" maxlength="50" size="30">
                <span class="error">* <?php echo $nameError;?></span>
                </td>
            </tr>
            <tr>     
                <td valign="top"">     
                <label for="last_name">Last Name *</label>     
                </td>     
                <td valign="top">     
                <input  type="text" name="last_name" maxlength="50" size="30">
            </td>
            </tr>     
            <tr>     
                <td valign="top">     
                <label for="email">Email Address *</label>     
                </td>     
                <td valign="top">     
                <input  type="text" name="email" maxlength="80" size="30">     
                </td>     
            </tr>     
            <tr>     
                <td valign="top">     
                <label for="telephone">Telephone Number</label>     
                </td>     
                <td valign="top">     
                <input  type="text" name="telephone" maxlength="30" size="30">     
                </td>     
            </tr>     
            <tr>     
                <td valign="top">     
                <label for="comments">Comments *</label>     
                </td>     
                <td valign="top">     
                <textarea  name="comments" maxlength="1000" cols="25" rows="6"></textarea>     
                </td>     
            </tr>     
            <tr>     
                <td colspan="2" style="text-align:center">
                <?php if(isset($msg)){?>
                <?php echo $msg;?><?php } ?><br>
                    <img src="captcha_code_file.php?rand=<?php echo rand(); ?>" id='captchaimg' ><br>
                    <label for='message'>Enter the code above here :</label><br>
                    <input id="6_letters_code" name="6_letters_code" type="text"><br>
                    <small>Can't read the image? click <a href='javascript: refreshCaptcha();'>here</a> to refresh</small>
                    </p>    
                <input name="Submit" type="submit" onclick="return validate();" value="Submit" class="button1">     
                </td>     
            </tr>     
        </table>     
    </form>

  <script language='JavaScript' type='text/javascript'>
    function refreshCaptcha() {
        var img = document.images['captchaimg'];
        img.src = img.src.substring(0,img.src.lastIndexOf("?"))+"?rand="+Math.random()*1000;
    }
  </script>

  </body>
</html>

Send_email_form.php

1 个答案:

答案 0 :(得分:0)

这是因为您正在使用此if语句。

if(strlen($error_message) > 0) { 
   died($error_message);
}

如果要在实际联系表单上显示错误消息。你需要使用

// Send_email_form.php中的更改

 if(strlen($error_message) > 0) 
 { 
    header('Location: contactform.php?err='.$error_message);
    exit;
 }

//更改contactform.php - 使用这段代码在页面上显示错误。

if(isset($_GET['err']) && !empty( $_GET['err'] )
{
    echo $_GET['err'];
}