Jump to content

URL question


nicolette

Recommended Posts

hi i'm new to php so if you would please help me out that would be great!

 

I need to make a form with a list box that will contain search engines when the user selects a site that site will open in a new window.

 

I know how to make the basic form but for the life of me i can't get it to pass the url of the search engine

 

I'm sure this is really easy but i just can't figure it out

 

thanks

Link to comment
https://forums.phpfreaks.com/topic/206333-url-question/
Share on other sites

<form method="post">
Choose Your Website:
<select name="url">
	<option value="http://google.com">Google</option>
	<option value="http://yahoo.com">Yahoo</option>
	<option value="http://bing.com">Bing</option>
</select>
<input type="submit" name="submit" value="Go to Search Engine">
</form>

<?php
if(isset($_POST['submit'])){
$urls = array('http://google.com','http://yahoo.com','http://bing.com');
if(in_array($_POST['url'],$urls)){
	header("Location: " . $_POST['url']);
}else {
	echo "<p>Invalid Search Engine!</p>";
}
}
?>

Link to comment
https://forums.phpfreaks.com/topic/206333-url-question/#findComment-1079369
Share on other sites

Hi thanks, I am getting a header error here is the code you gave me edited to my pre-existing form

 

<HTML>
<head>
<title> Final Part 2</title>
<body>
<h2>Please Select a search engine.</h2>
<form method="POST"target="_blank">
<table>
<tr>
<td>Sites: </td>
<td>
<select name="url">
<option value="http://yahoo.com">Yahoo.com</option>
<option value="http://google.com">Google.com</option>
<option value="http://ask.com">Ask.com</option>
<option value="http://lycos.com">Lycos.com</option>
<option value="http://bing.com">Bing</option>
</select>
</td>
</tr>
<tr>
<td><input type="submit" name="SubmitPart2" value="<<< Go To" /></td>
</tr>
</table>
</form>
<?php
if(isset($_POST['SubmitPart2']))
{	
	$urls = array('http://yahoo.com','http://google.com','http://ask.com','http://lycos.com','http://bing.com');
		if(in_array($_POST['url'],$urls))
			{		
				header("Location: " . $_POST['url']);	
			}
				else 
					{
						echo "<p>Invalid Search Engine!</p>";
					}
}			
?>
</body>

Link to comment
https://forums.phpfreaks.com/topic/206333-url-question/#findComment-1079379
Share on other sites

Just throw that code to the top.

 

try this

 

<?php
if(isset($_POST['SubmitPart2']))
   {   
      $urls = array('http://yahoo.com','http://google.com','http://ask.com','http://lycos.com','http://bing.com');
         if(in_array($_POST['url'],$urls))
            {      
               header("Location: " . $_POST['url']);   
            }
               else 
                  {
                     $a = "<p>Invalid Search Engine!</p>";
                  }
   }         
?>
<HTML>
<head>
<title> Final Part 2</title>
<body>
<h2>Please Select a search engine.</h2>
<?php echo $a; ?>
<form method="POST"target="_blank">
<table>
<tr>
<td>Sites: </td>
<td>
<select name="url">
<option value="http://yahoo.com">Yahoo.com</option>
<option value="http://google.com">Google.com</option>
<option value="http://ask.com">Ask.com</option>
<option value="http://lycos.com">Lycos.com</option>
<option value="http://bing.com">Bing</option>
</select>
</td>
</tr>
<tr>
<td><input type="submit" name="SubmitPart2" value="<<< Go To" /></td>
</tr>
</table>
</form>
</body>

Link to comment
https://forums.phpfreaks.com/topic/206333-url-question/#findComment-1079383
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.