Adding additional fields

I have this code that I have added two of my own inout fields to. I want to add another field just before the submit button but wherever I put it I am getting a Internal Server Error:

here is the code and the field i’m trying to add:

<input type="text" placeholder="'.__('Questions or comments: (required)', 'dt_themes').'" name="quescomms" required>';

I’d really appreciate it if you could help me realise what i’m doing wrong. Here is the full function with the added fields
Many thanks


function dt_sc_subscription_form( $atts, $content = null ) {
        
        extract(shortcode_atts(array(
            'image_url' => '',
            'slider' => '',
            'slider_id' => '',
            'title'  => __('Plan a Visit', 'dt_themse'),
            'submit_text' => __('Submit', 'dt_themes'),
            'success_msg' => __('Thanks for subscribing, we will contact you soon.', 'dt_themes'),
            'error_msg' => __('Mail not sent, please try again Later.', 'dt_themes'),
            'subject' => __('Subscription', 'dt_themes'),
            'admin_email' => get_bloginfo('admin_email'),
            'enable_planavisit' => 'true',
            'contact_label' => __('Inquiries', 'dt_themes'),
            'contact_number' => ''
        ), $atts));


        if($admin_email == '') $admin_email = get_bloginfo('admin_email');
            
        $out = '';
        
        $out .= '<div class="column dt-sc-two-third no-space">  
                    <div class="dt-sc-subscription-frm-image">';
                        
                        if($image_url != '') {                          
                            $out .= '<img src="'.$image_url.'" alt="'.$title.'" title="'.$title.'">';
                        } else if($slider != '' && $slider_id != '') {
                            if($slider == 'LayerSlider') {
                                $out .= do_shortcode("[layerslider id='{$slider_id}']");
                            } elseif($slider == 'RevolutionSlider') {
                                $out .= do_shortcode("[rev_slider $slider_id]");
                            }
                        }
                        
                    $out .= '</div>
                </div>
                <div class="column dt-sc-one-third no-space">
                
                    <div class="dt-sc-subscription-frm-container">
                        <h2> <i class="fa fa-clock-o"> </i> '.$title.' </h2>
                        <div class="dt-sc-clear"></div>
                        <form name="frmsubscription" action="'.get_template_directory_uri().'/framework/subscribe_mail.php" class="dt-sc-subscription-frm" method="post">
                                              <input type="text" placeholder="'.__('Full Name (required)', 'dt_themes').'" name="dtfullname" required>
                            <input type="email" placeholder="'.__('Email (required)', 'dt_themes').'" name="dtemail" required>
                            <input type="text" placeholder="'.__('Tel No (required)', 'dt_themes').'" name="dttelno" required>';
                            $course_args = array('post_type' => 'dt_courses', 'numberposts' => -1, 'orderby' => 'title', 'order' => 'DESC');
                            $course_array = get_posts( $course_args );
                            
                            $out .= '<select id="dtcourse" name="dtcourse" required>';
                            $out .= '<option value="">' . __( 'Choose class, date, time, location? ', 'dt_themes' ) . '</option>';
<input type="text" placeholder="'.__('Questions or comments: (required)', 'dt_themes').'" name="quescomms" required>';
                            if ( count( $course_array ) > 0 ) {
                                foreach ($course_array as $course_item){
                                    $out .= '<option value="' . esc_attr( $course_item->post_title ) . '" >' . esc_html( $course_item->post_title ) . '</option>';
                            
                                }
                            }
                            $out .= '</select>';
                    
                            if($enable_planavisit == 'true') {
                                $out .= '<div class="dt-sc-check-box">
                                            <input type="checkbox" id="dtplanavisit" name="dtplanavisit"> <label class="checkbox-label"> <span> </span>'.__('Plan a Visit', 'dt_themes').' </label>
                                        </div>
                                        <input type="text" style="display: none;" id="dtdatetimepicker" name="dtdatetimepicker" placeholder="'.__('Date Time', 'dt_themes').'" >';

                            }
                    
                        $out .= '<div id="ajax_subscribe_msg"></div>
                                <input type="submit" class="dt-sc-button" value="'.$submit_text.'" name="btnsubscribe" id="btnsubscribe">
                                <input type="hidden" value="'.$admin_email.'" name="hid_adminemail">
                                <input type="hidden" value="'.$subject.'" name="hid_subject">
                                <input type="hidden" value="'.$success_msg.'" name="hid_successmsg">
                                <input type="hidden" value="'.$error_msg.'" name="hid_errormsg">
                        </form>';
                        
                        if($contact_number != '') {
                            $out .= '<div class="dt-sc-subscription-enquiry"> <i class="fa fa-phone"> </i> <span> '.$contact_label.' </span> '.$contact_number.' </div>';
                        }
                        
                    $out .= '</div>                            
                
                </div>';
                    
        return $out;
                
    }

It needs to be wrapped in single quotes and come after the loop where you are populating your options in the select input, so after:

$out .= '</select>';

put:

$out .= '<input type="text" placeholder="'.__('Questions or comments: (required)', 'dt_themes').'" name="quescomms" required>';
1 Like

I seeee. So the html has to be preceeded with $out .=

Ok thanks because that works. Now the fields show but they do not show up in the email I receive. Only then name, email and the date and course shows in the email I receive.

