How can I have one submit button for multiple forms? - javascript

I have tried to make one submit button submit multiple forms using the following resources:
https://www.daniweb.com/web-development/php/threads/255723/two-forms-one-submit-button-hw-does-it-work
https://forums.digitalpoint.com/threads/post-multiple-forms-with-a-single-button-press-with-javascript.2271691/
http://board.phpbuilder.com/showthread.php?10351103-submit-two-forms-with-one-button
When I use any of code from above, I can submit only one of my forms and its always the last one in the list of all form. I believe the problem could be that every form I have is in separate php file and I'm using include 'php/option-type.php' ; to include them in index.php where I also tested codes from above forum.

Combine both forms into one form, and then process the submitted data in a php script, which can then do both actions needed.
Can you give a little more information about the two forms, and what is each submit supposed to do? Then I'll be able to elaborate on my answer.

Related

Need to reset fields in PHP edit form somehow

I have a DB-based AMCharts form/page that graphs figures that are entered in a separate edit form accessed from that main page. This latter php edit form is set up to always bring up the present set of entries in every field. This edit form serves two purposes: one is to be able to correct earlier mistakes, and the other is to enter a whole set of new ones. The new figures are entered in this edit page and then submitted. A series of four sets of previous generations are saved in identical tables, which are also graphed on the same main page. A JS div slider script allows the user to compare among 5 generations of separate graphs in the same space. A trigger built into MySql allows each new set to be passed on to older sets, 5 gen deep. Without getting into too many details, the trigger is written so that it is activated if two specific fields in the form are different from the previous. Thus allowing mistakes to be corrected without activating the trigger. With me so far? My question does not concern the structure of the thing, it works fine, but just one problem that does not seem to be available on SO or any other google search result. Specifically, in the case that the edit form is used to enter a brand new set of numbers, how could you use a reset button that activates a script that clears all the old figures from the inputs? I've found numerous answers on SO that use either pure JS or JQuery, but they are not used on PHP forms, rather HMTL forms. Some posters point out that this is anyhow not possible, as edit forms contain DB values cannot be replaced unless the SUBMIT button is used. This simple device is to make entry of new figures easier when they are empty, but I haven't found any scripts so far that succeed in clearing them. The simple reset code that works in HTML forms but in my php one:
<script>function resetform() {
document.getElementById("myform").reset();}</script>
<form action="" method="post" id="myform">
<input value="RESET" onclick="resetform()">
Like j08691 said, you might wish to brush up on your information on what PHP is and what it does.
It creates a HTML page and once it's created, it doesn't "touch" it anymore. It's done, finito. The only way for PHP to "modify" the page is to create (reload) it again.
If you wish to interact with a ready, loaded HTML page - such as a form with values inputed in the fields, you will have to use another technology like Javascript.
Now, if your problem is the "echo $var" -part - you could avoid populating the form fields with values by either emptying the $var (in which case the value would be '' like in your empty form) or having some kind of "toggle" which sniffs if you wish to print old values or have empty ones like:
$echo ($want_empty_form ? '' : $var);
... in which case you somehow, with a GET or POST parameter perhaps, tell the form at load time you want a empty form, leading to...
$want_empty_form = true;
... and thus the form fields to being empty.
You want to reset all form values to their initial values? What's wrong with this:
<button type="reset">Reset</button>
Or, if you want to party like it's 1999:
<input type="reset" value="Reset">
As no one answered this question, I assume, as I conjectured in the question, that this is not possible. My solution is to circumvent this problem by having two forms. Since you can recall DB values with this line-
value="<?php echo $var;?>"
I created another form almost exactly as the edit form minus the echo info:
value=""
so that I end up with two forms-the "edit" one with echo which is used to correct mistakes, and a "new" form without the echo instruction. So, one form recalls the DB values, the other doesn't. Keep in mind both submit to the table with the same ID, since I want the latest values, and older ones of more than 5 gens become irrelevant. Hope this is useful to anyone with similar problems. Post questions and maybe I'll be able to answer them.
EDIT: Also thanks to #miken32's suggestion, the following article does the trick, but my solution, though not 100% addressing the issues in initial post, is the best for my purposes:
How can I use a reset button for php form?

