Reputation:
Is it possible to link a PHP file to an HTML file?
I have a form in HTML code, and I want the data from the form to be processed by the PHP code I have written and to be stored in a database. The code is ready and when I have the HTML code and PHP code in a single PHP file it works fine. But what I need is to have two separate files; one in HTML and one in PHP. How can I do that?
Upvotes: 0
Views: 1181
Reputation: 1118
In addition to the answer by "Don't Panic" dont forget to set the method attribute in your form tag to match your php code most likely it is POS or GET
<form action="something.php" method="POST">
or
<form action="something.php" metod="GET">
and you you can omit the method="GET" since it is the default method. It worth mentioning that POST is more secure and can carry more data since it uses the actual request body unlike GET that appends the parameters to the url.
Upvotes: 0
Reputation: 433
index.html
<form action="some.php" method="POST">
<input type="text" name="inp">
<input type="submit" name="submit" value="submit">
</form>
some.php
echo $_POST['inp'];
Upvotes: 0
Reputation: 41820
If you have the form and the PHP code on the same page and it works, then your opening form tag either doesn't have an action
attribute, has a blank action
attribute, or has an action
attribute explicitly set to the URI of the same page that contains the form.
All you need to do is change that so that the form submits to the separate PHP script instead.
<form action="something.php"> ...
(Of course, you'll also need to move your PHP code to something.php
. And obviously "something" is just for example. I'm sure you have some sort of meaningful name you'll be using instead.)
Upvotes: 1