user140102
user140102

Reputation: 135

Html form and php function on same file

I have a html form in register.html. When submitted, the data is inserted into a database, using the script from register.php.

Since, there are just a few line of code, I would like to put them both in the same file. Right now I have:

<form action="register.php" METHOD="POST">

If I put the form and the php script in the same file, what should I call on action=" " ?

Upvotes: 1

Views: 15661

Answers (3)

user3071296
user3071296

Reputation: 103

You can keep action the name of the file you're calling from. But in the top of the file, make sure to check if the form has been posted or not and then process it before you even get to the head of the page. It'll have to be a php page though, not html.

So it would look something like:

<?php
if (!empty($_POST['whatever']))
{
    //process the database stuff here
}
?>
<html>
<head>
</head>
<body>
</body>
</html>

Upvotes: 2

GluePear
GluePear

Reputation: 7715

Use

action="<?=$_SERVER['PHP_SELF']?>"

$_SERVER is a reserved variable -- see more here.

Upvotes: 2

Patrick
Patrick

Reputation: 3367

action takes the form of a relative or absolute pathing, it doesn't matter where the form is.

You'll need to user the action to the path of the file that catchs the POST. PHP doesn't care it came from the same file :)

Upvotes: 0

Related Questions