Two submit buttons - perform cgi on second - php

I am working on a form that allows the user to edit, add, and remove committees. In the edit section, I wrote php code that allows the user to select (from a drop down populated by a csv) the name of the committee and then, when a first submit is clicked, information about that committee automatically displays in the fields of the form.
The problem is that I need cgi action linked to a second submit button at the end of the form, so that once it is clicked, all of the information will be updated. This is the button that actually needs to send in the data. The previous submit button was so the php could get the value of the selected committee and the info about it. The php-autofill feature is to (hopefully) make life easier for the user. The php is very intertwined with the html so having a separate file would be tricky.
My question: can I have two submit buttons in one form if the form has action="something.cgi" and only the second button is supposed to do the action?

You need to use either (A) Use two separate forms with separate action's, or (B) Write a JS handler for the second button to post to a different URL

Related

How do I redirect to different php files depending on what button is pressed in the base php file?

I have a base php file that displays items in a database on an html table. Each row has a remove button. When pressed, it will remove that one item from the database. At the end of the table, there is an add button that once it is pressed will take you to another php file with a form that you can fill out with information to add to the database. I am having two problems. The first is that I am not sure how to determine what remove button is pushed. My second question is how do I move to a different page for the other php file once the add button is clicked? I am not supposed to use AJAX for this. And though I tried using include and isset, I couldn't get them to work properly. Any help, conceptual or code examples would be greatly appreciated.
If you can go to other pages, simply create a form with a hidden input field holding the id of the row and have the remove button be a form submit button.
On the remove button being clicked it will go to the delete php file and redirect back to the table page.
As far as the adding button, instead of using a button just use a link to the add form.
If any of this doesn't work because of requirements you haven't mentioned let me know of any restrictions you have.
For your first problem, just make a form for each individual row of data. that way, you will be able to pass by post the relevant id you want to remove.
Another way would be to create a "button" (not a submit button) and have the relevant onClick="..." script - like redirect to "index?Action=Remove&Id=xxxx"
By making different form for each button, you add button will have its own "action" in the form and you'll be allright!
You either have to generate a new "form" (with proper html) if you need to pass data to your next page. If you only need to redirect depending on which button you press, you can have a onClick="..." event on your button. Make sure not to make a "submit" button :)

POST Data Without Form

Basically I have a bunch of data I get from a database and put onto my page in a table. Right now I have the user type in the name, session, etc. in the table and that is sent as post data into the next PHP page, which I then use to lookup more stuff in the DB and so on and so forth.
Obviously that's not a great user experience; it would be much easier to simply CLICK the item in the table and everything gets sent automatically into the next page.
I'm not sure how I'd go about doing this.
My tables are first and last names for now, so if you click a certain row it should go to the next page sending each cell as data.
EDIT: Some examples:
Traditionally you do this with a form
<form method="post" action="pageDataIsGoingTo.php">
to send data to the next page. However, I don't want to do this with a form; but rather when they click a URL and/or button that sends the data. I can "hide" the data from view I suppose, but I still don't know the function to actually go ahead and do that.
Would I make a javascript button/function that sets something in an invisible form?
You can use invisible/hidden form fields.
That might be your best guess.
Javascript would be a good solution if you wanted an ajax POST call, but you want to load other page.
So hidden form fields are your solution.
Parallel with table data.
You need to embed hidden fields and your visible item row within a form
(so each item row contains also a form & hidden form fields and visible submit button,
which you can style with css)
This presuming that your table contains more items which you can choose to send.
Although I would do this with backbone & jquery and do it all in ajax.

Submitting two different forms from a single submit button in PHP?

How to submit two different forms dat,a each having a submit button, in two different tables in the same database after clicking on the second form's submit button using PHP?
Create the a number of hidden fields in the second form (depending on how many textfields you have in the first one). On the second submit buttone use onclick="return xxx()" where xxx is a function that sets the hidden fields in the second form with the values from the first one. Just remember to return bool from xxx().
PHP runs on the server side and (usually) renders HTML before the user has a chance to interact with it. You can't, as far as I know, connect a single button to two forms with only HTML, hence you can't do it with only PHP. You'll have to use JavaScript, for instance you could use jQuery to intercept the submit event and do whatever you want to do instead of letting the form submit a request as usual.

Preserving data in one form when submitting a second form

I have a page with two forms that is generated with PHP.
The first part contains text boxes, a submit button and a clear button.
The second form is just a button called "Add more text boxes" so the user can add more to his form if he needs to.
The problem is when I click the "Add more rows" which loads another page which changes a value.
This value then affects the original page when it reloads causing more text boxes to get created.
The problem is that I lose all the data that was entered.
Is there any way to preserve the data when the user clicks "Add more rows"?.
Here's a screenshot of my page.
Thanks
If you want to do it without js than you put all in one form. When you click button to add row all entered data will be available in $_POST or $_GET so you can fill form with existing data and add a row when generating new page.
Ideally you should use javascript to dynamically add new rows w/out making new requests to the server and loading new pages. But if you want to keep it javascript free. If it's all the same php script just controlled by conditions, just use the $_POST['variable'] values as the value="$_POST['variable']" in your fields. If it's handled through multiple scripts, you can use a session variable to pass the data from one page to the next.

'Previous' Button in HTML/PHP

I thought hours about that problem but I didn't come to any conclusion. My problem here is that I need a 'Previous' Button added to a form. The user should fill out a formular that is splitted up in 13 parts. Every part is an own formular having a 'Next' button for submitting everything to a database and redirecting to the next page.
How do I integrate a 'Previous' button there? ...
I don't if it might be usefull for you to know that I'm using cakePHP, and well I'm pretty new to it.
Store the POST data of each form and the current form index in your session.
When clicking the back button, open form (currentForm - 1) (if that's a valid form index) and populate the fields with formData[currentForm] (assuming currentForm is now the form the back button redirected to)
The question really is, do you want to store each stage of the formula in a record? or do you want to store every stage of the formula in the "transaction"? The difference here is important. What is your relationship with the user? Do they login? are they anonymous? How do you associate their answers from one form entry to the next? If you store each entry in the database, in some chronological way, then simply populate the previous form with the previously entered values; when they click previous. If you do not store the entries and instead utilize a session to retain values between "next" clicks then populate the "previous" form with those values.
I've coded a similar form in classical ASP, see if you can make it work in CakePHP:
I had a 7 step form, step 2-7 have previous buttons. Each step consists of one asp script. All scripts post back to themself. I check the REQUEST_METHOD upon every invocation of the script to see if it was called by GET method or POST. If POST then data is validated, if validated then it is saved. There are three submit buttons on forms that allows user to choose whether he wants to just save the data, save and move to next step or save and move to previous step. Depending on which button was clicked, the user is "redirected" to the previous/next page. This post specifies how to add and handle the previous/next buttons:
Multiple Submit Buttons — Specifying default button

Categories