Oranje
Oranje

Reputation: 67

How to get value of JS promptbox, then pass it to PHP and save it in the database?

I have a home_admin.php file where the user can reject an entry with this code:

echo "
    <td>
        <a href=changeStatReject.php?id=" . $row['id'] . ">"  . "
            <img src='media/reject.png'>" . "
        </a>
    </td>
";

Once the link is clicked, it will direct the user to the changeStatReject.php page with the specific ID of the entry.

My goal is to show a JavaScript PromptBox asking the reason for rejecting before the code is executed.

I use this code:

function MyReason(){
    var reason = prompt("Reason:");
    document.getElementById("reason").value = reason;
    document.getElementById("form").submit();
}

Then I add this to the home_admin.php page:

<form id="form" method="POST" action="changeStatReject.php">
    <button onclick="MyReason()">Click Me!</button>
    <input type="hidden" id="reason" name="reason"/>
</form>

Even though I get the value from the prompt box, the script for the changeStatReject action is not working anymore.

I think there's a conflict with the <form action="changeStatReject.php"> and <a href=changeStatReject.php?id=" . $row['id'] . ">

Any advice on how to get the prompt box value, save it to the database and at the same time update (reject) the status of the specific entry?

Thanks Mates,

Upvotes: 1

Views: 837

Answers (2)

user3955254
user3955254

Reputation:

<?php
 echo "<td> <a href= "#" onclick='myreason(".$row['id'] . "')>"  . "<img src='media/reject.png'>" . "</a></td>";
?>
.............
function MyReason(id){
    id.preventDefault();    
    var reason = prompt("Reason:");
    document.getElementById("reason").value = reason;
    document.getElementById("id").value = id;
    document.getElementById("form").submit();
}
.............
<form id="form" method="POST" action="changeStatReject.php">
    <input type="hidden" id="id" name="id" />
    <input type="hidden" id="reason" name="reason"/>
</form>
...............
Now you can handle the POST variables in your changeStatReject.php as you want.

Upvotes: 2

sarath
sarath

Reputation: 56

add <input type="hidden" name="id" value='id_you_need_to_pass'/> in your form and call changeStatReject.php through this form submit.

Upvotes: 0

Related Questions