Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using HTML form action with a php script being in another directory (relative paths)

Tags:

html

php

apache

Here's what my diretory tree looks like

/application
    /lib
    /util
        /login
    /views
        /base_view

My login page is

localhost:737/astuto-lena/branches/application/views/base_view/index.php

And I want the action of my form to be this

localhost:737/astuto-lena/branches/application/util/login/main.php

Here's my form declaration

<form class="form_login" action="./util/login/main.php" method="POST">
...
</form>

But when I click the submit button, it takes me to

localhost:737/astuto-lena/branches/application/views/base_view/util/login/main.php

Which is the wrong path and generates a Error 404.

So what's wrong with the way I'm using relative paths in my form declaration and how can I fix that?

like image 525
Flayshon Avatar asked Jun 06 '13 16:06

Flayshon


People also ask

Can form have two actions PHP?

No, a form has only one action.

Which method is used to pass data from HTML to PHP?

PHP $_POST is a PHP super global variable which is used to collect form data after submitting an HTML form with method="post". $_POST is also widely used to pass variables.

What is action in form control in PHP?

The action attribute defines the action to be performed when the form is submitted. Usually, the form data is sent to a file on the server when the user clicks on the submit button. In the example below, the form data is sent to a file called "action_page.php".

How do I create an action on the same page in PHP?

In order to stay on the same page on submit you can leave action empty ( action="" ) into the form tag, or leave it out altogether. For the message, create a variable ( $message = "Success! You entered: ". $input;" ) and then echo the variable at the place in the page where you want the message to appear with <?


1 Answers

In your relative path ./util/login/main.php, you're using ./ which refers to the current folder, so it assumes that the folder structure /util/login is inside /base_view. You should try using ../ which refers to the parent folder:

<form class="form_login" action="../../util/login/main.php" method="POST">
...
</form>
like image 58
DarkAjax Avatar answered Sep 18 '22 09:09

DarkAjax