File extension added after form submit - php

Let's say I have a form on my website homepage: www.mysite.com
Now, the form tag looks like this:
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
...
</form>
When that form submits, it does everything it's suppose to. But it also reloads the page with the full filename and extension. So the user will now find the URL in the address bar is: www.mysite/index.php
Is there a way to make the form fire to the same page without adding this extension?
There may be situations where the form is as an include in a footer, so I cann't be specific about the page the form needs to fire to, hense the PHP_SELF code.

That's because $_SERVER['PHP_SELF'] refers to the actual filename of the current script.
Try this:
<form action="" method="post">
An empty action will post back to the current URL.

Try setting the action to #:
<form action="#" method="post">
...
</form>
The # refers to the current page.

Try changing action to #, this post to the current page.
Edit: Mike beat me to it.
Edit 2: It looks like you can leave out the action all together and it will default to the same page.
Edit 3: Mike beat me to that one too.

Related

Submit button redirecting to different address

So the submit button of this form is supposed to send the data to localhost/mvc/contact/test and redirect to localhost/mvc/contact
at the first click of submit button it works well
Right
but when I click the submit button again its taking me to localhost/mvc/test
Not right
here is the html code of the form <form action="test" method="post" accept-charset="utf-8" class="contact">
when I had this as <form action="contact/test" method="post" accept-charset="utf-8" class="contact"> it first redirected me to localhost/mvc/contact but on second attempt redirected to localhost/mvc/contact/contact/test
Btw it's a simple mvc framework you may check it out on github for more clarity on Leggera # Github
The above mentioned code is present on view/contact/index.php
You can try these things:
You must give the full path to the file like <form action="contact/test/index.html" method="post" accept-charset="utf-8" class="contact">
In the index file of contact/test you can use <?php header("Location: http://www.example.com/");?> for redirection.
Maybe you have given wrong paths to files.
You must give more details for more specific answer

After Using Get Method , Clean query strings in Adress Bar,

My php code in one page
<?php
if(isset($_POST['gonder'])){....}
if(isset($_GET['ozelid'])){.....}
?>
<input type="submit" name="gonder">
<td> <a href=\"?ozelid=YERTUTUCU&fiyat=$haskur\" onclick=\"return confirm('Tahsil Edilsin?')\" >Tahsil</a>
First time after the page loads ,when I click submit, it is okay and it only runs if(isset($_POST['gonder'])){....}
Again first time after the page loads , when I click the link then it only runs
if(isset($_GET['ozelid'])){.....}
However, after the page loads, when click the link and then I click the submit button both if(isset($_POST['gonder'])){....} and if(isset($_GET['ozelid'])){.....} run, which is not desired.
If you only want one of these being called at a time, then I would suggest just using an ElseIf.
<?php
if(isset($_POST['gonder'])){....}
Elseif(isset($_GET['ozelid'])){.....}
?>
You shoud check your form tag:
If you are using <form action="#" or keep the action attribute empty, the form is sent to the current page (in your case it's the page having the GET parameters).
Please check if you have the possibilty to set the action attribute of your form tag to something like this:
<form action="index.php" method="post">
You have to replace index.php with the name of your php file.
After you processed the request you can send location header. Like so:
header("Location: http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]");
die();
To your current url without get parameters. I guess this should be in your code in this block:
if(isset($_GET['ozelid'])){.....}

404 error when submitting form

I keep getting a 404 error when trying to submit this form. In my website directory i have a folder called mobile, which inside has forms.php and process.php.
The form is located on this page
http://localhost/mobile/forms.php?ft=0&id=2
here is the form
<form action='/mobile/process.php?o=9&ft=0' method='POST'>
//details
</form>
When i try to submit i get a 404 error, when it should go to http://localhost/mobile/process.php?o=9&ft=0? How do i fix this?
By looking at the URL's what I conclude is that both the php files are on the same page so change your action url from
<form action='/mobile/process.php?o=9&ft=0' method='POST'>
To
<form action='process.php?o=9&ft=0' method='POST'>
A forward slash before the mobile means it selects the folder from the root.. So you probably don't need that here.
Last but not the least, be sure the file names are proper, and also make sure the cases are same, because the file names are case sensitive.
Note: You may also get 404 error if you are using header('Location:
xyz.php'); on the form processing page, for redirecting the user to
some page after the form processes and you may encounter 404 because
the page on which the script has redirected doesn't exist. so make sure
that the URL is correct if you are using header()
Try changing
<form action='/mobile/process.php?o=9&ft=0' method='POST'>
to
<form action='process.php?o=9&ft=0' method='POST'>
Since they are in the same directory.
You can't pass a GET parameter in the form action.
In order to pass parameters, you will have to create a hidden input like :
<input type="hidden" name="o">

Insert to MySQL using HTML form

I would like to use the data entered in the html form and insert into MYSQL. I have a separate php (cus.php). but nothing is happening with current code I have
at the moment when I click "register" I'm nav to the php file. Thank you
You forgot the most important thing about an HTML form: the <form> tag.
You have to wrap the whole form (all inputs) which should be submitted when clicking like this:
<form method="POST" action="cus.php">
...
</form>
This will send all inputs to cus.php and make them available there as variables $_POST['input_name']. You can alternatively use GET as the method (and then use the $_GET array instead).
Edit: Didn't see it, you actually do have a form tag. However it's missing the target file in its action attribute.
First, see DCoder's comment. It's the most important.
Change:
<form action="" method="seller">
To:
<form action="cus.php" method="post">
Does that fix it?
Try to do this
FORM action="your script" method="Post"

Should I always set the "action" attribute?

I've seen <form> opening tags that look like this:
<form action="<?= $_SERVER['REQUEST_URI'] ?>">
Does the action attribute here make any sense?
Wouldn't the form behave the same way without it?
You should always include the action attribute in your form tag if you want a good valid markup (which you should). It is a required attribute (though most browsers will work around it if you don't and assume action="").
Using:
<form action="" method="post">
...will work and just use the current page as the action page.
http://www.w3schools.com/tags/tag_form.asp
Hope this helps.
If you set it to blank you get the same effect (which is what I prefer)
<form action="" method="post"> ....
Yes, you have to include action attribute within <form> tag. See some documentation.
However, you do not need to pass current URI, you can add empty action attribute like that:
<form action="">
...
</form>
and then the form will be sent to the current location (current URI).

Categories