douni1396 2015-05-14 12:49
浏览 17
已采纳

如何将下拉值转换为.php文件

I need to get selected dropdown value into my .php file, which is actually send mail file.

When i select one of 6 options from dropdown, i want selected option to be included into the mail as the "subject" of the email.

Here is my HTML code

<span class="custom-dropdown big">
    <select id="mounth" name="mounth">
        <option value="default">-- Domain name --</option>
        <option value="value1" rel="icon-temperature">value1</option>
        <option value="value2">value2</option>
        <option value="value3">value3</option>
        <option value="value4">value4</option>
        <option value="value5">value5</option>
        <option value="value6">value6</option>
    </select>
    <input type="hidden" id="changeme" name="changeme" />
</span>

POPUP EMAIL FORM

<div class="modal fade" id="popup-moda">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span id="spanhide" aria-hidden="true">&times;</span></button>
                <h4 class="modal-title">Contact Us</h4>
            </div>
            <form action="" method="post" name="form" id="forms">
                <div class="modal-body">
                    <div id="domain" name="domain"></div>
                    <input required id="name" name="name" placeholder="Name" type="text">
                    <input required id="email" name="email" placeholder="Email" type="text">
                    <textarea id="msg" name="msg" placeholder="Message"></textarea>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                    <button type="submit" class="btn btn-primary">Send</button>
                    <div id="success" style="color: blue;"></div>
                </div>
            </form>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div><!-- /.modal -->

Here is my JS code which actually displays selected option, but only as text before sending email.

$(document).ready(function(){

$('.popup').click(function(e){
    e.preventDefault();
    $('#domain').html('<b>' + $('#mounth').val() + '</b>');
    $('#popup-moda').modal('show');
        return false;
});
});

My PHP file

<?php
$name = $_POST['name'];
$email = $_POST['email'];
$msg = $_POST['msg'];

$to = 'example@example.com';
$subject = 'New Customer Enquiry';
$msg = <<<EMAIL
Subject: $subject
Message: $msg
From: $name
Email: $email
EMAIL;
$headers = 'From:' . $email;

if (filter_var($email, FILTER_VALIDATE_EMAIL)) { 
mail($to, $subject, $msg, $headers);   mail.

echo "Thank you for your email! We will reply as soon as possible."; 
}
else
{
echo "Invalid Email, please provide a correct email address.";
}

?>

Thank you guys. Any input is appreciated!

  • 写回答

1条回答 默认 最新

  • duangonglian6028 2015-05-14 13:00
    关注

    Put the select inside your form so the selected value is sent to server along with the rest of form parameters.

    <form action="" method="post" name="form" id="forms">
        <!-- ... -->
        <select id="mounth" name="mounth">
            <!-- options -->
        </select>
        <!-- ... -->
    </form>
    

    Then in your php file:

    $subject = $_POST['mounth'];
    

    And then use it in the subject.

    EDIT:

    Then you can add a hidden input to your form with that name and add the dropdown's value to it upon selection:

    <form action="" method="post" name="form" id="forms">
        <!-- form stuff -->
        <input type="hidden" id="hiddenMounth" name="mounth" value="" />
    </form>
    

    And then listen to select's onchange:

    $('#mounth').on('change', function() {
        $('#hiddenMounth').val($(this).val());
    });
    

    And then in your php:

    $subject = $_POST['mounth'];
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100