Drop down menu with redirection not working

can anybody help with this not redirecting to the page… im the beginner so it is not obvious for me as it is for other ppl
any feedback appreciated

<?php include 'connection.php';
					$sql= "SELECT DISTINCT Title FROM movies";
$result = mysqli_query($con,$sql);
echo "<strong>BY TITLE:</strong><br><br>";	  
echo "<select name='Title'>";
while ($row= mysqli_fetch_array($result))
{
   	  echo"<option value'" .$row['Title']."'>" .$row['Title'] ."</option><br>";
}
echo "</select>";
echo "<label for='GO'></label><input type='submit' name= 'go' value ='Read more'/><br>";
if(isset($_POST['go'])) 
	{
		$sql="SELECT Page FROM movies WHERE Title='$_POST[Title]'";
			$result=mysqli_query($con,$sql);
			while($row=mysqli_fetch_array($result))
						{			
						header('Location: $row[Page]'); 
						}
	}
	?>	

i have also tried with redirection to google.com

I don’t see any html form element in there. Is that actually on the page and not included in your code snippet?
I’m also curious why you are using a form for this, rather than just a list of links.

i echod out the drop down menu
can you not see it?

cinema.webdesignmoreno.co.uk it is SEARCH UNDER TITLE PART

I can’t find the search title box.

I see the drop-down (select) code, but not anything like this:-

<form method="post" action="myScript.php">
    ...
</form>

Should you have quotes around Page?

1 Like
Change
header('Location: $row[Page]'); 
To
$page =  $row['Page']; die('Redirect to: ' . $page);
header("Location: $page"); 
exit();

The die statement will confirm that you are getting the url correctly. I just kind of scanned the other code. I suspect you may have other issues as well. Consider adding die statements to help trace the code. Then remove the die.

Note the double quotes in the header function. Single quotes do not cause variables to be expanded. Makes a big difference. I also broke out $page to its own variable as using arrays inside of strings has their own rules which I forget at the moment.

The exit following the header is important as well. Once you redirect, your program is done. An explicit exit make it clear.

1 Like

As another aside, you should not be putting post data directly into sql queries.
Sanitise, validate, use prepared statements.

thank you for your answer…
unfortunatelly it is not working…
it shows redirect to: angry.php
and because of exit the rest of the page disapperared :frowning:

You do understand that when the browser encounters a Location header (aka redirect) the browser stops processing the current page and moves to whatever page the location tells it to. By definition, there is never anything after a redirect and hence there is nothing to disappear.

Perhaps this might help: http://symfony.com/doc/current/introduction/http_fundamentals.html

OK, I did find it this time (after scrolling down) and can confirm that the form element is there…
…but; it is not alone.
There are two search forms, and both have the same submit buttons named “go”.
so where you have this condition for the search by title:-

if(isset($_POST['go'])) {...}

How does it distinguish between “go” from the title form or “go” from the date form?
And I’m speculating (having not seen all the source php code) that you may have that same condition repeated again at the date search form. Now that’s going to confuse things. :smile:

You need to identify which form you are dealing with.

if ($_SERVER['REQUEST_METHOD'] == 'POST') { 
    if(isset($_POST['Title'])) {
        // Do stuff for the title search
    }
    elseif(isset($_POST['Date'])) {
        // Do stuff for the date search
    }
}

yes i know what you mean
but with this script what happens is it is not redirecting, stays on the same page… under form is PRINTED redirect to: angry.php and the rest of the page is not visible… so header (location) is not working

It won’t do.
A header cannot come after any html output.

Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP.
http://php.net/manual/en/function.header.php

You must put your form processing at the beginning of the page/script.
Or go the whole way to doing things properly and separate your php processing from your html altogether.

1 Like

Alternatively, things could be a whole lot simpler.

<div class="search">
    <h2>Search by Title</h2>
    <ul>
        <?php while ($row= mysqli_fetch_array($titles)) : ?>
        <li><a href="<?php echo $row['Page'] ?>"><?php echo $row['Title'] ?></a></li>
        <?php endwhile ?>
    </ul>
</div>

Then use css to make it a drop-down, like you would for ordinary navigation.

You only really need a search form if people are typing in keywords or something.

1 Like

thank you very much for your help

If you still want to go with the form method. The page could start something like this.

<?php    // This is line 1 of the document, there is nothing before this.
if ($_SERVER['REQUEST_METHOD'] == 'POST') { 
    if(isset($_POST['Title'])) {
        // Do stuff for the title search
    }
    elseif(isset($_POST['Date'])) {
        // Do stuff for the date search
    }
}

// get stuff from the database ready for making your lists further down.

//   Now the processing bit is out of the way, you can start printing html
?>
<!DOCTYPE html>
<html>
    <head>
    ect...

as i said im beginner and it is college project…we have booking form with drop down…i only wanted do extra drop down with search…never actually thought about list of links…which seems good idea
thanks

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.