首页 > 解决方案 > Getting the same value in an php button in javascript with AJAX

问题描述

I'm working on adding a reason as to why someone's request got denied on my website. I want the submit reason button to have the same value as the reject button that reveals the textarea to add a reason. I know that I have to use AJAX but I don't know how or what to use.

This is the javascript code that I'm using to reveal the popup

<script type="text/javascript">
    function openForm() {
        document.getElementById("myForm").style.display = "block";
    }

    function closeForm() {
        document.getElementById("myForm").style.display = "none";
    }
</script>

This is my html code for the popup

<div class="form-container" id="myForm">
    <form method="post" class="reason-popup">
        <h1>Reden</h1>
        <textarea placeholder="Type message..." id="rejectReasonText" name="rejectReasonText"></textarea>
        <button type="submit" class="btnSubmit" name="rejectReasonSubmit" value="<?php echo $leaveRequestId; ?>">Verzenden</button>
        <button type="button" class="btnCancel" onclick="closeForm()">Sluiten</button>
        <input type="hidden" name="selectedUser" value="<?php echo $selectedUser; ?>">
    </form>
</div>

And this is my php code

$result = mysqli_query($db, $sql);
        if (mysqli_num_rows($result) > 0) {
            while ($row = mysqli_fetch_assoc($result)) {
                if((isTeamLeider() || isAdmin()) && $_SESSION['user']['team'] == $row["team"]) {
                    echo '<tr><form method="post">';
                    echo '<td>' . $row["name"]              . "<br>"        . '</td>';
                    echo '<td>' . $row["startDate"]         . "<br>"        . $row["endDate"] . '</td>';
                    echo '<td>' . $row["startTime"]         . "<br>"        . $row["endTime"] . '</td>';
                    echo '<td style="text-align : center;">' . $row["reason"]           . "<br>"        .  '</td>';
                    echo '<td>' . $row["specialLeave"]      . "<br>"        . '</td>';
                    echo '<td><button name="accept" value="'. $row["ID"]    .'" class="btn">accepteren</button></td>';
                    echo '</form>';
                }

                echo '<td><button name="reject" value="'      . $row["ID"]    .'"  onclick="openForm()" class="btn">weigeren</button></td>';
                echo '</form></tr>';
            } 

                if (isset($_POST['reject'])) {
                    global $db, $leaveRequestId;
                    $_SESSION['leaveRequestId'] = $_POST['reject'];
                    $leaveRequestId = $_SESSION['leaveRequestId'];

                }


            if (isset($_POST['rejectReasonSubmit'])) {
                global $db, $leaveRequestId;
                $rejectReason = e($_POST["rejectReasonText"]);

            }

So I want the value of rejectreasonsubmit to be the same as the reject button.

标签: javascriptphphtmlajax

解决方案


推荐阅读