0

I'm not sure why my additional input information is not being submitted when the form is submitted. I have applied what I have found in a similar post to insert additional input before the form is submitted. Unfortunately, only "quesiton1answers" is submitted. Additional "time" info is not :( What's wrong with this code...

<form method="post" id="answer_form" action="./submit_test.php">
        <h3>1. xyz</h3>

   <div>
       <input type="radio" name="question1answers" id="question-1-answers-A" value="A" />
       <label for="question-1-answersA">A) <sup>253</sup>/<sub>240</sub> </label>
   </div>

   <input type="submit" value="Submit" name="Submit" style="font-size:32px;">
</form>

jquery:

$("#answer_form").submit(function(e){
    $("<input />").attr('type', 'hidden')
      .attr('name', 'time')
      .attr('value','60')
      .appendTo("#answer_form");
  return true;
});
rickster26ter1
  • 373
  • 3
  • 10

1 Answers1

1

This should work, as many others solutions offered at topic you this code from How to add additional fields to form before submit?

You can also do it simply like this (just add hidden):

$("#answer_form").submit(function(e) {
  $(this).append('<input  name="time" value="60">');
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<form method="post" id="answer_form" action="./submit_test.php">
  <h3>1. xyz</h3>

  <div>
    <input type="radio" name="question1answers" id="question-1-answers-A" value="A" />
    <label for="question-1-answersA">A) <sup>253</sup>/<sub>240</sub> </label>
  </div>

  <button type="Submit" value="Submit" name="Submit" style="font-size:32px;">Submit</button>
</form>

This works as you see, if not problem is somewhere else, not in code presented. Maybe in your PHP.

ikiK
  • 6,328
  • 4
  • 20
  • 40
  • I really appreciate your answer, that helps a lot, thank you so much. So, I'm just looking at the "Network" settings on chrome tools, and it doesn't show it submits. So I'll have to see if there's anything else weird I can't see for some strange reason... – rickster26ter1 Jul 12 '20 at 23:34
  • @rickster26ter1 I just tested this in production server, it works as it should. I am getting: 60 A, check your PHP fetching. This worked for me: `$time=$_POST['time']; $question1answers=$_POST['question1answers']; echo $time."
    "; echo $question1answers."
    ";`
    – ikiK Jul 12 '20 at 23:52
  • Thank you, ikiK. I found out that this works now, and I was looking for #answer_form before it even existed in another javascript file. I put it in my index.php, and it works! However, your $("#answer_form").submit works, not mine. I was just putting a value for the time for now, but I need to be able to insert a javascript variable (time would be dynamic) . When I try mine, it won't submit, unfortunately. – rickster26ter1 Jul 13 '20 at 00:01
  • @rickster26ter1 just replace 60 with: '+yourvariable+' – ikiK Jul 13 '20 at 00:08