Categories: How toPHP

How to Link Submit Button to Another Page in PHP

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.

There are several methods to link submit button to another page in PHP including using the HTML form, and Javascript as well. We will go through them from simple ways to advance and better ways to link submit button to another page in PHP.

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.

Step 1

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.

<form>
  <label for="name">Name:</label>
  <input type="text" name="name" id="name">
  <input type="submit" value="Submit">
</form>
HTML

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.

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.

<form action="submissions.php" type="post">
  <label for="name">Name:</label>
  <input type="text" name="name" id="name">
  <input type="submit" value="Submit" name="name_form_submit">
</form>
HTML

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.

Step 3

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).

<?php

if(isset($_POST["name_form_submit"])){
  $name = $_POST["name"];
  
  // Your dynamic code that you want to execute on the other page
}

?>
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.

Method 2: Redirect to another PHP page on the form submit using JavaScript.

Within this method of linking your submit button to another PHP page, we are going to use JavaScript.

So let’s start with the basic form that you might have.

<form>
  <label for="name">Name:</label>
  <input type="text" name="name" id="name">
  <input type="submit" value="Submit">
</form>
HTML

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.

<form onsubmit="formSubmit()">
  <label for="name">Name:</label>
  <input type="text" name="name" id="name">
  <input type="submit" value="Submit">
</form>
HTML

We added the “onsubmit” attribute to our form and added a JavaScript function as the value but we have not created the JavaScript function, so let’s create the function that will handle the submission event of the form.

<form onsubmit="formSubmit()">
  <label for="name">Name:</label>
  <input type="text" name="name" id="name">
  <input type="submit" value="Submit">
</form>
<script>
function formSubmit(){
  window.location.href = "https://developerwings.com";
}
</script>
HTML

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!

Happy Coding!

Kachkol Asa

Zubair Baloch/Kachkol Asa is a full-stack web developer with expertise in PHP, Python, and freelancing.

Recent Posts

5 Tips to prepare for a coding interview

Finally, you got a response from all the resume you submitted? Now It's time to…

8 months ago

5 Tips to improve your logic building in Programming

One of the hardest thing that beginners or even advanced coders is that they can't…

8 months ago

Complete Guide for Building a SaaS Platform using Laravel

Are you about to build a SaaS platform using Laravel? Well then you are at…

8 months ago

Coding for Dummies

If you are a programmer or a developer, then you might know what coding is.…

2 years ago

How to install export kit in Figma

If you are struggling with how to install export kit in Figma, then you are…

2 years ago

A Byte of Python Book: A must-read

If you are a regular reader of our blog, then there is no denying the…

2 years ago

This website uses cookies.