0

我尝试了很多次,访问了很多论坛,但找不到答案。请不要发布它与其他帖子相似。试着帮我解决这个问题。

HTML 表单:

<form action="" method="post" id="bookingForm">
<div id="radiobutton">
<input type="radio" name="trip" id="oneway" value="oneway" checked/>&nbsp;Oneway
<input type="radio" name="trip" id="return" value="return"/>&nbsp;Return
</div>
<br/>

From:<input id="boarding" type="text" maxlength="40" class="user_input" size="18" name="boarding" value="Hyderabad"/>
To:<input id="landing" type="text" maxlength="40" class="user_input" size="18" name="landing"/>
<br/><br/><br/>

Onward:<input id="datepick"  name="onward" class="user_input" size="15" />

Return: <input id="datepick2"  name="return" class="user_input" size="15" />
<br/><br/><br/>
Your Name:<input id="pax_name" type="text" name="pax_name"  class="user_input" size="25" maxlength="50"/>
<br/><br/>
Contact number:<input id="pax_contact" type="text" maxlength="14" name="pax_contact" class="user_input" size="25"/>
<br/><br/>
Email:<input id="pax_email" name="pax_email" class="user_input" size="25"/>
<br/><br/>
<center>

<input id="submit" type="button" value="Request" />

用于发送电子邮件的 process.php:

<?php

  $trip=$_POST['trip'];
  $boarding=$_POST['boarding'];
  $landing=$_POST['landing'];
  $onward=$_POST['onward'];
  $return=$_POST['return'];
  $pax_name=$_POST['pax_name'];
  $pax_contact=$_POST['pax_contact'];
  $pax_email=$_POST['pax_email'];

        $to='anvesh@bsbtravels.com,anvifriend@gmail.com';
        $subject='Booking Form Query';
        $body=
              'Name: '.$pax_name."\n".
              'Contact Number: '.$pax_contact."\n".
              'Trip: '.$trip."\n".
              'From: '.$boarding."\n".
              'To: '.$landing."\n".
              'Onward Date: '.$onward."\n".
              'Return Date: '.$return."\n";
        $headers='From: '.$pax_name.'<'.$pax_email.'>';
        mail($to,$subject, $body, $headers)

?>

jQuery代码:

$(document).ready(function(){
$('#submit').click(function(){
          var boarding=$('#boarding').val();
          var landing=$('#landing').val();
          var datepick=$('#datepick').val();
          var datepick2=$('#datepick2').val();
          var pax_name=$('#pax_name').val();
          var pax_contact=$('#pax_contact').val();
          var pax_email=$('#pax_email').val();


    if($('#landing').val().length<3 || $('#boarding').val().length<3 || $('#datepick').val().length<3 || $('#pax_name').val().length<3 || $('#pax_contact').val().length<8 || $('#pax_email').val().length<3){

        $('#bookfail').attr('title','Sending Failed!').text('Please Enter valid information. All fields are required').dialog({buttons:{'Ok':function(){
            $(this).dialog('close');
            }},closeOnEscape:true,draggable:false,resizable:false,modal:true});

    }else{
                var VarData='boarding='+boarding+'&landing='+landing+'&datepick='+datepick+'&datepick2='+datepick2+'&pax_name='+pax_name+'&pax_contact='+pax_contact+'&pax_email='+pax_email;
        $.ajax({
           type:'POST',
           url:'process.php',
                   data:VarData,
           success:function(){
           $('#booksuccess').attr('title','Request sent successfully').text('Your request has been sent. We will be in touch soon').dialog({buttons:{'Ok':function(){   $(this).dialog('close');
            }},closeOnEscape:true,draggable:false,resizable:false,modal:true});
           }
           error:function(){
                alert('Oops request sending failed. Please try again');
           }
        });
        }
    });
});
4

2 回答 2

1

success回拨后您缺少逗号:

success:function(){
   ...
}
error:function(){
   ...
}

必须

success:function(){
   ...
},// <--
error:function(){
   ...
}
于 2013-02-04T09:55:06.293 回答
0

您正在以传统的查询字符串格式发布您的值...将您的值放入 javascript 对象中并通过stringifying 将此对象发送到服务器...

有两种方法可以做到这一点

1]修改你的javascript文件有点像下面

把你所有的价值观都放在一个javascript对象中......就像这样

var VarData= {
    "boarding" : boarding,
    "landing" : landing,
    "datepick": datepick,
    "datepick2": datepick2,
    "pax_name": pax_name,
    "pax_contact": pax_contact,
    "pax_email": pax_email
}

并将您的通话data:varData线路更改为$.ajax()

data: JSON.stringify(varData)

完成此操作后,您的PHP代码将能够捕获$_POST数组中的值...

现在您正在构建查询字符串,并且您的$_POST数组中没有索引,就像boarding所有..

第二种方式...保持Javascript原样并修改您的PHP代码。喜欢以下

捕获从 javascript 代码发送的数据。并使用explode函数来打破字符串&

$temp = explode("&",$_POST['data']);

$temp将是一个数组,因此您需要使用循环遍历它。然后将每个元素分解$temp=符号

$t=explode("=",$temp[$i]);

$t将具有您正在寻找的价值,您可以使用它来发送邮件。

于 2013-02-04T09:46:26.533 回答