Submitting two different forms from a single submit button in PHP? - 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.

Related

Two submit buttons - perform cgi on second

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

One form two submit, should I just check for $_POST boolean?

I've got a form that will be using two submit buttons to post to two different databases.
One to hold permanent information, the other so that they may save the submission and return to it at a later point in time. Normally, I would use $_SERVER["REQUEST_METHOD"] == "POST" when the user hits the submit button, but since there are multiple submit buttons, would I be better off checking each button via if($_POST["button1"]){} and if($_POST["button2"]){}?
Is there a better way to do this? I've tried attaching a click event via jQuery to do this, however, when I get to a certain point, the script actually breaks and I'm not sure why. Regardless of which submit button is pressed, the form calls the same action page.
if($_POST["button1"]=='your-button-value'){}
and/or
if($_POST["button2"]=='your-other-button-value'){}
are best option to manage/handle data while submit from same form.
I don't think you will find anything better than this.

Best method to deleting table row with button inside of form

I wanted some input as to what the best way to handle this would be. I have a submit button and a normal button inside a form. I don't want to do a submit on my delete button for the form. Is setting a link outside of the button to carry over into another file using a $_GET parameter the best way here? Basically, take the GET parameter in the php file and if its true, then do my delete functionality. Is there a better way here?
e.g. <input type="button" value="Delete Item" />
GET requests should be used when accessing data (SELECT). POST requests should be used when modifying data (UPDATE, CREATE, DELETE). i.e. You shouldn't be using a GET request to delete a system resource.
I had a similar thing, and there where two methods I used:
The first was that I used a standard button which had the onclick attribute set to a javascript function that would change the value of a hidden input and would then submit the form.
The second was a submit button, but following this question: Position div box at the end of after ensuing elements, I had the main form submit (that would save) at the beginning of the form, which then appeared at the bottom of the form. This mean't that when if the enter button was pressed, the delete submit wouldn't be clicked (and detected by the script), but the main submit would be.

How to submit part of inputs from a form to another domain?

When user create a post, inside the post form, I want to provide a button. When user click the button, it will launch an iframe and submit some of the inputs of the post form. When nested form is not valid, how can I submit those inputs to the other website?
Two methods, Javascript, and PHP
Javascript:
You can use jQuery Post to do this by javascript.
Basic steps:
Use an "onsubmit" handler for the form
In the function for handling the submit, build a data array with data you want to submit and send to the second form.
Either return true (to allow the programmed submit to continue) or build a secondary submit if required.
This one allows you to build the validation you refer to client-side (i.e. in the submit handler).
PHP
Receive the data in the php form, then use CURL in the receiving PHP script to pass data as needed to the second form.

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.

Categories