Hello, I've got a problem with get data.

To display a form I use GET data, visiting the url: page.php?a=b . When entering the page there is a form with POST method: <form method="post"> The problem is, that when I submit this form, the get data a=b still remains in the url (after submitting post form)! I have tried to set action="page.php" , but the problem still remains. I want the action to be set to the very same page, however without the get data variables remaining in the address bar.

Member Avatar for langsor

I cannot reproduce your situation in either IE7 or FF3 -- here is the last of several ways I tried

Calling page code:

<?php
header( 'Location: test.php?junk=ford' );
?>

test.php

<html>
<head>
</head>
<body>
<form action="test.php" method="POST">
  <input type="submit" value="POST" />
</form>
</body>
</html>

When I hit the POST submit button the GET data goes away -- what are you doing here that is not apparent?

If you just don't want the GET values passed to your POST php script, use the specific $_GET/$_POST superglobals ... but I still don't see the GET data getting through after a form POST.

This code illustrates the problem:

<html>
<body>

<?
echo 'Post data: ' . $_POST['test'] . '<br>';
echo 'Get data: ' . $_GET['test'] . '<br><br>';
?>

<form method="post">
        <input name="test">
        <input type="submit">
</form>

<a href="?test=hi">This page</a>

</body>
</html>

The problem happens when you do the following:
- Click the link 'This Page'
- The get data is now set as it should
- Enter a value for the post data and submit
- The post data is now set as it should BUT: the get data still remains as it should NOT!

try this:

<html>
<body>

<?
echo 'Post data: ' . $_POST['test'] . '<br>';
echo 'Get data: ' . $_GET['test'] . '<br><br>';
//header("location:pageurl.php");
?>

<form method="post" action="pageurl.php">
        <input name="test">
        <input type="submit">
</form>

<a href="pageurl.php?test=hi">This page</a>

</body>
</html>

not even this works:

<?
header("location:index.php");
?>

<html>
<body>

<?

echo 'Post data: ' . $_POST['test'] . '<br>';
echo 'Get data: ' . $_GET['test'] . '<br><br>';
?>

<form method="post">
	<input name="test">
	<input type="submit">
</form>

<a href="index.php?test=hi">This page</a>

</body>
</html>

Firefox (2.0.0.16) returns:
The page isn't redirecting properly
Firefox has detected that the server is redirecting the request for this address in a way that will never complete.
* This problem can sometimes be caused by disabling or refusing to accept
cookies.

In opera 9.50 nor the post data neither the get data are retreived (they disappear).

I would suggest doing something like the following:
Create a new page (such as process.php) and put the following code into it:

<?
session_start();
//Store the POST and GET data into the session
$_SESSION['post_data'] = $_POST['test'];
$_SESSION['get_data'] = $_GET['test'];

//Redirect to the correct page
header("Location: index.php?test=hi");
?>

This will register the values to a session, and then because you are navigating away from the page with the header redirect you the POST data will be deleted automatically.

You will then need to add session_start() to the top of index.php and change

echo 'Post data: ' . $_POST['test'] . '<br>';

and

echo 'Get data: ' . $_GET['test'] . '<br>';

to

echo 'Post data: ' . $_SESSION['post_data'] . '<br>';

and

echo 'Get data: ' . $_SESSION['get_data'] . '<br>';

respectively.

Thanks for your detailed reply.

However the project I'm working on is quite complex, and I wouldn't want to make yet another page only for getting rid of the get data. Isn't it strange that the form acts as if sending both old get data and new post data? Seems like a bug? But why then is it the same in different browsers? Also, even if I set action="index.php" it still keeps the old get data, as if some attached and dependent fool!

Some other solution or explanation to this whole drama?

:S

The POST data is cached by the browser, which is why in most cases when you refresh a page with POST data the browser will ask if you want to re-submit it (as well as when you go back to the page with the data on).

The other option you can look into is setting a no-cache in the header of the dosument, detailed at http://uk2.php.net/header

Be aware that if there are times that you want the browser to store page details and refer to POST and GET this may mean you need a work-around for that

actually it was really simple. just by adding action='index.php'. i made some mistake earlier thinking it didn't.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.