douzhuo1858 2014-09-10 18:01
浏览 34
已采纳

将Query String变量传递给联系表单电子邮件

I have an application form on my page. Everything is working correctly except for the query string. I want to take a url with a variable, and pass it into the contact form email. Ex. http://www.example.net?merchantid=12345

I want the merchant id variable name and value to display on the email I receive, along with the rest of the user information. The way it sits now breaks my page. It would be great to send a CC email without this merchant id as well.

Here is my markup:

<form id="shortApplication" action="{site_url}application#contact-sent" method="POST" enctype="multipart/form-data" data-type="advanced">
    <input type="hidden" value="true" name="emailSent" id="emailSent">
                            <input type="hidden" name="XID" value="{XID_HASH}" />                               
                            <input type="hidden" name="merchantid" value='<?php echo $merchantid; ?>' />                                
                            <div class="row">
                                <div class="form-group">
                                    <div class="col-md-6">
                                        <label>Your First Name *</label>
                                        <input type="text" value="" required data-msg-required="Please enter your first name." maxlength="100" class="form-control" name="first_name" id="first_name">
                                    </div>
                                    <div class="col-md-6">
                                        <label>Your Last Name *</label>
                                        <input type="text" value="" required data-msg-required="Please enter your last name." maxlength="100" class="form-control" name="last_name" id="last_name">
                                    </div>                                      
                                </div>
                            </div>  
                            <div class="row">
                                <div class="form-group">
                                    <div class="col-md-6">
                                        <label>Your Business Name *</label>
                                        <input type="text" value="" required data-msg-required="Please enter your business name." maxlength="100" class="form-control" name="business_name" id="business_name">
                                    </div>
                                    <div class="col-md-6">
                                        <label>Your Phone Number *</label>
                                        <input type="text" value="" required data-msg-required="Please enter your phone number." maxlength="100" class="form-control" name="phone" id="phone">
                                    </div>                                      
                                </div>
                            </div>
                            <div class="row">
                                <div class="form-group">
                                    <div class="col-md-6">
                                        <label>Your email address *</label>
                                        <input type="email" value="" required data-msg-required="Please enter your email address." data-msg-email="Please enter a valid email address." maxlength="100" class="form-control" name="email" id="email">
                                    </div>
                                    <div class="col-md-6">
                                        <label>Confirm Your email address *</label>
                                        <input type="email" value="" required data-msg-required="Emails must match." data-msg-email="Please enter a valid email address." maxlength="100" class="form-control required email" equalTo='#email' name="emailConfirm" id="emailConfirm">
                                    </div>
                                </div>
                            </div>                                  
                            <div class="row">
                                <div class="checkbox">
                                    <div class="form-group">
                                        <div class="col-md-6">
                                            <label>
                                                <input type="checkbox" id="checked"> I agree to the <a href="{site_url}terms-and-conditions" target="_blank">terms & conditions</a>
                                            </label>
                                        </div>                                      
                                    </div>
                                </div>
                            </div>
                            <div class="row">
                                <div class="col-md-12">
                                    <label>Human Verification *</label>
                                </div>
                            </div>
                            <div class="row">
                                <div class="form-group">
                                    <div class="col-md-4">
                                        <div class="captcha form-control">
                                            <div class="captcha-image">
                                                <?php
                                                $_SESSION['captcha'] = simple_php_captcha(array(
                                                    'min_length' => 6,
                                                    'max_length' => 6,
                                                    'min_font_size' => 22,
                                                    'max_font_size' => 22,
                                                    'angle_max' => 3
                                                ));

                                                $_SESSION['captchaCode'] = $_SESSION['captcha']['code'];

                                                echo '<img src="' . "php/simple-php-captcha/simple-php-captcha.php/" . $_SESSION['captcha']['image_src'] . '" alt="CAPTCHA code">';
                                                ?>
                                            </div>
                                        </div>
                                    </div>
                                    <div class="col-md-8">
                                        <input type="text" value="" maxlength="6" data-msg-captcha="Wrong verification code." data-msg-required="Please enter the verification code." placeholder="Type the verification code." class="form-control" name="captcha" id="captcha">
                                    </div>
                                </div>
                            </div>
                            <div class="row">
                                <div class="col-md-12">
                                    <hr>
                                </div>
                            </div>
                            <div class="row">
                                <div class="col-md-12">
                                    <input type="submit" id="shortApplicationSubmit" value="Send Message" class="btn btn-primary btn-lg pull-right" data-loading-text="Loading..." disabled="disabled">
                                </div>
                            </div>
                        </form>

