简体   繁体   English

将PHP电子邮件添加到具有通过AJAX呼叫终止代码发送的信息的文件中

[英]Adding in a PHP email to a file that has info sent with an AJAX call killing code

I created code that updates the user's group level(permission level). 我创建了更新用户组级别(权限级别)的代码。 I send the selected id and group level via AJAX to a php file called user_update_group. 我通过AJAX将选定的ID和组级别发送到名为user_update_group的php文件。 Updating the user's group # worked perfectly before I tried to add the PHP email in the same file. 在尝试将PHP电子邮件添加到同一文件之前,更新用户的组#效果很好。 I'm doing this because I am already getting that user's info from my database, so I thought it was the best approach. 我这样做是因为我已经从数据库中获取了该用户的信息,所以我认为这是最好的方法。

However, when adding in the email it breaks the code. 但是,在添加电子邮件时会破坏代码。 Is there something in my php file that is obviously killing this or can I not do it this way? 我的php文件中是否有某些东西显然可以杀死它,或者我不能这样做吗? I am trying to SELECT all the data in my users table from the id that was brought over and then send an email out to them using the same button as the other query used. 我试图从带来的ID中SELECT用户表中的所有数据,然后使用与其他查询相同的按钮向他们发送电子邮件。 My php email is the second part in the php file I added in here. 我的php电子邮件是我在此处添加的php文件的第二部分。 I added in all of this code to show what I am trying to do. 我添加了所有这些代码以显示我要执行的操作。

