简体   繁体   中英

Issue with email form in JQuery/PHP

I'm still getting my way around JQuery, what i'm trying to do here is send a message to all my users on site, i'm attempting to use JQuery/Ajax more, the code so far:

mass-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>";
    }
?>

This is what i have done so far, i can pring out the value of the message fine, i just cannot seem to pass the value through to the PHP file, any help on where i'm going wrong would be appreciated.

I think this is what you want. $('#insert-text').load('ajax-emails.php'); won't do anything.

<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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM