【问题标题】:Getting a form to send an selected option to email获取表单以将选定的选项发送到电子邮件
【发布时间】:2021-11-18 04:49:03
【问题描述】:

嗨,我已经尝试了 2 个小时来获得这个下拉菜单,以通过 post 方法将选定的结果发送到电子邮件,我是 php 新手,我想我可以通过一些指导来做 ill post html php js下面的代码。我尝试了很多方法,这不是我唯一的编辑,我确实尝试了所有我只需要一点帮助的方法

html

    <div class="col-lg-3">
<div class="form-group mt-2">
<div class="dropdown" >
    <select name="vehicles" id="vehicles" class="ddstyle">
    <option value="motor" selected>Motorcycle</option>
    <option value="Car">Car</option>
    <option value="Small">Small Van</option>
    <option value="Opel">Opel</option>
    <option value="Midi">Midi Van</option>
    <option value="SWB">SWB up to 2.4m</option>
    <option value="MWB3">MWB up to 3m</option>
    <option value="LWB4">LWB up to 4m</option>
    <option value="XLWB4">XLWB 4m+</option>
    <option value="Luton">Luton</option>
    <option value="T5">7.5T</option>
  </select>     
</div>
</div>                          
</div>

PHP

<?php

if(!$_POST) exit;

// Email address verification, do not edit.
function isEmail($email) {
    return(preg_match("/^[-_.[:alnum:]]+@((([[:alnum:]]|[[:alnum:]][[:alnum:]-]*[[:alnum:]])\.)+(ad|ae|aero|af|ag|ai|al|am|an|ao|aq|ar|arpa|as|at|au|aw|az|ba|bb|bd|be|bf|bg|bh|bi|biz|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|com|coop|cr|cs|cu|cv|cx|cy|cz|de|dj|dk|dm|do|dz|ec|edu|ee|eg|eh|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gh|gi|gl|gm|gn|gov|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|in|info|int|io|iq|ir|is|it|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|mg|mh|mil|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|museum|mv|mw|mx|my|mz|na|name|nc|ne|net|nf|ng|ni|nl|no|np|nr|nt|nu|nz|om|org|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|pro|ps|pt|pw|py|qa|re|ro|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|st|su|sv|sy|sz|tc|td|tf|tg|th|tj|tk|tm|tn|to|tp|tr|tt|tv|tw|tz|ua|ug|uk|um|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|yu|za|zm|zw)$|(([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5])\.){3}([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5]))$/i",$email));
}

if (!defined("PHP_EOL")) define("PHP_EOL", "\r\n");

$name      = $_POST['name'];
$email     = $_POST['email'];
$comments  = $_POST['comments'];
$dod       = $_POST['dod'];
$yourname  = $_POST['yourname'];
$tel       = $_POST['tel'];
$caddy     = $_POST['caddy'];
$cpost     = $_POST['cpost'];
$daddy     = $_POST['daddy'];
$dpost     = $_POST['dpost'];
$dofd      = $_POST['dofd'];
$Car       = $_POST['Car'];
$Small     = $_POST['Small'];
$Opel      = $_POST['Opel'];
$Midi      = $_POST['Midi'];
$MWB       = $_POST['MWB'];
$MWB3      = $_POST['MWB3'];
$LWB4      = $_POST['LWB4'];
$XLWB4     = $_POST['XLWB4'];
$Luton     = $_POST['Luton'];
$T5        = $_POST['T5'];
//*$vehicles  = $_POST['$vehicles'];**/

 if(isset($_POST['send'])){
    if(isset($_POST['vehicles'])){
        $value = $_POST['vehicles'];
        $ddme = $_POST['value'];
    }
 }
if(isset($_POST['vehicles']) && $_POST['vehicles']=='motor') echo $value;
    

if(trim($name) == '') {
    echo '<div class="error_msg">You must enter your company name.</div>';
    exit();
} else if(trim($email) == '') {
    echo '<div class="error_msg">Please enter a valid email address.</div>';
    exit();
} else if(!isEmail($email)) {
    echo '<div class="error_msg">You have entered an invalid e-mail address. Please try again.</div>';
    exit();

}

if(trim($dod) == '') {
    echo '<div class="error_msg">Please enter your collection date.</div>';
    exit();
}
if(trim($dofd) == '') {
    echo '<div class="error_msg">Please enter your drop off deadline.</div>';
    exit();
}
if(get_magic_quotes_gpc()) {
    $comments = stripslashes($comments);
}

   
// Configuration option.
// Enter the email address that you want to emails to be sent to.
// Example $address = "joe.doe@yourdomain.com";