<?php
$con2 = mysqli_connect("localhost", "root", "", "db");
$run2 = mysqli_query($con2,"SELECT * FROM user_requests ORDER BY id DESC");
$runUsers2 = mysqli_query($con2,"SELECT * FROM users ORDER BY id DESC");
$numrows2 = mysqli_num_rows($run2);

    if( $numrows2 ) {
        while($row2 = mysqli_fetch_assoc($run2)){
            if($row2['status'] == "Approved"){
            //var_dump ($row2);

                $approved_id        = $row2['user_id'];
                $approved_firstname = $row2['firstname'];
                $approved_lastname  = $row2['lastname'];
                $approved_username  = $row2['username'];

    if ($approved_firstname == true) {
        echo "Name - ". $approved_firstname . " " . $approved_lastname . "

</br>" . 
                "Username - ". $approved_username . "</br></br>"
?>
<div class="change_group_button"> 
     <a class="change_group" href="javascript:void(0)">Change User Permission</a>
</div><br>
<div id="light" class="change_group_popup">
    <a class="close" href="javascript:void(0)">Close</a>
 <div class="group_success" style="color: red;"></div><br>
    <form id="update_group" action="" method="POST" accept-charset="utf-8">
       <div class="field">
        <label for="group">Group</label>
        <input type="hidden" value="<?php echo $approved_id; ?>" id="approved_id" name="id" />
        <select id='group_id' name='group' required>
            <option value=''><?php echo htmlentities($group); ?></option>
            <option value="1">Bench</option>
            <option value="2">Spectator</option>
            <option value="3">Team Member</option>
            <option value="4">Commissioner</option>
        </select>
    </div>
    <input type="submit" value="submit" name="group">
    </form>

AJAX call AJAX电话

$(document).ready(function () {

    $('#update_group').on('submit', function (event) {
    event.preventDefault();
        $.ajax({
            url: 'user_group_update.php',
            type: 'POST',
            data: {
            id: $("#approved_id").val(), //id
           // update_group: $("#group_id").val() //group level
          update_group: $(this).find( "#group_id option:selected" ).val()
        },
            success: function (data) {
                //do something with the data that got returned
                $(".group_success").fadeIn();
                $(".group_success").show();
                $('.group_success').html('User Permission Level Changed!');
                $('.group_success').delay(5000).fadeOut(400);
               // alert(data);
            },
             error: function(jqXHR, textStatus,errorThrown )
            {
              // alert on an http error 
              alert( textStatus +  errorThrown );
            }
        });
        return false;
    });
});

user_group_update php file. user_group_update php文件。

$approved_id = $_POST['id'];
$change_group = $_POST['update_group'];

$con = mysqli_connect("localhost","root","","db");
    /* check connection */
    if (mysqli_connect_errno()) {
        printf("Connect failed: %s\n", mysqli_connect_error());
        exit();
    }
    $stmt = $con->prepare("UPDATE users SET `group`=? WHERE id=?");
    if ( !$stmt || $con->error ) {
     // Check Errors for prepare
        die('User Group update prepare() failed: ' . htmlspecialchars($con->error));
    }
    if(!$stmt->bind_param('ii', $change_group, $approved_id)) {
    // Check errors for binding parameters
        die('User Group update bind_param() failed: ' . htmlspecialchars($stmt->error));
    }
    if(!$stmt->execute()) {
        die('User Group update execute() failed: ' . htmlspecialchars($stmt->error));
    }

    //-------Email test--------------

    $email_stmt = $con->prepare("SELECT * FROM users WHERE id=?");
    if ( !$email_stmt || $con->error ) {
     // Check Errors for prepare
        die('User email prepare() failed: ' . htmlspecialchars($con->error));
    }/*
    if(!$email_stmt->bind_param('ii', $change_group, $approved_id)) {
    // Check errors for binding parameters
        die('User email bind_param() failed: ' . htmlspecialchars($stmt->error));
    }
    if(!$email_stmt->execute()) {
        die('User email execute() failed: ' . htmlspecialchars($stmt->error));*/
        /*$pending_id = $_POST['id'];
        $group_firstname = $_POST['firstname'];
            $group_lastname = $_POST['lastname'];
            $group_username = $_POST['username'];
            $group_email = $_POST['email'];
            $group_email = $_POST['group'];

            $to = $group_email;
            $subject = 'There is a new user request to join the Sunday Funday League';
            $message = '
            <html>
            <head>
                <title>New SFL User Request</title>
            </head>
            <body>
                <p>Hi '.$group_firstname.',</p><br>
                <p>Your Sunday Funday League Account has been accepted. You have been added to the group. To sign in, click this link
                http://sundayfundayleague.com . </p><br>
                <p>Thank you,</p>
                <p>Administration</p>
            </body>
            </html>
            ';

            $from = "user-requests@sundayfundayleague.com";
            $Bcc = "user-requests-confirm@sundayfundayleague.com";

            // To send HTML mail, the Content-type header must be set
            $headers  = 'MIME-Version: 1.0' . "\r\n";
            $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

            // Additional headers
            $headers .= 'To: ' .$to. "\r\n";
            $headers .= 'From: ' .$from. "\r\n";
            $headers .= 'Bcc: '.$Bcc. "\r\n";

            // Send the email
            mail($to,$subject,$message,$headers);
    }*/

UPDATE code for user_update_group file user_update_group文件的更新代码

$approved_id = $_POST['id'];
//test - delete if it doesn't work
$approved_firstname = $_POST['firstname'];
$approved_lastname = $_POST['lastname'];
$approved_username = $_POST['username'];
$approved_email = $_POST['email'];
$change_group = $_POST['update_group'];


$con = mysqli_connect("localhost","root","","db");
    /* check connection */
    if (mysqli_connect_errno()) {
        printf("Connect failed: %s\n", mysqli_connect_error());
        exit();
    }
    $stmt = $con->prepare("UPDATE users,user_requests SET users.group=?, user_requests.group=? WHERE users.id=? AND user_requests.user_id=?");
    if ( !$stmt || $con->error ) {
     // Check Errors for prepare
        die('User Group update prepare() failed: ' . htmlspecialchars($con->error));
    }
    if(!$stmt->bind_param('iiii', $change_group, $change_group, $approved_id, $approved_id)) {
    // Check errors for binding parameters
        die('User Group update bind_param() failed: ' . htmlspecialchars($stmt->error));
    }
    if(!$stmt->execute()) {
        die('User Group update execute() failed: ' . htmlspecialchars($stmt->error));
    }

    //test
    $email_stmt = $con->prepare("SELECT * FROM users WHERE id=?");
    if ( !$email_stmt || $con->error ) {
     // Check Errors for prepare
        die('User email prepare() failed: ' . htmlspecialchars($con->error));
    }
    /*if(!$email_stmt->bind_param('ii', $change_group, $approved_id)) {
    // Check errors for binding parameters
        die('User email bind_param() failed: ' . htmlspecialchars($stmt->error));
    }
    if(!$email_stmt->execute()) {
        die('User email execute() failed: ' . htmlspecialchars($stmt->error));*/

            $to = $approved_email;
            $subject = 'There is a new user request to join t';
            $message = '
            <html>
            <head>
                <title>New User Request</title>
            </head>
            <body>
                <p>Hi '.$approved_firstname.',</p><br>
                <p>Your  Account has been accepted. You have been added to the group. To sign in, click this link
                http://example.com . </p><br>
                <p>Thank you,</p>
                <p>Administration</p>
            </body>
            </html>
            ';

            $from = "user-requests@example.com";
            $Bcc = "user-requests-confirm@example.com";

            // To send HTML mail, the Content-type header must be set
            $headers  = 'MIME-Version: 1.0' . "\r\n";
            $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";

            // Additional headers
            $headers .= 'To: ' .$to;//. "\r\n";
            $headers .= 'From: ' .$from;//. "\r\n";
            $headers .= 'Bcc: '.$Bcc;//. "\r\n";

            // Send the email
            mail($to,$subject,$message,$headers);

_POST is auto generated by the browser on submit of the form. _POST由浏览器在提交表单时自动生成。 If you want to pass data through the post you have to use form submit fields. 如果要通过帖子传递数据,则必须使用表单提交字段。 These fields can be hidden: 这些字段可以隐藏:

<input type=hidden name="lastname" value="{some_last_name}"/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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