• I want to insert a mixture of PHP and HTML for a form. I inserted the following code I took directly from my working contact us page template and pasted into a raw HTML shortcode field.

    The form sends a message to my email address correctly but the validation/response message that normally appears at the top is missing.

    It’s the bit called by

    <?php echo $response; ?>

    <?php
    
      //response generation function
    
      $response = "";
    
      //function to generate response
      function my_contact_form_generate_response($type, $message){
    
        global $response;
    
        if($type == "success") $response = "<div class='callout success'><p>{$message}</p></div>";
        else $response = "<div class='callout error'><p>{$message}</p></div>";
      }
    
      // response messages
        $not_human       = "Are you sure you're human? We can't tell. Please do the sum below to confirm.";
        $missing_content = "It looks like we are missing something. Please chek the form below.";
        $email_invalid   = "That email Address doesn't look right. Please double check it";
        $message_unsent  = "Your message wasn't sent. Please have another go. If it still doesn't work then please call us using the number supplied.";
        $message_sent    = "Thank you for your enquiry. We will be in contact shortly.";
    
      // user posted variables
      $business_name = $_POST['message_business_name'];
      $first_name = $_POST['message_first_name'];
      $last_name = $_POST['message_last_name'];
      $email = $_POST['message_email'];
      $phone = $_POST['message_phone'];
      $message = $_POST['message_text'];
      $lead_source = $_POST['message_lead_source'];
      $human = $_POST['message_human'];
      $location = $_POST['location'];
      $body = " This message was sent from intY.com $location \n\nBusiness name: $business_name \nName: $first_name \nName: $last_name \nEmail: $email \nPhone: $phone \n\n Lead source: $lead_source \n\nMessage: \n\n$message";
    
      //php mailer variables
      $to ="[email protected]";
      $subject = "Someone sent a message from ".get_bloginfo('name');
      $headers = 'From: '. $email . "\r\n" .
        'Reply-To: ' . $email . "\r\n";
    
      if(!$human == 0){
        if($human != 2) my_contact_form_generate_response("error", $not_human); //not human!
        else {
    
          //validate email
          if(!filter_var($email, FILTER_VALIDATE_EMAIL))
            my_contact_form_generate_response("error", $email_invalid);
          else //email is valid
          {
            //validate presence of name and message
            if(empty($first_name) || empty($message)){
              my_contact_form_generate_response("error", $missing_content);
            }
            else //ready to go!
            {
              $sent = wp_mail($to, $subject, strip_tags($body), $headers);
              if($sent) my_contact_form_generate_response("success", $message_sent); //message sent!
              else my_contact_form_generate_response("error", $message_unsent); //message wasn't sent
            }
          }
        }
      }
      else if ($_POST['submitted']) my_contact_form_generate_response("error", $missing_content);
    
    ?>
                    <?php echo $response; ?>
                            <form class="marketing" action="<?php the_permalink(); ?>" method="post">
                                <legend>General enquiries</legend>
                                <fieldset>
                                    <label class="form-group required wow fadeIn">
                                        <span>Business name:</span>
                                        <input type="text" name="message_business_name" value="<?php echo esc_attr($_POST['message_business_name']); ?>" required />
                                    </label>
                                    <label class="form-group required wow fadeIn">
                                        <span>First name:</span>
                                        <input type="text" name="message_first_name" value="<?php echo esc_attr($_POST['message_first_name']); ?>" required />
                                    </label>
                                    <label class="form-group required wow fadeIn">
                                        <span>Last name:</span>
                                        <input type="text" name="message_last_name" value="<?php echo esc_attr($_POST['message_last_name']); ?>" required />
                                    </label>
                                    <label class="form-group required wow fadeIn">
                                        <span>Email:</span>
                                        <input type="text" name="message_email" value="<?php echo esc_attr($_POST['message_email']); ?>" required />
                                    </label>
                                    <label class="form-group required wow fadeIn">
                                        <span>Phone number:</span>
                                        <input type="text" name="message_phone" value="<?php echo esc_attr($_POST['message_phone']); ?>" required />
                                    </label>
                                    <label class="form-group required wow fadeIn">
                                        <span>Message:</span>
                                        <textarea type="text" name="message_text" required><?php echo esc_textarea($_POST['message_text']); ?></textarea>
                                    </label>
                                    <label class="form-group required wow fadeIn">
                                        <span>Where did you hear about us:</span>
                                        <select name="message_lead_source" id="id_lead_source">
                                            <option value=""> </option>
                                            <option <?php if ($_POST['message_lead_source'] == 'Advert') { ?>selected="true" <?php }; ?> value="Advert">Advert</option>
                                            <option <?php if ($_POST['message_lead_source'] == 'Cold Call') { ?>selected="true" <?php }; ?> value="Cold Call">Cold Call</option>
                                            <option <?php if ($_POST['message_lead_source'] == 'Direct Mail') { ?>selected="true" <?php }; ?> value="Direct Mail">Direct Mail</option>
                                            <option <?php if ($_POST['message_lead_source'] == 'Email Campaign') { ?>selected="true" <?php }; ?> value="Email Campaign">Email Campaign</option>
                                            <option <?php if ($_POST['message_lead_source'] == 'Partner Referral') { ?>selected="true" <?php }; ?> value="Partner Referral">Partner Referral</option>
                                            <option <?php if ($_POST['message_lead_source'] == 'PR') { ?>selected="true" <?php }; ?> value="PR">PR</option>
                                            <option <?php if ($_POST['message_lead_source'] == 'Seminar') { ?>selected="true" <?php }; ?> value="Seminar">Seminar</option>
                                            <option <?php if ($_POST['message_lead_source'] == 'Trade Show') { ?>selected="true" <?php }; ?> value="Trade Show">Trade Show</option>
                                            <option <?php if ($_POST['message_lead_source'] == 'Web') { ?>selected="true" <?php }; ?> value="Web">Web</option>
                                            <option <?php if ($_POST['message_lead_source'] == 'Webinar') { ?>selected="true" <?php }; ?> value="Webinar">Webinar</option>
                                            <option <?php if ($_POST['message_lead_source'] == 'Word of mouth') { ?>selected="true" <?php }; ?> value="Word of mouth">Word of mouth</option>
                                            <option <?php if ($_POST['message_lead_source'] == 'Other') { ?>selected="true" <?php }; ?> value="Other">Other</option>
                                        </select>
                                    </label>
                                    <label class="form-group required wow fadeIn">
                                        <span>Human verification:</span>
                                        <input type="text" style="width: 60px;" name="message_human" required value="<?php echo esc_attr($_POST['message_human']); ?>" title="Hint: The answer is 2" /> + 3 = 5
                                    </label>
                                    <label class="form-group required wow fadeIn">
                                        <button type="submit" class="button">Send enquiry</button>
                                        <input type="hidden" name="location" value="contact us page" />
                                        <input type="hidden" name="submitted" value="1" required />
                                    </label>
                                </fieldset>
                            </form>

    I tried pasting as code and as PHP but neither worked.

    https://www.ads-software.com/plugins/global-content-blocks/

  • The topic ‘Mixed PHP and HTML in REaw HTML field’ is closed to new replies.