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

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'])){.....}

Related

WordPress Custom Page Submit Form

I'm trying to write a simple script to write some data to mysql then read it. My code is working without a problem alone but I'm trying to use it inside a WordPress page, this is the point problem starts.
I have created a template file for WordPress, and using this template to create the page. Page shows up without a problem but whenever I try to submit the form inside it (my custom php form) it forwards me to index.php .
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
<span>Enter Your Code : </span><br/>
<input type="text" name="sha256"><br/>
<p align = right><input type="submit" name="shaSubmit" value="Submit" /></p>
</form>
this is my form (inside custom php), and as you can see it posts the data to itself. At the the start of my custom php code I have
if(isset($_POST['Submit']))
But it doesn't matter, as soon as I click on button, it forward me to domain.com/index.php
Btw, this custom php is on a page with such url domain.com/custompage/
How can make this form work ?
ps. Code above is for reading from mysql.
You're using the following conditional statement if(isset($_POST['Submit'])) along with the submit button's named element name="shaSubmit".
You need to make those match.
Either by changing the name of your submit button to name="Submit"
or by changing your conditional statement to if(isset($_POST['shaSubmit']))
which is why your code is failing because of the conditional statement you've set is relying on a form element named "shaSubmit".
You need to change 2 things.
(1)make action=""
don't need to use action=" echo htmlspecialchars($_SERVER["PHP_SELF"])"
(2)if(isset($_POST['shaSubmit'])){
the code ....
}

How do I rewrite URLs to make them shorter and not show any GET variables in the address bar

I have a link structure on single index.php page such as index.php?lang=eng&theme=dark&page=shop for example. I do need to pick up GET variables from the link to give appropriate content and configuration to each visited page. However I just want to show name of the page such as default.php or I if possible I want to show something like shop.php if the page=shop and chat.php if the page=chat. Can anyone recommend me what technique I should use at all?
If you want to do that, you should use POST rather than GET because it won't show its variables in the URL bar.
In PHP, you can find the value of POST variables by using $_POST['var'] rather than $_GET['var'] but how will you use open them in the first place if they won't show in a URL?
The answer is to use a form which appears to be a normal link, with elements inside it as variables for the PHP POST stuff. Here's an example:
...
<form name="openPage2" method="post" action="path/to/page2.php">
<input type="hidden" name="lang" value="en" />
<input type="submit" value="Page 2"/>
</form>
...
Now, what this will do is quite simple really. Say the current page containing this form is mywebsite.com/index.php. There's a button labelled Page 2 and everything else in the form is hidden. When you click page 2, the browser will go to mywebsite.com/path/to/page2.php without any visible variables in the URL bar. But, we've put hidden inputs into the form which will be submitted as POST when you click Page 2. Meaning, on Page 2, you can do this to get the language:
...
<?php
$lang = $_POST['lang'];
setLang($lang);
?>
...
and $lang will now be "en" because that's the form value. You can put as many hidden inputs as you like and none of them will show up in the URL bar because they are POST, not GET.
Hope that helped!

GET and POST is submitting on same time

I have html page and I have taken one form in it and other link outside the form .Form is Submitted by POST method,when I submitting form first time its ok and when I click link it pass data by GET method and when I again submit form then it send both GET and POST variable i.e form data and link data both.so what is the reason for that and how can I solve it.My html page is below
<html>
<body>
<form method='post'>
<input type=input name='name'/>
<input type=submit name='submit' value='submit'/>
</form>
<a href='check_global.php?page_number=6'>Page Number</a>
</body>
</html>
Because the form hasn't the action attribute, so it simply reload the page. When you submit it the first time it's all fine, but when you do it after clicking the link, the url is 'dirty' due to the data of the link, so you have both GET and POST values.
You can check wether the POST attribute is set ( if(isset($_POST['name'])) with php), in this case it has been submitted with the form
When you submit the form the second time you see the form parameters + the url parameter of the page (remember you clicked the link with the relative URL 'check_global.php?page_number=6').
To verify the above try this:
<?php
echo 'GET param ' . $_GET["page_number"];
echo 'POST param ' . $_POST["name"];
?>
As you can see you can access both types of parameters during a POST request.
Hope that helps.
Just to make the point, the OP did not indicate that the form was supposed to submit to anywhere but the current page. So just for funsies, here is the same basic idea, but with an action attribute value:
<form method="post" action="">
<input type="text" name="name"/>
<input type="submit" name="submit" value="submit"/>
</form>
Page Number
Notice that I've set it up so that, for whatever reason, the link points back to this same page and so does the form. The result:
First Load: form submit makes request with POST data to blah.php
Second Load: link follow makes request with GET (thanks to the query string) to blah.php?page_number=6
Third Load: form submit, using blank action to indicate that current page is where to post, makes request with POST form data to blah.php?page_number=6, thus having both POST form data and GET URL data.
So your options are to either set the action attribute value to blah.php so that it does not include the query string, or to accept that if you want to avoid the various ways of doing this in favor of having a more modular form (drop it in any page and you know it will post to that address), then to simply have the PHP backend check if $_POST['submit'] is set and if so, handle it like a form post and don't use any of the $_GET logic that might be screwing things up.
The link is never sending the form data as POST, and the POST data is not part of the GET array, so you know that when there is no POST, it's just get and if there is POST, it was a form submit, even if there is a GET array.
Or just use separate scripts so you don't get mixed up.

File extension added after form submit

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.

Search MySQL with PHP and display results on the same page

This is definately a novice question, but if you could be of any help i would be very grateful.
Basically, i'm building a database management page, and it of course includes a search function.
So, the search form looks something like this
<form name="name" function="search.php" method="get">
But, whenever i use it, i will of course get redirected to search.php. What i want is a way to display the results on the same page i did the search from (let's say index.php), without having to build an entire identical page around search.php
Thankful for answers.
Use a hidden field in the form that indicates that the form has been submitted.
In your form page (e.g. index.php)
<form name="name" action="index.php" method="post">
{OTHER_FORM_FIELDS}
<input type="hidden" name="doSearch" value="1">
</form>
So in your php code (could be in the index.php page or in a php script included)
<?php
if($_POST['doSearch']==1) {
//query database
//get results
} ?>
in your index.php page
<?php if($_POST['doSearch']) { //a search request was made display my search results ?>
HTML_CODE
<?php } ?>
Let the page submit to itself:
<form name="name" function="index.php" method="get">
In the handler for the page, check whether or not you have parameters and display either the input box or the results as appropriate.
You could even take it one step futher. You could use AJAX to insert the results directly into the page content when the submit button is pressed, rather than causing a page refresh.

Categories