Tyler Conoff
Tyler Conoff

Reputation: 1

Cant submit my button with form

I am trying to submit my button with a form. Im not trying to make my button submit the form. I want to be able to see my button value in the POST variable after the form submits. From my understanding all I need is to give my element a name and value. I should be able to see all the form variables once my form is submitted.

<input name='MC[]' type='text' size='51' placeholder='Enter In Question'>
<br/>
<input name='MC[]' type='button' value='Incorrect'>
<input name='MC[]' id='Options' size='40' placeholder='Enter In Option A'>

I'm new to this site not sure if I'm providing enough information but I simply want to submit this button inside a form and to be able to add the value of my button to a file. For some reason I cant see the button once the form is submitted. Are type button not sent to POST when submitted?

Note, I am able to see my other input elements. The type button one is the only one I cant see.

Upvotes: 0

Views: 235

Answers (2)

Barmar
Barmar

Reputation: 780724

You can use Javascript that fills in the value of a hidden input from the value of the button that was clicked.

HTML:

<input type="hidden" name="answer" id="answer">

JS:

document.querySelectorAll("input[name='MC[]']").forEach(function(el) {
    el.addEventListener("click", function() {
        document.getElementById("answer").value = el.value;
    }
}

Then you'll be able to get the button's value in $_POST['answer'].

Upvotes: 1

Graeme Chapman
Graeme Chapman

Reputation: 126

That's not how buttons work... they perform an action, they don't get included in the post data. What you need is a checkbox, or a disabled input perhaps?

Upvotes: 0

Related Questions