Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Mfrancik's avatar

Javascript popup with text input to post

I have a checkbox the user can change with a submit button which currently posts and updates that particular field. What I'd like to do, is when user clicks submit a popup will appear with a text area with user can input a string. I want both, the inputed text to post, as well as the value which was changed from the checkbox. I'd like a confirm and cancel button on the popup. Confirm will post the data and cancel will do nothing.

What is the best way to do this?

0 likes
3 replies
Mfrancik's avatar

My question may have been worded incorrectly. I am using a javascript prompt which the user inputs data before submitting form. I want to get the data inputed into to the prompt window in my controller.

<script>
        function reasonForLeaving() {
            var reasonForLeaving = prompt("Reason for discharge?");
            if(reasonForLeaving === null  || reasonForLeaving == "") {
                alert('Enter reason for discharge');
                return false;
            }
            else if (reasonForLeaving != null) {
                return true;
            }
        }
</script>
Mfrancik's avatar
Mfrancik
OP
Best Answer
Level 3

I created a hidden input in my form and assigned the value to that field in JS.

HTML:

<input type="hidden" name="dischargeReason" id="setReasonForLeaving">

Javascript:

function reasonForLeaving() {
            var reasonForLeaving = prompt("Reason for discharge?");
            if(reasonForLeaving === null  || reasonForLeaving == "") {
                alert('Enter reason for discharge');
                return false;
            }
            else if (reasonForLeaving != null) {
                document.getElementById("setReasonForLeaving").value = reasonForLeaving;
                return true;
            }
        }

Please or to participate in this conversation.