从下拉框中选择值(未显示)并发送到电子邮件。

huangapple go评论67阅读模式
英文:

Jquery Select value from a dropdown box(NOT SHOWING) on the email

问题

我有一个下拉表单,我在PHP中使用for循环生成,我试图获取所选的日期(作为值)通过电子邮件发送。

在首页上我基本上有如下所示。这不是完整的代码,因为我的循环更复杂,但我不认为问题出在那里。

在我的custom.js文件中,我有以下内容。问题应该出在下面代码中的预订日期选择上。我对jQuery不太熟悉,可能是个愚蠢的问题。

我能正确获取所有输入。我的问题出在选择下拉框上。

我做错了什么?

我的最后一个文件是send.php,只是发送电子邮件。那里也没问题,因为所有字段都被正确发送,除了预订日期。

作为输入单选框,在我之前的代码中它可以工作。但是在用PHP动态创建的下拉框中就不行了。

send.php发送了所有的信息,除了预订日期,以下是send.php的输出/send.php?firstname=Demetris&lastname=Demetriou&amount=2&mobilephone=99999&email=email%40gmail.com&message=somenotes

在代码片段中,它显示了我的PHP foreach和for循环的输出。这是用PHP完成的。那里没有问题。

我不确定或者说这部分代码不起作用var reservationDate = $contactform.find('select[name="add_r_date"]:selected').val();

$('#send').live("click", function() {
  var url = 'send.php';
  var error = 0;
  var $contactpage = $(this).closest('.ui-page');
  var $contactform = $(this).closest('.contact-form');
  $('.required', $contactform).each(function(i) {
    if ($(this).val() === '') {
      error++;
    }
  });
  if (error > 0) {
    alert('请填写所有必填字段。必填字段用星号 * 标记。');
  } else {
    var firstname = $contactform.find('input[name="add_name"]').val();
    var lastname = $contactform.find('input[name="add_surname"]').val();
    var amount = $contactform.find('input[name="add_seats"]:checked').val();
    var reservationDate = $contactform.find('select[name="add_r_date"]:selected').val();
    var mobilephone = $contactform.find('input[name="add_phone"]').val();
    var email = $contactform.find('input[name="email"]').val();
    var message = $contactform.find('input[name="add_note"]').val();

    $.ajax({
      type: "GET",
      url: url,
      data: {
        firstname: firstname,
        lastname: lastname,
        amount: amount,
        reservationDate: reservationDate,
        mobilephone: mobilephone,
        email: email,
        message: message
      },
      success: function(data) {
        if (data == 'success') {
          console.log(data);
          window.location = 'thankyou.php';
        } else {
          alert('无法发送您的消息。请重试。');
        }
      }
    });
  }
  return false;
});
<form action="" id="add-form" class="contact-form">
  <label class="col-lg-12 control-label"><strong>日期:</strong></label>
  <select name="add_r_date" id="add_r_date">
    <!-- 这里是你的日期选项 -->
  </select>
</form>
英文:

I have a dropdown form that i generate with a for loop in php and i am trying to get the selected value which is a date to be send by email

On the index page i have basically like below. It's not the full code because my loop is more complex but i don't believe the problem is there.

on my custom.js file i have as below. The issue must be on the reservation date select on the below code. I am not that good with jquery and it must be something silly probably.

All inputs i get them correctly. My issue is with the select dropdown box.

What i am doing wrong?

my last file its the send.php which just sends the email. Nothing wrong there as well as all fields are sent correctly besides the reservation date.

As an input radio box which i had the code before the date works. But inside a dynamically created with php select dropdown box it doesn't.

The send.php sends all the info besides Reservation date here the output of send.php /send.php?firstname=Demetris&lastname=Demetriou&amount=2&mobilephone=99999&email=email%40gmail.com&message=somenotes

In the snippet it shows the output of the my php foreach and for loops. Its done in php. No issues there.

