我正在尝试将存储在变量中的元素的ID发送到我的PHP文件中,但它不会发送它。我的JS文件中的警报响起,所以问题似乎并不存在。当我点击提交以发送我的电子邮件时,'email'变量会发送,但'band'会变空。 我的错误在哪里?
HTML LIST
<div class="artistn">
<li class="highlight" id="Mitski"> <span class="left gone">Mitski</span>
<audio controls>
<source src="songs/exsong.mp3" type="audio/mpeg">
<source src="songs/exsong.mp3" type="audio/ogg">
<embed height="50" width="100" src="horse.mp3">
</audio>
</li>
<li id="AdultMom">
<span class="left">Adult Mom</span>
<audio controls>
<source src="songs/exsong.mp3" type="audio/mpeg">
<source src="songs/exsong.mp3" type="audio/ogg">
<embed height="50" width="100" src="horse.mp3">
</audio>
</li>
</div>
HTML表格
<form method="post" action="js/sendmail.php">
<label> Your Email </label> <input type="text" name="email"></input>
<input id="submit" type="submit" value="Submit" class="button">
</form>
JS
$( "li" ).click(function() {
$('.highlight').removeClass('highlight');
$( this ).toggleClass( "highlight" );
var tBand = jQuery(this).attr('id');
alert(tBand);
$("#submit").click(function(){
alert(tBand);
$.ajax({url: "sendmail.php", type: "post", data: {"tBand": tBand}})
});
});
PHP
$email = $_POST['email'];
$band = $_POST['tBand'];
mail("example@email.com", "Subject Title",$band,"From: $email");
header("Location: ../index.php");
alert('success');
答案 0 :(得分:1)
您没有通过Ajax调用将数据发送到服务器,表单正在提交。您需要取消表单提交。你也没有发送电子邮件,只有tBand。
$("#submit").click(function(e){
e.preventDefault();
$.ajax({
url: "sendmail.php",
type: "post",
data: {
"tBand": tBand,
"email", $("[name='email']").val()
}
});
});
如果您多次分配,也可能需要删除点击
$("#submit").off("click").on("click", function(e){