dcsimg
www.webdeveloper.com
Results 1 to 4 of 4

Thread: form not receiving all the fields to the email

  1. #1
    Join Date
    Nov 2017
    Posts
    4

    Smile form not receiving all the fields to the email

    I have created a for using PHP. and I am unable to receive all the forms fields .. only message is receiving to my email.

    Below is my code.

    form.php


    Code:
    <?php include('form_process.php'); ?>
    =
    <link rel="stylesheet" href="form.css" type="text/css">
    <div class="container">  
      <form id="contact" action="<?= htmlspecialchars($_SERVER["PHP_SELF"]) ?>" method="post">
        <h3>Contact</h3>
        <h4>Contact us today, and get reply with in 24 hours!</h4>
        <fieldset>
          <input placeholder="Your name" type="text" name="name" value="<?= $name ?>" tabindex="1" autofocus>
          <span class="error"><?= $name_error ?></span>
        </fieldset>
        <fieldset>
          <input placeholder="Your Email Address" type="text" name="email" value="<?= $email ?>" tabindex="2">
          <span class="error"><?= $email_error ?></span>
        </fieldset>
        <fieldset>
          <input placeholder="Your Phone Number" type="text" name="phone" value="<?= $phone ?>" tabindex="3">
          <span class="error"><?= $phone_error ?></span>
        </fieldset>
        <fieldset>
    
        <fieldset>
          <textarea value="<?= $message ?>" name="message" tabindex="4" placeholder="No of adults, No of children and Date for ride"  >
          </textarea>
        </fieldset>
        <fieldset>
          <button name="submit" type="submit" id="contact-submit" data-submit="...Sending">Submit</button>
        </fieldset>
        <div class="success"><?= $success ?></div>
      </form>
    </div>


    form_process.php


    Code:
    <?php 
    
    // define variables and set to empty values
    $name_error = $email_error = $phone_error  = "";
    $name = $email = $phone = $message = $success = "";
    
    //form is submitted with POST method
    if ($_SERVER["REQUEST_METHOD"] == "POST") {
      if (empty($_POST["name"])) {
        $name_error = "Name is required";
      } else {
        $name = test_input($_POST["name"]);
    
        // check if name only contains letters and whitespace
        if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
          $name_error = "Only letters and white space allowed"; 
        }
    	
      }
    
      if (empty($_POST["email"])) {
        $email_error = "Email is required";
      } else {
        $email = test_input($_POST["email"]);
        // check if e-mail address is well-formed
        if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
          $email_error = "Invalid email format"; 
        }
      }
      
      if (empty($_POST["phone"])) {
        $phone_error = "Phone is required";
      } else {
        $phone = test_input($_POST["phone"]);
        // check if e-mail address is well-formed
        if (!preg_match("/^(\d[\s-]?)?[\(\[\s-]{0,2}?\d{3}[\)\]\s-]{0,2}?\d{3}[\s-]?\d{4}$/i",$phone)) {
          $phone_error = "Invalid phone number"; 
        }
      }
    
    
    
      if (empty($_POST["message"])) {
        $message = "";
      } else {
        $message = test_input($_POST["message"]);
      }
      
      if ($name_error == '' and $email_error == '' and $phone_error == ''){
          $message_body = '';
    	  
          unset($_POST['submit']);
          foreach ($_POST as $key => $value){
              $message_body .=  "$key: $value\n";
    		  $phone.="$KEY: $value";
    		 
    		 
          }
          
          $to = 'subonlinemaldives@gmail.com';
          $subject = 'Contact Form Submit';
    	  
           'Reply-To: ikrishak@gmail.com';
          if (mail($to, $subject, $message) ){
              $success = "Message sent, thank you for contacting us!";
              $name = $email = $phone = $message = '';
          }
      }
      
    }
    
    function test_input($data) {
      $data = trim($data);
      $data = stripslashes($data);
      $data = htmlspecialchars($data);
      return $data;
    }

    A help is much appreciated ..
    Thanks

  2. #2
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    5,425
    Change :
    HTML Code:
    <textarea value="<?= $message ?>" name="message" tabindex="4" placeholder="No of adults, No of children and Date for ride"  >
          </textarea>
    To:
    HTML Code:
    <textarea name="message" tabindex="4" placeholder="No of adults, No of children and Date for ride"  ><?= $message ?></textarea>
    HTML Code:
     <button name="submit" type="submit" id="contact-submit" data-submit="...Sending">Submit</button>
    is not a proper submit button... it should be
    HTML Code:
     <input name="submit" type="submit" value="Submit">
    Avoid using name="name" name conventions, use something like name="group_name"


    HTML Code:
    <form id="contact" action="<?= htmlspecialchars($_SERVER["PHP_SELF"]) ?>" method="post">
    has no enctype="multipart/form-data" setting and I assume that you are submitting to the same page when you use <?= htmlspecialchars($_SERVER["PHP_SELF"]) ?> so using basename( $_SERVER["PHP_SELF"]); will just allow the last part of the URL

    If you use a HTML5 web form and its designated fields, you can unload some of the checks by using appropriate attributes in the tag, for example <input name="email" type="email" value="" required> will expect an email address to be entered and the field to be filled not empty.

    Using HTML5 code on older browsers means that they degrade to plain text inputs, so your script needs to issue a web page with support for browsers that do not support HTML5 inputs, there is still some issue over what browsers now support as the full set of HTML5 fields were not supported by all browsers, this does not mean you can't use them, it means that currently you would have to build in support from existing libraries / frameworks to add in the feature you want, like a date picker if the browser does not support the current HTML% date picker field.

    You only use filter_var once when you should be using it more and also your using the input arrays directly when you should be filtering in to a safe array that you then use because you know that the data in those fields is safe to use.

    You are performing some routines that are not needed as well as repeating tests over in different methods, you can use empty() to test for an empty variable over having to construct a comparison like $mystring == "" a simpler empty($mystring) method exists.

    Your web page for errors is not very forgiving, you should return and display any fields that you have already got data inputs to, by adding the field name in the value field with [] you can have that field contents displayed in that field so that you are not presenting an empty form because of one error in a field...
    Last edited by \\.\; 11-14-2017 at 01:03 PM.
    --> JavaScript Frameworks like JQuery, Angular, Node <--
    ... and please remember to wrap code with forum BBCode tags:-

    [CODE]...[/CODE] [HTML]...[/HTML] [PHP]...[/PHP]

    If you can't think outside the box, you will be trapped forever with no escape...

  3. #3
    Join Date
    Nov 2017
    Posts
    4
    Thanks for the suggestion.. I have corrected those accordingly.. Actually the problem I asked previously is with this code below.
    Code:
    unset($_POST['submit']);
          foreach ($_POST as $key => $value){
              $message_body .=  "$key: $value\n";
    		  $phone.="$KEY: $value";
    I have changed my codes and now I am able to receive the form fields to my email. But my message field is repeating. I have played around with the code and don't know where I am having the mistakes.
    ---------------------------
    This is how I get the fields input now

    This isi myname: Christiano // this isi my (is the message input)
    email: phil.rentalsinc@gmail.com
    phone: 982-333-2333
    message: This isi my // message is repeating here again
    -----------------------------------------

    One last help and I am done with the form.

    By the way this the link to the contact form for your reference

    http://www.ticketsubmarine.com/form.php



    Thanks

  4. #4
    Join Date
    Nov 2017
    Location
    Amsterdam,Netherlands
    Posts
    1
    Thanks I also got solve my problem through this post.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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