I am not sure or its not working this part of the code var reservationDate = $contactform.find('select[name="add_r_date"]:selected').val();

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

$(&#39;#send&#39;).live(&quot;click&quot;, function() {
var url = &#39;send.php&#39;;
var error = 0;
var $contactpage = $(this).closest(&#39;.ui-page&#39;);
var $contactform = $(this).closest(&#39;.contact-form&#39;);
$(&#39;.required&#39;, $contactform).each(function(i) {
if ($(this).val() === &#39;&#39;) {
error++;
}
});
// each
if (error &gt; 0) {
alert(&#39;Please fill in all the mandatory fields. Mandatory fields are marked with an asterisk *.&#39;);
} else {
var firstname = $contactform.find(&#39;input[name=&quot;add_name&quot;]&#39;).val();
var lastname = $contactform.find(&#39;input[name=&quot;add_surname&quot;]&#39;).val();
var amount = $contactform.find(&#39;input[name=&quot;add_seats&quot;]:checked&#39;).val();
var reservationDate = $contactform.find(&#39;select[name=&quot;add_r_date&quot;]:selected&#39;).val();
var mobilephone = $contactform.find(&#39;input[name=&quot;add_phone&quot;]&#39;).val();
var email = $contactform.find(&#39;input[name=&quot;email&quot;]&#39;).val();
var message = $contactform.find(&#39;input[name=&quot;add_note&quot;]&#39;).val();
//submit the form.send.php takes from below.
$.ajax({
type: &quot;GET&quot;,
url: url,
data: {
firstname: firstname,
lastname: lastname,
//	state : state,
amount: amount,
reservationDate: reservationDate,
mobilephone: mobilephone,
email: email,
message: message
},
success: function(data) {
if (data == &#39;success&#39;) {
console.log(data);
// show thank you
window.location = &#39;thankyou.php&#39;
//$contactpage.find(&#39;.contact-thankyou&#39;).show();
//$contactpage.find(&#39;.contact-form&#39;).hide();
} else {
alert(&#39;Unable to send your message. Please try again.&#39;);
}
}
});
//$.ajax
}
return false;
});

<!-- language: lang-html -->

&lt;form action=&quot;&quot; id=&quot;add-form&quot; class=&quot;contact-form&quot;&gt;
&lt;label class=&quot;col-lg-12 control-label&quot;&gt;&lt;strong&gt;Dates:&lt;/strong&gt;&lt;/label&gt;
&lt;select name=&quot;add_r_date&quot; id=&quot;add_r_date&quot;&gt;Friday,31 Mar
&lt;option value=&quot;2023-03-31&quot;&gt;Friday,31 Mar&lt;/option&gt;Saturday,01 Apr
&lt;option value=&quot;2023-04-01&quot;&gt;Saturday,01 Apr&lt;/option&gt;Sunday,02 Apr
&lt;option value=&quot;2023-04-02&quot;&gt;Sunday,02 Apr&lt;/option&gt;Monday,03 Apr
&lt;option value=&quot;2023-04-03&quot;&gt;Monday,03 Apr&lt;/option&gt;Tuesday,04 Apr
&lt;option value=&quot;2023-04-04&quot;&gt;Tuesday,04 Apr&lt;/option&gt;Wednesday,05 Apr
&lt;option value=&quot;2023-04-05&quot;&gt;Wednesday,05 Apr&lt;/option&gt;Thursday,06 Apr
&lt;option value=&quot;2023-04-06&quot;&gt;Thursday,06 Apr&lt;/option&gt;Friday,07 Apr
&lt;option value=&quot;2023-04-07&quot;&gt;Friday,07 Apr&lt;/option&gt;Saturday,08 Apr
&lt;option value=&quot;2023-04-08&quot;&gt;Saturday,08 Apr&lt;/option&gt;Sunday,09 Apr
&lt;option value=&quot;2023-04-09&quot;&gt;Sunday,09 Apr&lt;/option&gt;Monday,10 Apr
&lt;option value=&quot;2023-04-10&quot;&gt;Monday,10 Apr&lt;/option&gt;Tuesday,11 Apr
&lt;option value=&quot;2023-04-11&quot;&gt;Tuesday,11 Apr&lt;/option&gt;Wednesday,12 Apr
&lt;option value=&quot;2023-04-12&quot;&gt;Wednesday,12 Apr&lt;/option&gt;Thursday,13 Apr
&lt;option value=&quot;2023-04-13&quot;&gt;Thursday,13 Apr&lt;/option&gt;Friday,14 Apr
&lt;option value=&quot;2023-04-14&quot;&gt;Friday,14 Apr&lt;/option&gt;Saturday,15 Apr
&lt;option value=&quot;2023-04-15&quot;&gt;Saturday,15 Apr&lt;/option&gt;Sunday,16 Apr
&lt;option value=&quot;2023-04-16&quot;&gt;Sunday,16 Apr&lt;/option&gt;Monday,17 Apr
&lt;option value=&quot;2023-04-17&quot;&gt;Monday,17 Apr&lt;/option&gt;Tuesday,18 Apr
&lt;option value=&quot;2023-04-18&quot;&gt;Tuesday,18 Apr&lt;/option&gt;Wednesday,19 Apr
&lt;option value=&quot;2023-04-19&quot;&gt;Wednesday,19 Apr&lt;/option&gt;Thursday,20 Apr
&lt;option value=&quot;2023-04-20&quot;&gt;Thursday,20 Apr&lt;/option&gt;Friday,21 Apr
&lt;option value=&quot;2023-04-21&quot;&gt;Friday,21 Apr&lt;/option&gt;Saturday,22 Apr
&lt;option value=&quot;2023-04-22&quot;&gt;Saturday,22 Apr&lt;/option&gt;Sunday,23 Apr
&lt;option value=&quot;2023-04-23&quot;&gt;Sunday,23 Apr&lt;/option&gt;Monday,24 Apr
&lt;option value=&quot;2023-04-24&quot;&gt;Monday,24 Apr&lt;/option&gt;Tuesday,25 Apr
&lt;option value=&quot;2023-04-25&quot;&gt;Tuesday,25 Apr&lt;/option&gt;Wednesday,26 Apr
&lt;option value=&quot;2023-04-26&quot;&gt;Wednesday,26 Apr&lt;/option&gt;Thursday,27 Apr
&lt;option value=&quot;2023-04-27&quot;&gt;Thursday,27 Apr&lt;/option&gt;Friday,28 Apr
&lt;option value=&quot;2023-04-28&quot;&gt;Friday,28 Apr&lt;/option&gt;Saturday,29 Apr
&lt;option value=&quot;2023-04-29&quot;&gt;Saturday,29 Apr&lt;/option&gt;Sunday,30 Apr
&lt;option value=&quot;2023-04-30&quot;&gt;Sunday,30 Apr&lt;/option&gt;Monday,01 May
&lt;option value=&quot;2023-05-01&quot;&gt;Monday,01 May&lt;/option&gt;Tuesday,02 May
&lt;option value=&quot;2023-05-02&quot;&gt;Tuesday,02 May&lt;/option&gt;Wednesday,03 May
&lt;option value=&quot;2023-05-03&quot;&gt;Wednesday,03 May&lt;/option&gt;Thursday,04 May
&lt;option value=&quot;2023-05-04&quot;&gt;Thursday,04 May&lt;/option&gt;Friday,05 May
&lt;option value=&quot;2023-05-05&quot;&gt;Friday,05 May&lt;/option&gt;Saturday,06 May
&lt;option value=&quot;2023-05-06&quot;&gt;Saturday,06 May&lt;/option&gt;Sunday,07 May
&lt;option value=&quot;2023-05-07&quot;&gt;Sunday,07 May&lt;/option&gt;Monday,08 May
&lt;option value=&quot;2023-05-08&quot;&gt;Monday,08 May&lt;/option&gt;Tuesday,09 May
&lt;option value=&quot;2023-05-09&quot;&gt;Tuesday,09 May&lt;/option&gt;Wednesday,10 May
&lt;option value=&quot;2023-05-10&quot;&gt;Wednesday,10 May&lt;/option&gt;Thursday,11 May
&lt;option value=&quot;2023-05-11&quot;&gt;Thursday,11 May&lt;/option&gt;Friday,12 May
&lt;option value=&quot;2023-05-12&quot;&gt;Friday,12 May&lt;/option&gt;Saturday,13 May
&lt;option value=&quot;2023-05-13&quot;&gt;Saturday,13 May&lt;/option&gt;Sunday,14 May
&lt;option value=&quot;2023-05-14&quot;&gt;Sunday,14 May&lt;/option&gt;Monday,15 May
&lt;option value=&quot;2023-05-15&quot;&gt;Monday,15 May&lt;/option&gt;Tuesday,16 May
&lt;option value=&quot;2023-05-16&quot;&gt;Tuesday,16 May&lt;/option&gt;Wednesday,17 May
&lt;option value=&quot;2023-05-17&quot;&gt;Wednesday,17 May&lt;/option&gt;Thursday,18 May
&lt;option value=&quot;2023-05-18&quot;&gt;Thursday,18 May&lt;/option&gt;Friday,19 May
&lt;option value=&quot;2023-05-19&quot;&gt;Friday,19 May&lt;/option&gt;Saturday,20 May
&lt;option value=&quot;2023-05-20&quot;&gt;Saturday,20 May&lt;/option&gt;Sunday,21 May
&lt;option value=&quot;2023-05-21&quot;&gt;Sunday,21 May&lt;/option&gt;Monday,22 May
&lt;option value=&quot;2023-05-22&quot;&gt;Monday,22 May&lt;/option&gt;Tuesday,23 May
&lt;option value=&quot;2023-05-23&quot;&gt;Tuesday,23 May&lt;/option&gt;Wednesday,24 May
&lt;option value=&quot;2023-05-24&quot;&gt;Wednesday,24 May&lt;/option&gt;Thursday,25 May
&lt;option value=&quot;2023-05-25&quot;&gt;Thursday,25 May&lt;/option&gt;Friday,26 May
&lt;option value=&quot;2023-05-26&quot;&gt;Friday,26 May&lt;/option&gt;Saturday,27 May
&lt;option value=&quot;2023-05-27&quot;&gt;Saturday,27 May&lt;/option&gt;Sunday,28 May
&lt;option value=&quot;2023-05-28&quot;&gt;Sunday,28 May&lt;/option&gt;Monday,29 May
&lt;option value=&quot;2023-05-29&quot;&gt;Monday,29 May&lt;/option&gt;Tuesday,30 May
&lt;option value=&quot;2023-05-30&quot;&gt;Tuesday,30 May&lt;/option&gt;
&lt;/select&gt;
&lt;/form&gt;

<!-- end snippet -->

答案1

得分: 2

Ok,终于我解决了这个问题。

我将这行代码更改为:

var reservationDate = $contactform.find('#add_r_date option:selected').val();

在此之前,我也按照一些人的建议将方法从GET更改为POST。谢谢。但仅仅那样并没有解决问题,直到我改变了上述行。我正在使用jQuery 1.12。感谢大家的帮助。

英文:

Ok finally i solved the problem.

I changed this line of code

var reservationDate = $contactform.find(&#39;select[name=&quot;add_r_date&quot;]:selected&#39;).val();

to this

var reservationDate = $contactform.find(&#39;#add_r_date option:selected&#39;).val();

Before that i also changed from GET method to POST method as some here suggested. Thanks. But that alone didnt solve the problem until i changed the above line. I am using jquery 1.12. Thanks all for trying to help.

huangapple
  • 本文由 发表于 2023年3月31日 20:39:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/75898634.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定