Php 通过Ajax/Post进行数据推送
我构建了一个PHP/Ajax脚本,通过PHPMailer发送大量的时事通讯电子邮件,除了从邮件主题的HTML输入框中获取值外,它工作得非常好 该表使用以下HTML/Bootstrap从MySQL数据库获取值Php 通过Ajax/Post进行数据推送,php,ajax,ajaxform,Php,Ajax,Ajaxform,我构建了一个PHP/Ajax脚本,通过PHPMailer发送大量的时事通讯电子邮件,除了从邮件主题的HTML输入框中获取值外,它工作得非常好 该表使用以下HTML/Bootstrap从MySQL数据库获取值 <div class="table-responsive"> <table class="table table-bordered table-striped">
<div class="table-responsive">
<table class="table table-bordered table-striped">
<tr>
<th>Customer Name</th>
<th>Email</th>
<th>Select</th>
<th>Action</th>
</tr>
<?php
$count = 0;
foreach($result as $row)
{
$count = $count + 1;
echo '
<tr>
<td>'.$row["customer_name"].'</td>
<td>'.$row["customer_email"].'</td>
<td>
<input type="checkbox" name="single_select" class="single_select" data-email="'.$row["customer_email"].'" data-name="'.$row["customer_name"].'" />
</td>
<td>
<button type="button" name="email_button" class="btn btn-info btn-xs email_button" id="'.$count.'" data-email="'.$row["customer_email"].'" data-name="'.$row["customer_name"].'" data-action="single">Send Single</button>
</td>
</tr>
';
}
?>
<tr>
<td colspan="3"></td>
<td><button type="button" name="bulk_email" class="btn btn-info email_button" id="bulk_email" data-action="bulk">Send Bulk</button></td></td>
</tr>
<tr>
</table>
</div>
与
我尝试的操作是在我的HTML页面中包含以下行,如
<input type="text" name="subject" id="subject">
// modify the AJAX Script
subject:$(this).data("subject")
or var subject = document.getElementById("subject");
//修改AJAX脚本
主题:$(本).数据(“主题”)
或var subject=document.getElementById(“subject”);
但是没有一个成功:(
有什么指导吗?谢谢!祝大家今后健康。干杯。既然您从$的数据属性中选择了“主题”(this),您需要在复选框/按钮上填充数据
因此,从您当前的代码
<input type="checkbox" name="single_select" class="single_select" data-email="'.$row["customer_email"].'" data-name="'.$row["customer_name"].'" />
<button type="button" name="email_button" class="btn btn-info btn-xs email_button" id="'.$count.'" data-email="'.$row["customer_email"].'" data-name="'.$row["customer_name"].'" data-action="single">Send Single</button>
单发
您需要转换为
<input type="checkbox" name="single_select" class="single_select" data-email="'.$row["customer_email"].'" data-name="'.$row["customer_name"].'" data-subject="'.$row["subject"].'" />
<button type="button" name="email_button" class="btn btn-info btn-xs email_button" id="'.$count.'" data-email="'.$row["customer_email"].'" data-name="'.$row["customer_name"].'" data-action="single" data-subject="'.$row["subject"].'">Send Single</button>
单发
一旦您填充了数据属性,您就可以使用
subject:$(this).data(“subject”)
来填充此属性。不确定将$(this).data(“subject”)放在哪里
但这一切都取决于此是什么,以及它是否具有适当的数据主题
属性。var subject=document.getElementById(“主题”);
会将变量设置为匹配的元素。不过您可能想要元素的值。太棒了!谢谢Andrea。填充数据主题需要从数据库中获取数据。有没有建议如何简单地获取inputbox的值并通过ajax发布到pHPMail?我最好的尝试是添加一个输入框,其中,
读取val使用var subject=document.getElementById(“subject”);
和ajaxsubject:$(this.data(“subject”)
的ue,我没有工作。有什么想法吗?关于如何从输入中获取主题,你说得对。最后一部分应该是subject:subject
或subject:document.getElementById(“subject”)
因为您没有使用$(this)。$(this)实际上是按钮或复选框不起作用。脚本根本不起作用。我尝试通过getElementId读取值,并发布如下代码,$('.email_按钮')。单击(function(){$(this.attr('disabled','disabled');var id=$(this.attr(“id”);var action=$(this).data(“action”);var subject=document.getElementById(“subject”);var email_data=[];if(action='single'){email_data.push({email:$(this.data(“email”);name:$(this.data(“name”);subject:subject};}else{
我已经用以下,$解决了我的问题。ajax({url:“send_mail.php”),方法:“POST”,数据:{email\u data:email\u data,subject},
和var subject=document.getElementById(“subject”).value;
谢谢!
$mail->Subject($row["subject"]);
<input type="text" name="subject" id="subject">
// modify the AJAX Script
subject:$(this).data("subject")
or var subject = document.getElementById("subject");
<input type="checkbox" name="single_select" class="single_select" data-email="'.$row["customer_email"].'" data-name="'.$row["customer_name"].'" />
<button type="button" name="email_button" class="btn btn-info btn-xs email_button" id="'.$count.'" data-email="'.$row["customer_email"].'" data-name="'.$row["customer_name"].'" data-action="single">Send Single</button>
<input type="checkbox" name="single_select" class="single_select" data-email="'.$row["customer_email"].'" data-name="'.$row["customer_name"].'" data-subject="'.$row["subject"].'" />
<button type="button" name="email_button" class="btn btn-info btn-xs email_button" id="'.$count.'" data-email="'.$row["customer_email"].'" data-name="'.$row["customer_name"].'" data-action="single" data-subject="'.$row["subject"].'">Send Single</button>