Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I am trying to add some codes to my webpage. I just made a contact form and I need some help adding more info to it. I also want to add some information and Error checking for required information -Address -City -Zip -Birthday

    <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>

this is my php so far

    <?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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
384 views
Welcome To Ask or Share your Answers For Others

1 Answer

You can do this way...

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;

I have removed the check_input() and placed a required field in HTML form which does the validation depending upon the input type="".

If you still want to do PHP vaidation you can do this way...

in php

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

in your html code..

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...