Jump to content

Split string to capture first name ?


ttmt

Recommended Posts

Hi all

 

I have a simple email script that shows a message when the email has been sent. The message uses the name used in the email form

 

$message = "Thank you $name_field, we will be in touch soon.";

 

This name could be two names - first name and last last name. Is it possible to split this string and just use the first name. I was thinking it might be possible to use the space character to determine the split.

 

<?php
   $message = "";
   if(isset($_POST['submit'])) {
      $errors = array();
      $required_fields = array('name','email','message');
      foreach($required_fields as $fieldname){
         if(!isset($_POST[$fieldname]) || empty($_POST[$fieldname])){
            $errors[] = $fieldname;
         }
      }
      if(empty($errors)){
         $to = "info@ttmt.org.uk";
         $name_field = $_POST['name'];
         $email_field = $_POST['email'];
         $subject = "Email from Website";
         $message = $_POST['message'];
         //
         $body = "From: $name_field\n E-Mail: $email_field\n Message:\n $message";
         //
         mail($to, $subject, $body);
         $message = "Thank you $name_field, we will be in touch soon.";
      }else{
         $message = "Please complete all fields.";
      }
   } 
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.