duano3557 2013-12-10 16:37
浏览 9

too long

I am new to php and I have post forms down but not I want some of my imput fields to be required.

I want this form to force the user to fill out the required fields but then be directed to my process.php page which will send me an email with the data the form collected. Right now the data is being posted at the bottom of the page. Please help me direct the data to an email.

    <!DOCTYPE HTML> 
    <html>
    <head>
    <style>
    .error {color: #FF0000;}
    </style>
    </head>
    <body> 

    <?php
    // define variables and set to empty values
    $nameErr = $emailErr = $genderErr = $websiteErr = "";
    $name = $email = $gender = $comment = $website = "";

    if ($_SERVER["REQUEST_METHOD"] == "POST")
    {

       if (empty($_POST["name"]))
         {$nameErr = "Name is required";}
       else
         {$name = test_input($_POST["name"]);}

       if (empty($_POST["email"]))
         {$emailErr = "Email is required";}
       else
         {$email = test_input($_POST["email"]);}

       if (empty($_POST["website"]))
         {$website = "";}
       else
         {$website = test_input($_POST["website"]);}

       if (empty($_POST["comment"]))
         {$comment = "";}
       else
         {$comment = test_input($_POST["comment"]);}

       if (empty($_POST["gender"]))
         {$genderErr = "Gender is required";}
       else
         {$gender = test_input($_POST["gender"]);}

    }

    function test_input($data)
    {
         $data = trim($data);
         $data = stripslashes($data);
         $data = htmlspecialchars($data);
         return $data;
    }
    ?>

    <h2>PHP Form</h2>
    <p><span class="error">* required field.</span></p>
    <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"> 
       Name: <input type="text" name="name">
       <span class="error">* <?php echo $nameErr;?></span>
       <br><br>
       E-mail: <input type="text" name="email">
       <span class="error">* <?php echo $emailErr;?></span>
       <br><br>
       Website: <input type="text" name="website">
       <span class="error"><?php echo $websiteErr;?></span>
       <br><br>
       Comment: <textarea name="comment" rows="5" cols="40"></textarea>
       <br><br>
       Gender:
       <input type="radio" name="gender" value="female">Female
       <input type="radio" name="gender" value="male">Male
       <span class="error">* <?php echo $genderErr;?></span>
       <br><br>
       <input type="submit" name="submit" value="Submit"> 
    </form>

    <?php
    echo "<h2>Your Input:</h2>";
    echo $name;
    echo "<br>";
    echo $email;
    echo "<br>";
    echo $website;
    echo "<br>";
    echo $comment;
    echo "<br>";
    echo $gender;
    ?>

    </body>
    </html>

I have the code to collect that data and email it. i just don't know how to get the form to validate and then direct to the process.php page.

    $name = $_POST['name'];
    $email = $_POST['email'];
    $website = $_POST['website'];
    $comment = $_POST['comment'];
    $gender = $_POST['gender'];

    $to      = 'str@xxxxxxxx.com';
    $subject = 'Executive Plaza Contact Form Response';
    $message.= "Name: $name 
";
    $message.= "Email: $email 
";
    $message.= "email: $email 
";
    $message.= "comment: $comment 
";
    $message.= "gender: $gender 
";
    $headers = "From: Eleven55
";

    mail($to, $subject, $message, $headers);




    header('Location: thank-you.php');
  • 写回答

2条回答 默认 最新

  • doumeilmikv7099 2013-12-10 16:46
    关注

    http://php.net/manual/en/function.mail.php

    $to='youremail@yourdomain.com';
    $subject='Form data';
    $body = "Name :$name 
     ...";
    mail($to, $subject, $body);
    

    Read the mail function documentation for more info

    My bad. Do this
    document.forms.onsubmit = function(){ //onsubmit event
    //validate form data using getElementById().val and so on
    if(formdataisnotvalid){
    alert('descriptive error message');
    return false; // this will prevent the form from submitting
    }

    评论

报告相同问题?

悬赏问题

  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数