【问题标题】:HTML and PHP contact formHTML 和 PHP 联系表
【发布时间】:2015-03-06 08:31:34
【问题描述】:

我正在尝试向我的网页添加一些代码。我刚刚制作了一个联系表格,我需要一些帮助来添加更多信息。我还想添加一些信息并检查所需信息的错误 -地址 -城市 -压缩 -生日

    <p>

<html>
<body>

<p>Required fields are <b>bold</b></p>

<form action="contact.php" method="post">
<p><b>First Name:</b> <input type="text" name="firstname" /><br />
<p><b>Last Name:</b> <input type="text" name="lastname" /><br />
<p><b>Phone Number:</b> <input type="text" name="phonenumber" /><br />
<b>Subject:</b> <input type="text" name="subject" /><br />
<b>E-mail:</b> <input type="text" name="email" /><br />
Website: <input type="text" name="website"></p>

<p>Do you like this website?
<input type="radio" name="likeit" value="Yes" checked="checked" /> Yes
<input type="radio" name="likeit" value="No" /> No
<input type="radio" name="likeit" value="Not sure" /> Not sure</p>

<p>State
<select name="how">
<option value=""> -- Please select -- </option>
<option>CT</option>
<option>NJ</option>
<option>NY</option>
<option>PA</option>
</select>

<p><b>Your comments:</b><br />
<textarea name="comments" rows="10" cols="40"></textarea></p>

<p><input type="submit" value="Send it!"></p>

<p> </p>


</form>

</body>
</html>

</p>

到目前为止,这是我的 php

    <?php
/* Set e-mail recipient */
$myemail  = "colonnam@gator4198.hostgator.com";

/* Check all form inputs using check_input function */
$firstname = check_input($_POST['firstname'], "Enter your First Name");
$lastname = check_input($_POST['lastname'], "Enter your Last Name");
$subject  = check_input($_POST['subject'], "Write a subject");
$email    = check_input($_POST['email']);
$website  = check_input($_POST['website']);
$likeit   = check_input($_POST['likeit']);
$state = check_input($_POST['state']);
$comments = check_input($_POST['comments'], "Write your comments");

/* If e-mail is not valid show error message */
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email))
{
    show_error("E-mail address not valid");
}

/* If URL is not valid set $website to empty */
if (!preg_match("/^(https?:\/\/+[\w\-]+\.[\w\-]+)/i", $website))
{
    $website = '';
}

/* Let's prepare the message for the e-mail */
$message = "Hello!

Your contact form has been submitted by:

First Name: $firstname
Last Name: $lastname
E-mail: $email

【问题讨论】:

  • 问题到底是什么?
  • 执行“错误检查”客户端。对于必填字段和数据。使用 JavaScript。用那个很正常!
  • 好的 HTML 部分我想为我的联系表单添加更多信息。我希望它还向用户询问地址、城市、邮编和生日(月/日/年)然后对于 PHP 部分,我希望它对地址、城市、邮编和生日进行错误检查。
  • @animaacija 您还需要在服务器端进行一些错误检查/数据验证。您不能仅仅依靠客户端为您进行检查。

标签: php html


【解决方案1】:

你可以这样做......

index.php

<html>
<body>

<p>Required fields are <b>bold</b></p>

<form action="contact.php" method="post">
    <p><b>First Name:</b> <input type="text" name="firstname" required=""/><br/>

    <p><b>Last Name:</b> <input type="text" name="lastname" required=""/><br/>

    <p><b>Phone Number:</b> <input type="text" name="phonenumber" required=""/><br/>
        <b>Subject:</b> <input type="text" name="subject" required=""/><br/>
        <b>E-mail:</b> <input type="email" name="email" required=""/><br/>
        Website: <input type="url" name="website"></p>

    <p>Do you like this website?
        <input type="radio" name="likeit" value="Yes" checked="checked"/> Yes
        <input type="radio" name="likeit" value="No"/> No
        <input type="radio" name="likeit" value="Not sure"/> Not sure</p>

    <p>State
        <select name="how" name="how" >
            <option value=""> -- Please select --</option>
            <option>CT</option>
            <option>NJ</option>
            <option>NY</option>
            <option>PA</option>
        </select>

    <p><b>Your comments:</b><br/>
        <textarea name="comments" rows="10" cols="40" required=""></textarea></p>

    <p><input type="submit" value="Send it!"></p>

    <p></p>


</form>


</body>
</html>

contact.php

<?php
/* Set e-mail recipient */
$myemail = "colonnam@gator4198.hostgator.com";
/* Check all form inputs using check_input function */

$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$subject = $_POST['subject'];
$email = $_POST['email'];
$phnumber = $_POST['phonenumber'];
$website = $_POST['website'];
$likeit = $_POST['likeit'];
$state = $_POST['how'];
$comments = $_POST['comments'];

/* If e-mail is not valid show error message */
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email)) {
    echo "E-mail address not valid";
}

/* If URL is not valid set $website to empty */
if (!preg_match("/^(https?:\/\/+[\w\-]+\.[\w\-]+)/i", $website)) {
    $website = '';
}

/* Let's prepare the message for the e-mail */
$message = "Hello!";
echo '<p>Your contact form has been submitted by:</p>';

echo 'First Name:'. $firstname;
echo '<br>';
echo 'Last Name:'. $lastname;
echo '<br>';
echo 'E-mail:'. $email;
echo '<br>';
echo $comments;
echo '<br>';
echo $likeit;
echo '<br>';
echo $state;
echo '<br>';
echo $subject;
echo '<br>';
echo $myemail;

我删除了check_input() 并在HTML 表单中放置了一个必填字段,该字段根据input type="" 进行验证。

如果你还想做PHP vaidation,你可以这样做...

php

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

在您的html 代码中..

<input type="text" name="firstname"><span class="error">* <?php echo $nameErr;?></span>

【讨论】:

    猜你喜欢
    • 2015-06-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多