bootstrap输入字段和下拉按钮提交邮件表单

时间:2018-01-01 13:03:24

标签: php html twitter-bootstrap forms

我有以下问题。我有一个表格,我可以从三个选项中选择。问题是它不发送选定的选项,而其他部分名称,姓氏等发送。下面我发送代码: 提前谢谢你

<div class="col-sm-4"> 
                  <div class="dropdown"> 
                    <button class="btn btn-dropdown dropdown-toggle" type="button" id="service-type" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true"> 
                      <span class="drp-name" data-bind="label">Choose</span> 
                      <span class="caret"></span> 
                    </button> 
                    <ul class="dropdown-menu" aria-labelledby="service-type"> 
                   <li><a href="#">x1</a></li> 
                      <li><a href="#">x2</a></li> 
                      <li><a href="#">x3</a></li>                      
                    </ul> 
                  </div> 
                </div> 

php代码:

<?php 
$to = 'xxx@xx.com'; 
$subject  = "Appointment Form"; 
$servicetype = stripslashes($_REQUEST['service-type']); 
$msg = ""; 
$msg .= "Service Type: ".$servicetype."\r\n\n";  // add sender's sources to the message 
$mail = @mail($to, $subject, $msg, "From:".$email); 

if($mail) { 
   header("Location:index.html");    
} else { 
   echo 'Message could not be sent!'; 
} 

?> 

我还添加了JS,但它仍然无效

Dropdown Select
    $( document.body ).on( 'click', '.dropdown-menu li', function( event ) {
      var $target = $( event.currentTarget );
      $target.closest( '.dropdown' )
         .find( '[data-bind="label"]' ).text( $target.text() )
            .end()
         .children( '.dropdown-toggle' ).dropdown( 'toggle' );
      return false;
   });

3 个答案:

答案 0 :(得分:1)

HTML:

<div class="col-sm-4">
    <div class="dropdown">
        <button class="btn btn-dropdown dropdown-toggle" type="button" id="service-type" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true">
            <span class="drp-name" data-bind="label">Rodzaj usługi</span>
            <span class="caret"></span>
        </button>
        <ul class="dropdown-menu" aria-labelledby="service-type">
            <li><a href="#">x1</a></li>
            <li><a href="#">x2</a></li>
            <li><a href="#">x3</a></li>
        </ul>
    </div>
    <input type='hidden' name='service-type'> <!-- ADD THIS -->
</div>

JavaScript的:

$(document.body).on('click', '.dropdown-menu li', function(event) {
    event.preventDefault();

    var $target = $(event.currentTarget);

    $target.parents('.dropdown').sibling('[name=service-type]').val($target.text());

});

答案 1 :(得分:0)

你不能将值传递给没有名称属性和表单标记的php。您可以使用select代码

直接设置它
<form action="file.php" method=POST>
    <select name=option>
 <option value=0 selected>0</option>
 <option value=1> 1</option>
</select><input type=button value=submit>
</form>

答案 2 :(得分:0)

假设表单的名称为mail_form,并且有一个隐藏的表单元素来保存标识为service-type-holder且名称为service-type的选择信息。然后,以下代码可以与内联Javascript一起使用:

<form  name="mail_form" id="mail_form" action="" method="post">
<input type="hidden" name="service-type" id="service-type-holder">
<div class="col-sm-4"> 
        <div class="dropdown"> 
        <button class="btn btn-dropdown dropdown-toggle" type="button" id="service-type" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true"> 
            <span class="drp-name" data-bind="label">Choose</span> 
            <span class="caret"></span> 
        </button> 
        <ul class="dropdown-menu" aria-labelledby="service-type"> 
            <li onclick="$('#service-type-holder').val('x1'); $('#mail_form').submit()"><a href="#">x1</a></li> 
            <li onclick="$('#service-type-holder').val('x2'); $('#mail_form').submit()"><a href="#">x2</a></li> 
            <li onclick="$('#service-type-holder').val('x3'); $('#mail_form').submit()"><a href="#">x3</a></li>                      
        </ul> 
        </div> 
</div>