简体   繁体   中英

how to link a php file to a html file

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?

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

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'];

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM