Reputation: 135
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
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
Reputation: 7715
Use
action="<?=$_SERVER['PHP_SELF']?>"
$_SERVER
is a reserved variable -- see more here.
Upvotes: 2
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