1

So I have this php file where form is located.

<head>
<script src="js/create_competition.js" type="text/javascript"></script>
</head>
<body>
<form id='create-competition' action='create_competition_action.php' method='post' accept-charset='UTF-8'>

    <div id="main">
    <input type="button" id="addButton" value="Add" class="bt" />
    </div>

    <input type='submit' id='submit' name='Submit' value='Submit' />
</body>

And when you press the add button a field is created on the page. When you press it again, another field is created and so on. And when a field is created a variable in create_competition.js gets bigger by 1.

Now when lets say there is 3 field created and after that submit button is pressed I want to pass that variable (which is 3 at the moment) to create_competition_action.php so I could use the knowledge of how many fields was created there. But I have no idea how I can do that.

I tried using this in create_competition.js

$('#submit').click(function(){
      alert(nrFields); // to see if works
      window.location.href = "create_competition_action.php?var=" + nrFields;
    })

Alert worked, after pressing submit I got an alert of how many fields was created but window.location.href did not. But other then that I have not found any good solutions on how to do it.

1
  • Put a hidden field into the form, put the number into its value via your script … and then let the form submit normally. Commented Mar 28, 2015 at 23:39

2 Answers 2

1

You have typo error in your function. You are alerting nrField and appending nrFields with s at end.

But if you need count for iteration or something simmilar, you can use arrays:

 <input type="text" name="values[]" />

and then in PHP

 foreach($_POST["values"] as $value) ...
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks with the typo and the array method worked perfectly so thanks for that too.
0

How about when you click the "add" button it creates the field with this a name like this :

<input type='text' name='data[]'/>

set this same name (data[]) to every inputs.

Then, server side :

<?php
$nb_fields = count($_POST['data']);

If you want to be sure you can iterate on $_POST['data'] as it's an array, and remove empty or invalid values...

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.