PHP: Can I submit a form within a form using AJAX?

I am modifying an existing plugin, and it has a form. The perfect place for me to add my code is at the end of the form but before the submit button. I want to add a form that will allow users to enter their credit card info, but nesting my form within the plugin's form is causing problems.
I was wondering if it would be possible for my form nesting to somehow work with AJAX. So basically, I just need 4 input areas (CC#, Exp date, CCV, amount) to be submitted that to Braintree's servers. I need to maintain PCI compliance with anything I do, so is this possible? Is it recommended? If not, what is?
EDIT - I found a question on here that made me wonder if it would be possible to separate the 2 forms but use CSS to make it look like my screenshot. Below is a quote from one of the question's answers.
Why not place the input inside the form, but use CSS to position it elsewhere on the page?
Update - I'm still confused...
It is against the standards to do nested forms like you are thinking. (See this question for more about that: Can you nest html forms?)
That doesn't mean that you can't have the form send data to multiple locations on submit. Register a submit handler for the form with two ajax methods. The first takes the four pieces of data and sends them to your server. The second grabs the rest of the data and sends it to the location specified by the form.

Combining 2 form actions into one form

I currently have two forms doing two separate actions.
One form subscribes a user to a mailchimp list. The other allows a user to submit their cv.
However I have two submit buttons, one for each action. I want to condense this down to just one submit.
Here is my jsfiddle: http://jsfiddle.net/GSVY8/
.
The end result should be that on submission the CV should be sent to me, and then the form for mailchimp should be submitted.
Any ideas would be much appreciated.
Cheers, Matt
You are using two different actions, so you need to different forms! Without the tag, the input button is useless anyway!
Validate the form in both client-side and server-side
So just wrap each submit button in a different form with a different action/validation and you're done!

Submit multiple forms to same action page

Sorry if this one has been answered somewhere else but couldn't quite find the fool proof answer I need.
I have a update form page that contains a number of iframes, each iframe contains another form. I need to be able to hit submit on the parent page and as well as submitting the parents form fields but also the iframes form fields all to the same php script.
Looks like javascript is the way to go but I am a complete beginner with javascript so need some extremely dumbed down help and how to.

Can I share data between forms on HTML pages? Or can submitting one form, submit another?

I wonder if anyone can advise? I have a JSP which contains two forms, both of which are mapped to the same servlet on the server.
Everything seems fine although when I submit one form, a necessary piece of data entered on the other form is not being submitted at the same time.
The first form is used to add or delete the address of an RSS feed. As there may be several addresses on the page, a table is used to store them. Each cell of the table contains a form like this for deletion:
<form action = "<c:url value = '/deleteRSSFeed?rssFeedURL=${rssFeedURL}' />" method = "post">
<input type = "image" src = "${imageFileURL}myApp_rightArrow.png" />
<input name = "writeWordcloud" type = "hidden" value = "true" />
</form>
And there is another form for adding a feed.
The situation is that upon submission of either of these forms, a wordcloud must be redrawn on the page. But the wordcloud's settings are contained in the other form.
As it not possible for me to merge the forms, can anyone tell me if I can share data betwen forms in HTML? Or better yet, is it possible to submit one form, and have this action submit the second form?
At this point, it is not practical for me to have the forms served by different servlets.
Thanks
Mr Morgan
By using Javascript (or better, jQuery), you can have hidden fields in one form get the other form's fields values before submitting.
Update
This fiddle shows the code you need: http://jsfiddle.net/MqsK8/1/
You should probably use a single form, and have the server-side script perform both actions.
Depending on your scenario, you may want to have two submit buttons for different tasks; the server can check which button was clicked.
It would if you can provide detail of what you are trying to do. You can use Javascript to add elements to DOM but again all of this will depend on your use-case.

Categories