0

I have some question. I have the html form with one hidden input element. And I have an array in my javascript code. I want to catch pressing submit button event (by using jquery) and push the array data in the hidden input.

The question is - what happens first: form date will flow to .php script or javascript's array will be pushed into hidden input and afterwords .php script will be called? And why it is so?

TIA!

upd (code sample):

...
// process $_POST['domains']
...
<form action="registration?id=reg" method="post" enctype="multipart/form-data" id="reg_form">
 ...
 <input type="hidden" id="domains" name="domains[]" value=""/>
 ...
 <input type="submit" name="go" value="Register"/>
</form>
<script type="text/javascript">
 var domainlist = [];
 Array.prototype.indexOf = function (name) {
 var ind = -1;
 for (var i = 0; i < this.length; i++) {
 if (this[i].name == name) {
 ind = i;
 break;
 }
 }
 return ind;
 }
 $(document).ready(function() {
 ...
 });
 function checkDomain() {
 ...
 req = $.ajax({
 type: 'post',
 cache: false,
 url: 'ajax.php',
 data: ...,
 success: function(data) {
 $('#domain_list_wrap').delegate('input:checkbox', 'click', function () {
 var dmnName = $(this).attr('name');
 domainlist.push({name: dmnName, cost: domainsArr[dmnName.split('.')[1]]});
 ...
 });
 $('#domain_cart').delegate('input:checkbox', 'click', function () {
 domainlist.splice(domainlist.indexOf($(this).attr('name')), 1);
 ...
 });
 }
 });
 ...
 }
</script>
hakre
200k55 gold badges454 silver badges868 bronze badges
asked Sep 26, 2011 at 14:54
2
  • 1
    If you code it such that the hidden field is populated first, the hidden field will be populated first. Commented Sep 26, 2011 at 14:56
  • 2
    it is better if you add some code to your question. for passing array values from the same HTML page you do not need PHP to process it. create a JavaScript function and call it when the form is submitted. make sure the the function returns FALSE to stop the page from completing the submisson to the action page (in the HTML form). Hope this will help. Commented Sep 26, 2011 at 15:04

2 Answers 2

5

The JavaScript submit event is fired when the form is submitted, but before the request is made to the server (as JavaScript runs on the client side this makes perfect sense). Therefore, the way you have imagined it working should be fine, and you can bind to the form submit event using jQuery as follows:

$("#yourForm").submit(function() {
 //Submit event handler will run on form submit
});

The submit event handler can also be used to cancel form submission (for example, if some validation fails) by returning false. It wouldn't really work if the data was sent to the server first!

answered Sep 26, 2011 at 14:57
Sign up to request clarification or add additional context in comments.

Comments

1

Put this in your body

<form id="frm" method="post" action="/">
 <input type="hidden" id="txtHidden" />
 <input type="submit" id="btnSubmit" value="submit" />
</form>

And this in your head:

<script type="text/javascript">
 var arr = [ "hello", "there", "world" ];
 $(document).ready( function() {
 $("#btnSubmit").click( function(event) {
 var str = arr.join(",");
 $("#txtHidden").val(str);
 // make sure the value is in the field
 alert($("#txtHidden").val());
 });
 });
</script>

When you click on btnSubmit, the array will be joined with commas, you'll see the alert, then the form will be submitted.

answered Sep 26, 2011 at 15:20

2 Comments

So, .click event fires before .submit event?
Yes! Otherwise you wouldn't have seen the alert :-)

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.