I have subscribe_mail.php where I have added the dttelno and still it does not tell me what the user inputted in the email I recive. Similarly I’m not sure how to add the comments to it or whether the newsletter choice was ticked or not.

If it helps here is the form: http://www.womenoffaithwhothrive.com/registration
and here is the info I receive when someone registers:

xxx has been subscribed for Family Acupuncture \r\n\nYou can contact xxx via
email, xxx@hotmail.com.

[code]<?php

if(!$_POST) exit;

$to       = $_POST['hid_adminemail']; #Replace your email id...
$dtfullname      = $_POST['dtfullname'];
$dtemail    = $_POST['dtemail'];
    $dttelno    = $_POST['dttelno'];
$dtcourse    = $_POST['dtcourse'];
$subject  = $_POST['hid_subject'];
$dtdatetimepicker  = isset($_POST['dtdatetimepicker']) ? $_POST['dtdatetimepicker'] : '';

if($dtfullname == '' || $dtemail == '') :     
    echo "<span class='error-msg'>Please fill the required fields</span>";
    exit;
endif;

$e_subject = $subject;

$msg  = $dtfullname. ' has been subscribed for '.$dtcourse.'\r\n\n';
if($dtdatetimepicker != '')
    $msg  .= 'He/She planned to visit in person on '.$dtdatetimepicker.'\r\n\n';
$msg .= "You can contact $dtfullname via email, $dtemail.\r\n\n";
$msg .= "-------------------------------------------------------------------------------------------\r\n";
                        
if(@mail($to, $e_subject, $msg, "From: $dtemail\r\nReturn-Path: $dtemail\r\n"))
{
    echo "<span class='success-msg'>".$_POST['hid_successmsg']."</span>";
}
else
{
    echo "<span class='error-msg'>".$_POST['hid_errormsg']."</span>";
}

?>[/code]

Eeek! Is this script live? You open spam/all sorts of nasties with this script.

You need to filter you inputs before you use them.

Replace all references of $_POST with filter_input: http://php.net/manual/en/function.filter-input.php

e.g:

$to = filter_input(INPUT_POST, 'hid_adminemail')

Secondly to capture the new input you need to save the ‘quescomms’ input to a var and then add it to $msg before sending the email.

$quescomms = filter_input(INPUT_POST, 'quescomms');
$msg .= $quescomms
1 Like

and with filter functions it’s dead easy to validate email addresses as well.

$to = filter_input(INPUT_POST, 'hid_adminemail', FILTER_VALIDATE_EMAIL);
1 Like

@djsmithme hmm this is very interesting.

I have made the changes and added the suggestion from @Dormilich

but now I get this message when submitting:

’ Please fill the required fields ’

Although all fields have been filled in correctly.
Have I done something wrong or do I need to add something else?

Also how do I add a field for the newsletter checkbox to be added to the message?

here is the script I have now:

[code]<?php

if(!$_POST) exit;

$to = filter_input(INPUT_POST, 'hid_adminemail', FILTER_VALIDATE_EMAIL);
$dtfullname     = filter_input(INPUT_POST, 'dtfulname');
$dtemail    = filter_input(INPUT_POST, 'dtemail');
    $dttelno    = filter_input(INPUT_POST, 'dttelno');
    $dtcomms = filter_input(INPUT_POST, 'dtcomms');
$dtcourse    = filter_input(INPUT_POST, 'dtcourse');
$subject  = filter_input(INPUT_POST, 'hid_subject');
$dtdatetimepicker  = isset($_POST['dtdatetimepicker']) ? $_POST['dtdatetimepicker'] : '';

if($dtfullname == '' || $dtemail == '') :     
    echo "<span class='error-msg'>Please fill the required fields</span>";
    exit;
endif;

$e_subject = $subject;

$msg  = $dtfullname. " has been subscribed for $dtcourse.\r\n\n";
if($dtdatetimepicker != '')
    $msg  .= 'He/She planned to visit in person on '.$dtdatetimepicker.'\r\n\n';
$msg .= "You can contact $dtfullname via email, $dtemail.\r\n\n";
    $msg .= "Contact Telephone - $dttelno.\r\n\n";
$msg .= "-------------------------------------------------------------------------------------------\r\n";
    $msg .= $dtcomms;
                        
if(@mail($to, $e_subject, $msg, "From: $dtemail\r\nReturn-Path: $dtemail\r\n"))
{
    echo "<span class='success-msg'>".$_POST['hid_successmsg']."</span>";
}
else
{
    echo "<span class='error-msg'>".$_POST['hid_errormsg']."</span>";
}

?>[/code]

it looks like at least one of those fields is missing or contains an empty value.

filter_input() returns NULL on non-existing fields and false if the validation (if any) fails.

I’m a bit stuck there?

Do you mean i have missing fields in the if(!$_POST) exit; section? Is this for the ones I have there already or do you mean it is missing a field for the newsletter check option?

How can I add this?

!$_POST is never true. better check $_SERVER['REQUEST_METHOD'].

Check your script for typo’s ‘dtfullname’ is missing an ‘l’ hence why its coming back empty.

Oh yes I see.

Thank you @djsmithme and @Dormilich you have been a great source of knowledge.

My only other question is the checkbox. It show up in the form but doesn’t show up if ticked in the nessage. I’ve tried to add iff statements but nothing seems to work. What would you say?

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.