Aditya P Bhatt
Aditya P Bhatt

Reputation: 22071

jQuery input value not updating with form submission

Before my HTML form submission, I am putting some values in my hidden input element, which is not submitted with my form, any error or something?

For ex:

Below is my form function:

<form id="clientForm" name="clientForm" method="post" enctype="multipart/form-data" action="clients.php" onClick="beforeSubmit(); return false;">
    <input type="text" name="OLD_BRANCH_FILTERS_ALL" id="OLD_BRANCH_FILTERS_ALL" value="">
</form>

Below is my JS function:

<script language="javascript" type="text/javascript">

    function beforeSubmit(){

        $('#OLD_BRANCH_FILTERS_ALL').attr('value', 'xyz');

        alert(document.getElementById('OLD_BRANCH_FILTERS_ALL').value);

        document.clientForm.action = 'clients.php?saveBtn=Save';
        document.clientForm.submit();
    }

</script> 

Now, When I check the form request $_REQUEST in PHP, it shows no value in element.

Any clue why its not working.

Thanks in advance !

Upvotes: 0

Views: 2513

Answers (1)

AbstractChaos
AbstractChaos

Reputation: 4211

your onlick is not called in time to set the values, try it this way

<form id="clientForm" name="clientForm" method="post" enctype="multipart/form-data" action="clients.php">
    <input type="text" name="OLD_BRANCH_FILTERS_ALL" id="OLD_BRANCH_FILTERS_ALL" value="">
</form>

and the jquery

$('#clientform').submit(function() {
    beforeSubmit();
});

Also, if you also wanted to provide validation to the field you could use,

$('#clientform').submit(function(e) {
    beforeSubmit();
    if (!{VALID}) {
       e.preventDefault();
    }
});

Upvotes: 1

Related Questions