//$address = "example@example.net";
$address = "admin@ready2gologistics.co.uk";


// Configuration option.
// i.e. The standard subject will appear as, "You've been contacted by John Doe."

// Example, $e_subject = '$name . ' has contacted you via Your Website.';

$e_subject = '  ' . $name . ' has a possible job!';


// Configuration option.
// You can change this if you feel that you need to.
// Developers, you may wish to add more fields to the form, in which case you must be sure to add them here.

$e_body = "$name wants a quote details are listed below." . PHP_EOL . PHP_EOL;
$e_content = 
"
Company: $name
Name of Person: $yourname
Contacts email: $email
Contacts Telephone Number: $tel

Desired Collection Date: $dod
Drop Off Deadline: $dofd

Collection Address: $caddy
Collection Post Code: $cpost

Destination Address: $daddy
Destination Post Code: $dpost 

Addtional Information: $comments

test: $value
" . PHP_EOL . PHP_EOL;
$e_reply = "You can contact $name via email, $email";

$msg = wordwrap( $e_body . $e_content . $e_reply, 70 );

$headers = "From: $email" . PHP_EOL;
$headers .= "Reply-To: $email" . PHP_EOL;
$headers .= "MIME-Version: 1.0" . PHP_EOL;
$headers .= "Content-type: text/plain; charset=utf-8" . PHP_EOL;
$headers .= "Content-Transfer-Encoding: quoted-printable" . PHP_EOL;

if(mail($address, $e_subject, $msg, $headers)) {

    // Email has sent successfully, echo a success page.

    echo "<fieldset>";
    echo "<div id='success_msg'>";
    echo "<h3>Email Sent Successfully.</h3>";
    echo "<p>Thank you <strong>$name</strong>, your quote has been submitted to us. To see how we use your information check out our <a href='PrivacyPolicy.html".$link_address."'>Privacy Policy</a></p>";
    echo "</div>";
    echo "</fieldset>";

} else {

    echo 'ERROR!';

}
    ?>

JavaScript

//Contact
$('#working_form').submit(function() {

    var action = $(this).attr('action');

    $("#message").slideUp(750, function() {
        $('#message').hide();

        $('#submit')
            .before('<img src="" class="gif_loader" />')
            .attr('disabled', 'disabled');

        $.post(action, {
                name: $('#name').val(),
                email: $('#email').val(),
                comments: $('#comments').val(),
                dod: $('#dod').val(),
                dofd: $('#dofd').val(),
                yourname: $('#yourname').val(),
                cpost: $('#cpost').val(),
                caddy: $('#caddy').val(),
                tel: $('#tel').val(),
                daddy: $('#daddy').val(),
                dpost: $('#dpost').val(),
                vehicles: $('#vehicles').val(),
                value: $('#value').val(),
            
            },
            function(data) {
                document.getElementById('message').innerHTML = data;
                $('#message').slideDown('slow');
                $('#cform img.gif_loader').fadeOut('slow', function() {
                    $(this).remove()
                });
                $('#submit').removeAttr('disabled');
                if (data.match('success') != null) $('#cform').slideUp('slow');
            }
        );

    });

    return false;

});

【问题讨论】:

  • 你没有在$.post调用中传递send,所以isset($_POST['send']永远不会满足
  • 请你告诉我你的意思我不明白

标签: javascript php html post


【解决方案1】:

$.post请求中传递send

$.post(action, {
    ...other data
    value: $('#value').val(),
    send: $('#send').val(),
}

因为您的$value 车辆数据取决于if(isset($_POST['send'])) 检查

if(isset($_POST['send'])){
    if(isset($_POST['vehicles'])){
        $value = $_POST['vehicles'];
        $ddme = $_POST['value'];
    }
 }

【讨论】:

  • 好吧,伙计们,我在 js 车辆中声明了 var:$('#vehicles').val(),值:$('#value').val(),发送:$(' #send').val(),这是我的 php if(isset($_POST['vehicles']) && $_POST['vehicles']=='motor') echo $value; if(isset($_POST['send'])){ if(isset($_POST['vehicles'])){ $value = $_POST['vehicles']; $ddme = $_POST['value']; } } 那么我希望选项值在我调用 $value 的电子邮件中显示的位置是正确的
  • 只有当$vehicle 的值等于motor 时,您的$value 才会有一些内容,在电子邮件模板中您将其设置为test: $value,看起来还可以
猜你喜欢
  • 2018-02-18
  • 2016-07-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-07-02
  • 2014-10-26
  • 2021-07-31
  • 1970-01-01
相关资源
最近更新 更多