2017-03-22 84 views
1

我仍在绕过JQuery,我在这里要做的是向所有用户发送消息,我试图更多地使用JQuery/Ajax ,到目前为止的代码:在JQuery/PHP中使用电子邮件发送问题

质量email.php

<?php 
include('includes/db_connection.php'); 
include('includes/sessions.php'); 
include('includes/functions.php'); 
include('includes/header.php'); 
include('includes/navbar-logged.php'); 
// who is the admin sending the email? 
$row  = DB::getInstance()->selectOneByField('membership', 'member_username', $member); 
$admins_id = $row['member_id']; 
?> 
<div id="insert-text"></div> 
<div class="panel panel-primary"> 
    <div class="panel-heading">Mass e-mail all users.</div> 
    <div class="panel-body"> 
     <form id="frmAjax" action="mass-email.php" method="post" class="form-horizontal container-fluid" role="form"> 
      <div class="row form-group"> 
       <div class="col-sm-4 text-right"><label for="txtMessage" class="control-label">Message:</label></div> 
       <div class="col-sm-8"> 
        <textarea id="" name="message" class="form-control" required="required"></textarea> 
       </div> 
      </div> 
      <div class="row form-group"> 
       <div class="col-sm-12 text-right"> 
        <button type="submit" name="submit" class="btn btn-default">Send</button> 
       </div> 
      </div> 
     </form> 
    </div> 
    <div class="panel-footer">Send a mass e-mail to <b>all users</b> currently registered.</div> 
</div> 
<script> 
$('#frmAjax').submit(function(e) { 
e.preventDefault(); // important so the submit doesn't clear the data... 
var txtAreaValue = document.getElementsByTagName("textarea")[0].value; 
$.post('ajax-emails.php', {txtAreaValue:txtAreaValue}, function (data) { 
      //alert(txtAreaValue); 
}); 
$('#insert-text').load('ajax-emails.php'); 
}); 
</script> 
<?php 
include('includes/footer.php'); 

Ajax的emails.php

<?php 
    include('includes/db_connection.php'); 
    include('includes/functions.php'); 
    // mass email 
    $result = DB::getInstance()->selectAll('membership'); 
    // loop through the emails 
    foreach ($result as $row) { 
     $email   = $row['member_email']; 
     $username  = $row['member_username']; 
     $message  = nl2br($_POST['txtAreaValue']); 
     $message  = stripslashes($message); 
     echo $message; 
     //$mailSent = sendMassEmail($email, $username, $message); 
     echo "<b>email sent to: <font color='green'>".$email."</font></b>"; 
    } 
?> 

Ť他是我迄今为止所做的工作,我可以将这些信息的价值发挥得淋漓尽致,我似乎无法将价值传递给PHP文件,对于我出错的地方提供帮助将不胜感激。

+0

你要去的jQuery的PHP,PHP的jQuery,并希望再次去PHP?,只留在ajax-emails.php并发送电子邮件。 – Roy

+0

嘿罗伊,你是说在这个例子中绕过使用jquery? – colinreedy674

+0

看看@afzaalace的回复,它用php发送邮件(ajax-email.php),然后返回到#insert-text – Roy

回答

0

我认为这是你想要的。 $('#insert-text').load('ajax-emails.php');不会做任何事情。

<script> 
$('#frmAjax').submit(function(e) { 
    e.preventDefault(); // important so the submit doesn't clear the data... 
    var txtAreaValue = document.getElementsByTagName("textarea")[0].value; 
    $.post('ajax-emails.php', {txtAreaValue:txtAreaValue}, function (data) { 
     $('#insert-text').html(data); 
    }); 
}); 
</script>