I have two different forms on one page that dynamically generate inputs. One is used for text inputs and by pressing a button(I’ll call this form 1) another textbox appers. The other adds users to be notified. When a button is pressed a user is added to the list(I’ll call this form 2).
Say I have 3 text boxes generated on form 1, if I try to add another user with form 2 all of the textboxes from form 1 disappear because the data from form 1 isn’t being posted.
Here’s how the two forms are called
<form method="post">
<?
$friends = new common_functions();
$friends->add_friends($added_friends); //this is in common functions - also used for assistance invites
?>
<br>
</form>
<form method="post">
<br>
<?
$register = new common_functions();
$register->register_tasks($j, $reg_description, $reg_num);
?>
</form>
<?
When I change it so these are both posted at the same time (instead of individually as it currently is), the individual forms don’t work correctly.
To sum it up, is there any way to tell a form to post its data even when a submit button isn’t pressed? Something like onaction(post data from other form…)
This is from form 2. If I remove the method from this then I can’t properly delete items from the list.
if(count($added_friends) > 0){
?>
<table width="200">
<col width="150">
<th>Name</th><th>Remove</th>
<?
$count = count($added_friends);
for($i=0; $i< $count; $i++){
if($added_friends[$i] == NULL){
$count = $count;
}
$friend = $added_friends[$i];
?>
<!-- allows a user to remove invited friends -->
<form method="post">
<tr><td><? echo $friend; ?></td><td><input type="submit" name="remove_friend" value="X"/>
<input type="hidden" name="remove_name" value="<? echo $friend; ?>"/>
<input type="hidden" name="added_friends" value="<? echo implode(',',$added_friends); ?>"/></td></tr> </form>
</form>
<?
}
?>
</table>
<?
}
I’m not certain of you’re exact needs, but for something like this I would use knockout.js. There are other such javascript frameworks like angularjs and backbone (google for more).
Here is a simple knockoutjs example on jsFiddle displaying the functionality you’re looking for.
Javascript
HTML
As you can see knockoutjs relies on JQuery being available, but this also gives you access to JQuery’s ajax functionality. This allows you to send the data via ajax. I <3 knockoutjs for it’s simplicity and users love the magic it provides. You’ll have to modify the ajax options to suit your needs, of course