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 ?

2
  • 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? Commented Jan 30, 2018 at 2:12
  • Possible duplicate of JavaScript post request like a form submit Commented Jan 30, 2018 at 2:13

1 Answer 1

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
});
Sign up to request clarification or add additional context in comments.

2 Comments

Ty Scott but still not sending the userID whose value should be $('#suggest1_hidden').val() according to the first working script above
@Nik You have to make sure the hidden field gets its value set. You have no code to do that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.