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.
Table of Contents
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.
<form>
<label for="name">Name:</label>
<input type="text" name="name" id="name">
<input type="submit" value="Submit">
</form>
HTMLThis 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.
<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>
HTMLSo 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).
<?php
if(isset($_POST["name_form_submit"])){
$name = $_POST["name"];
// Your dynamic code that you want to execute on the other page
}
?>
PHPBy 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.
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>
HTMLNow 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>
HTMLWe 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>
HTMLWith 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!
Finally, you got a response from all the resume you submitted? Now It's time to…
One of the hardest thing that beginners or even advanced coders is that they can't…
Are you about to build a SaaS platform using Laravel? Well then you are at…
If you are a programmer or a developer, then you might know what coding is.…
If you are struggling with how to install export kit in Figma, then you are…
If you are a regular reader of our blog, then there is no denying the…
This website uses cookies.