Hello Developers! Linking a submit button to another page using PHP is very simple, you will need to add the “action” attribute to your HTML form. Let’s see how you can do it briefly within this article.
Table of Contents
Method 1: Link Submit Button to another Page in PHP
The first and simple method of linking your form to another PHP page would be using the action attribute within your form tag. You will need to add the “action” to your form tag and then within that attribute, you will need to add the PHP page that you want to link to. For example, If I have a page called form.php and I want the form submission to link to submissions.php with the form data then I would add the action attribute to the HTML form with the value of submissions.php. And when someone submits the form in form.php, they would be redirected to submissions.php with the form data.
Now let’s see how we can link submit button to another page using the action attribute in the HTML form tag.
The first step would be creating a basic form of course. So let’s create a form assuming that this form is in the form.php page.
This is just an example of a form, you can add your own inputs such as email fields, radios, passwords etc.
But now we need to add a few things to make this form link to another PHP page. Let’s see that in step 2.
Now we have a basic form, let’s add the action attribute to the form and a few more tweaks such as adding the form type and adding a name to the submit button so that we can verify if this particular form has been submitted on the submissions.php page.
So a few changes have been in this new form, we add the action attribute (feel free to change that to whatever page you want the form to be redirected to) and we have added the form type to “post” and also add the name attribute to the submit button, it’s important that you choose a unique name for this particular form so that it does not conflict with any other form within the same page.
With the code above, the form is already being redirected to another page (submissions.php) but now we need to capture the form data on the submission page so that you can run your dynamic codes for that form. And to do that we need to create the submissions.php page, of course, so create that in the next step.
Now it’s time to create the submissions page that the form is redirecting to. It can be any page where you want the form to be redirected, however, in our case, it’s submissions.php if you want to redirect the form to any other page you just need to change the action attribute in your HTML form from the codes above.
So within the page that you want the form to be redirected to (in our case submissions.php), you need to capture the form submit. So let’s see how to capture the form data being redirected from another page (form.php).
By using the PHP codes above we are capturing the post data from our form.php page.
And with this, the first method of linking submit button to another PHP page is done.
So let’s start with the basic form that you might have.
Now we need to make some tweaks to the form so that when someone submits the form, it should redirect to another page. So let’s add the “onsubmit” attribute to our form.
With the code above, whenever someone submits the form, it will redirect them to the developerwings.com website. With this approach, you can’t process the form data. You can only redirect users to another page whenever someone submits the form. However, there are ways that you can process data, you can use Ajax to process the form data and then redirect using the same codes above.
And with that being said, it’s a wrap!