Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder cineweekly.com's Avatar
    Join Date
    Aug 2010
    Posts
    485
    Thanks
    14
    Thanked 3 Times in 3 Posts

    Question How do I make input fields required?

    How can I make "Name" and "Phone" required fields in the code below. Any help is appreciated.

    PHP Code:
    <?
    $text
    =$_POST["text"];
    $email=strtolower($_POST["email"]);
    if (
    strpos($email,"@")==or strpos($email,".")==0){$email="";}
    if (
    $email!="" and $text!=""){
       
    $message="$email\n\n$text";
       
    mail("webmaster@mysite.com","Contact form"$message,"From: <webmaster@mysite.com>\nContent-Type: text/plain");
       print 
    "<center>Message Sent!</center>";
    }else{
    ?>

    <form action=<? print $_SERVER["PHP_SELF"]; ?> method=post>
    Name:
    <INPUT NAME="name" SIZE="35" MAXLENGTH="65">
    Phone:
    <INPUT NAME="phone" SIZE="35" MAXLENGTH="65">
    E-mail:
    <INPUT NAME="email" SIZE="35" MAXLENGTH="65">
    Your Message:
    <TEXTAREA name=text cols=40 rows=5><? print $text?></TEXTAREA>
    <INPUT TYPE="SUBMIT" NAME=submit VALUE=Submit>
    </form>

    <? ?>
    Last edited by cineweekly.com; 05-21-2011 at 08:03 AM.

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,853
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    PHP Code:
    if (strpos($email,"@")==or strpos($email,".")==0){$email="";} 
    That's not sufficient to check the format of email, since as per you logic xx.@ will be a valid email.

    You need to use a regular expression to match or filter_var()

    You may modify your code as per the following pseudo
    PHP Code:
    if(Email format is invalid)
     
    $errors[]="Email is invalid"
    if( value of "name" is empty or doesnt have the required length )
     
    $errors[]="Name is invalid/short"
    .....

    if(!isset(
    $errors)){
    //send email

    PS: When using strpos(), you'd need to use === operator to compare the value returned. Check http://php.net/strpos#refsect1-funct...s-returnvalues
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #3
    Regular Coder cineweekly.com's Avatar
    Join Date
    Aug 2010
    Posts
    485
    Thanks
    14
    Thanked 3 Times in 3 Posts
    Quote Originally Posted by abduraooft View Post
    That's not sufficient to check the format of email, since as per you logic xx.@ will be a valid email.

    You need to...
    I originally had this titled to acknowledge that I'm an absolute beginner with PHP but no one was responding. Should have kept it there cause I'm totally lost. I copied this code from a tutorial and I'm not too concerned with security (the site is for local clients only). Could you point out the code I need to add and remove to make the Name and Phone fields required using the same technique as the code I have? Or at least just so they're sent with the e-mail.
    Last edited by cineweekly.com; 05-21-2011 at 08:21 PM.

  • #4
    Regular Coder djh101's Avatar
    Join Date
    May 2009
    Location
    California
    Posts
    616
    Thanks
    48
    Thanked 65 Times in 65 Posts
    If all you want to do is make them required, you would simply need to check if their values are set or null and act accordingly. Something like this:
    PHP Code:
    if($email){
    //do something; email field is not empty
    } else {
    //email field is empty; do something else

    There's a lot of ways to check if a field is empty. In forms I usually use 'strlen($variable) > x' since form fields usually require a specific length. Basically, for all the fields you want to require, you would want to check if the data they submitted was empty and act accordingly; if they are not empty, submit the form to your database (or wherever the data is being submitted to), and if they are, give an error message. For simplicity, you could do this all on one line.
    PHP Code:
    if(strlen($name) > && strlen($phone) > 0){
    echo 
    "Form submission successful!"
    } else {
    echo 
    "Please fill out all forms."

    As far as regex goes (what Abdura was explaining), you shouldn't worry too much about that now if you're just learning PHP. My advice would be to get your form working without regex first and then work on implementing it afterwards.
    "Yeah science!"
    Online Science Tools

  • #5
    Regular Coder cineweekly.com's Avatar
    Join Date
    Aug 2010
    Posts
    485
    Thanks
    14
    Thanked 3 Times in 3 Posts
    Say we skip the required part and I just want to have the name and phone sent to my e-mail along with their e-mail address and comments which are already sending properly. I have experimented with this code that's already in the original code:
    Code:
    if ($email!="" and $text!=""){
       $message="$email\n\n$text";
     mail("info@example.com","New Message!", $message,"From: <info@example.com>\nContent-Type: text/plain");
    From my minor ActionScript experience I would think this is saying "If the email and text fields aren't empty then send the email and text comments." So I tried adding $name and $phone but couldn't get it to work. I'm asking the code to be implemented into the existing code cause I'm clueless to PHP.
    Last edited by cineweekly.com; 05-22-2011 at 09:25 PM.

  • #6
    Regular Coder djh101's Avatar
    Join Date
    May 2009
    Location
    California
    Posts
    616
    Thanks
    48
    Thanked 65 Times in 65 Posts
    Well $name and $phone are always going to be blank because you haven't assigned them values. You need to set them to the values sent by the forms, which will be done (just like with email and text) using $_POST['fieldname'].
    PHP Code:
    $phone $_POST['phone'];
    $name $_POST['name']; 
    Or you could skip that entirely (creating variables with the post values) and simply use the $_POST variables in your statement:
    PHP Code:
    if ($_POST['phone'] != "" && $_POST['name'] != ""){ 
    Last edited by djh101; 05-22-2011 at 09:34 PM.
    "Yeah science!"
    Online Science Tools


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •