URL redirect

Hi everyone,

I have a page on my site with a series of links that, when clicked, submit to a page (index.php) located in the same directory as itself, eg.

<li id=“formal”><?php echo “<a href=\”$url_consumer/index.php?subcatID=6&itemTypeID=1\“>Link 1</a>”?></li>

index.php contains only php and basically just queries a database and then loads the information into the current page with the links in it.

This page with all the links in it, also contains a form which when submitted, inserts the information into a database table and is then redirected to the current page:

$WA_redirectURL = “?sent=true”;

On this current page, the first ‘confirm’ paragraph below is supposed to be shown:

<?php if(isset($_GET[‘sent’]) && $_GET[‘sent’] == “true”) { ?> <p id=“confirm” style=“background-color: #ded5b3; color: #00274c; padding: 5px; margin-bottom: 8px;”> <strong>Thank you – your details have been processed. </strong></p><?php } else { ?>
<p>Subscribe to our newsletter.</p>
<?php } ?>

It doesn’t seem to work though because even though it’s submitting to itself, it’s not loading the current page because it needs certain information in the url string to do this (see the abovementioned link). When the submit button is pressed the following is at the end of the url:


Since index.php is the php page that only queries the database the page shown is blank and not the page that was previously seen, before submitting the form.

I just wondered if anyone knows of a workaround for this problem?

Appreciate any advice.

Adjust the form action to include those querystring parameters.

Expanding on what pmw57 has said, I think he means put in hidden form elements.

e.g. <input type=“hidden” name=“sent” value=“true”>

see http://tinyurl.com/y3vj74u

Thanks for the reply,

The problem is that the query string parameters will be different for each link on the page so how would I account for that? I just need it to redirect to the exact same page as it was before submitting the form, ie. the user would be looking at the same information from the database.

Appreciate any further help.

As was said before, adjust the form action to include those querystring parameters.

The form action determines where the form is submitted. When the form action includes querystring parameters, any other form values are added on to that query string.

You can use $_SERVER[‘REQUEST_URI’] to retrieve the URI that was used to access the page, including querystrings.

$thisPage = $_SERVER['REQUEST_URI'];
echo '<form method="get" action="' . $thisPage . '">';

I revised the code and replaced this line:

<form action=“” method=“post” id=“newsletter” name=“newsletter”>

with this:

$thisPage = $_SERVER[‘REQUEST_URI’];
echo ‘<form method=“get” id=“newsletter” name=“newsletter” action="’ . $thisPage . ‘">’;

When I tested though and submitted the form, the end of the url looked like this:


So it’s appending the values put into the input fields and not the correct url parameters.

Do you know what I’m doing wrong?

Have you tried using post?

If I use post then the url ends the following again:


I thought I had to use GET since it looks for the sent=true in order to show the correct paragraph as below:

    &lt;?php if(isset($_GET['sent']) && $_GET['sent'] == "true") { ?&gt; &lt;p id="confirm" style="background-color: #ded5b3; color: #00274c; padding: 5px; margin-bottom: 8px;"&gt; &lt;strong&gt;Thank you – your details have been processed. &lt;/strong&gt;&lt;/p&gt;&lt;?php } else { ?&gt;

<p>Subscribe to our newsletter.</p>
<?php } ?>

Is there a workaround?

There is a work-around, where you also use the hidden form value that mrwooster mentioned, and check for that using $_POST[‘sent’]
Although, normally a more accepted solution is to check for the name of the submit button instead.

Yeah it seems the hidden form value and the POST method is the way to go.

Thank you both for the help.