1

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=" " ?

1
  • Leave it out completely and check for post data to determine if it was a submission or not Commented May 18, 2014 at 20:14

3 Answers 3

2

Use

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

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

Sign up to request clarification or add additional context in comments.

2 Comments

That works just fine but what if I have 3 forms with 3 different php scripts on that page? How will action="<?=$_SERVER['PHP_SELF']?>" know what to call?
You'll need to give the <submit> tags of each form a different name attribute. Then in the php script check which one has been sent by using isset. E.g. <?php if (isset($_POST['first_script_submit'])) { [code for first script] }. And so on.
2

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>

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.