0

i've got this script that send the variable recordID with GET (and works OK)

<input type="hidden"   id="suggest1_hidden"  name="suggest1_hidden" value="">
<input name="suggest1" type="text" class="suggest_table {th : ['nome', 'email', 'tel', 'cell']}" id="suggest1" style="width:650px;" alt="Adm_ut_search.php" />
<span id="comando"><span class="button">Dettagli utente</span></span>

<script language="javascript">
$(document).ready(function()
{
$('#comando').click(function () { 
var url="Adm_ut_view_details.php?recordID=" + $('#suggest1_hidden').val()
document.location.href = url
});
});
</script>

I need to send the variable with POST and i wrote this... but doest work

<form id="myForm" action="Adm_ut_view_details.php" method="post"/>
<input type="hidden" id="suggest1_hidden" name="suggest1_hidden" value="">
<input name="suggest1" type="text" class="suggest_table {th : ['nome', 'email', 'tel', 'cell']}" id="suggest1" style="width:650px;" alt="Adm_ut_search.php" />
<input name="recordID" type="hidden" id="userID" value="" />
<button type="submit" id="comando">Submit</button>
</form>

<script language="javascript">
$(function(){
$('#myForm').on('submit', function(e){
document.getElementById('userID').value=+ $('#suggest1_hidden').val()
$('#myForm').submit();
});
});
</script>

what i do wrong ?

Nik
  • 1
  • 1
  • Have you checked the browser console for errors or tried to `echo` thing in the `php` to check what (if any) data is being sent? – NewToJS Jan 30 '18 at 02:12
  • Possible duplicate of [JavaScript post request like a form submit](https://stackoverflow.com/questions/133925/javascript-post-request-like-a-form-submit) – Tibrogargan Jan 30 '18 at 02:13

1 Answers1

1

There is nothing inherently different that you need to do with your form fields because you are sending a POST request vs. a GET request. Changing the form's action to POST is all you need to do as long as no JavaScript pre-submit processing is desired.

And, the hidden form field is going to be sent along with the other form fields anyway, so why bother concatenating it to the userID?

But, to your question and your code, you have a typo:

document.getElementById('userID').value =+ $('#suggest1_hidden').val()

Should be:

document.getElementById('userID').value += $('#suggest1_hidden').val()

Now, you have code that sets up your submit event handler, but you have no code that sets the value of the hidden form field. That needs to be done prior to the submit taking place.

Also, you need to prevent the form from submitting first, so that you can modify the form field value.

$(function(){
  $('#myForm').on('submit', function(e){
    e.preventDefault(); // Stop the submit

    // YOU NEED TO MAKE SURE THAT THE HIDDEN FORM FIELD'S
    // VALUE HAS BEEN SET BY THIS POINT.

    document.getElementById('userID').value += $('#suggest1_hidden').val()
    $('#myForm').submit(); // Then manually submit
});
Scott Marcus
  • 57,085
  • 6
  • 34
  • 54
  • Ty Scott but still not sending the userID whose value should be $('#suggest1_hidden').val() according to the first working script above – Nik Jan 30 '18 at 02:27
  • @Nik You have to make sure the hidden field gets its value set. You have no code to do that. – Scott Marcus Jan 30 '18 at 03:07