Here is my PHP:

<?php
    session_start();

include("php/simple-php-captcha/simple-php-captcha.php");
include("php/php-mailer/class.phpmailer.php");


//  Step 1 - Enter your email address below.
$to = 'info@novawebdev.com';

$arrResult = array('response'=>'');

if(isset($_POST['emailSent'])) {

$subject = 'RTO Camera Application';

// Step 2 - If you don't want a "captcha" verification, remove that IF.
if (strtolower($_POST["captcha"]) == strtolower($_SESSION['captcha']['code'])) {

    $first_name = $_POST['first_name'];
    $last_name = $_POST['last_name'];
    $business_name = $_POST['business_name'];
    $phone = $_POST['phone'];
    $email = $_POST['email'];
    $merchantid = $_GET['merchantid'];
    // $emailConfirm = $_POST['emailConfirm'];

    // Step 3 - Configure the fields list that you want to receive on the email.
    $fields = array(
        0 => array(
            'text' => 'First Name',
            'val' => $_POST['first_name']
        ),
        1 => array(
            'text' => 'Last Name',
            'val' => $_POST['last_name']
        ),
        2 => array(
            'text' => 'Business Name',
            'val' => $_POST['business_name']
        ),
        3 => array(
            'text' => 'Phone Number',
            'val' => $_POST['phone']
        ),
        4 => array(
            'text' => 'Email',
            'val' => $_POST['email']
        ),
        5 => array(
            'text' => 'Merchant ID',
            'val' => $_POST['merchantid']
        )
    );

    $message = "";

    foreach($fields as $field) {
        $message .= $field['text'].": " . htmlspecialchars($field['val'], ENT_QUOTES) . "<br>
";
    }

    $mail = new PHPMailer;

    $mail->IsSMTP();

    // Step 4 - If you don't receive the email, try to configure the parameters below:

    //$mail->Host = 'mail.yourserver.com';                // Specify main and backup server
    //$mail->SMTPAuth = true;                             // Enable SMTP authentication
    //$mail->Username = 'username';                       // SMTP username
    //$mail->Password = 'secret';                         // SMTP password
    //$mail->SMTPSecure = 'tls';                          // Enable encryption, 'ssl' also accepted

    $mail->From = $email;
    $mail->FromName = $_POST['first_name'].' '.$_POST['last_name'];
    $mail->AddAddress($to);
    $mail->AddReplyTo($email, $first_name, $last_name);
    $mail->AddCC($email);
    $mail->IsHTML(true);

    $mail->CharSet = 'UTF-8';

    $mail->Subject = $subject;
    $mail->Body    = $message;  

    if($mail->Send()) {
       $arrResult['response'] = 'success';
    } else {
        $arrResult['response'] = 'error';
    }

} else {

    $arrResult['response'] = 'captchaError';

}
}
?>

I've been researching this for a few hours, and this is how far I've come. I've never worked with PHP outside a basic copy paste for a contact form.

Thanks!

  • 写回答

1条回答 默认 最新

  • dttwois6098 2014-09-10 18:08
    关注

    You're nearly there. In your

    <input type="hidden" name="merchantid" value='<?php echo $merchantid; ?>' />
    

    make sure $merchantid is set with the get parameter:

    <input type="hidden" name="merchantid" value="<?php if ( isset( $_GET['merchantid'] ) ) echo $_GET['merchantid']; ?>" />
    

    and then it'll be available as

    $merchantid = $_POST['merchantid'];
    

    in your processing function.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化