PDA

View Full Version : Open new window URL, variable from form in URL PHP



squaredvision
02-23-2009, 07:44 PM
Hi how are you?


<form name="form" target="_new" method="POST" action="mp3search.php">
Song or Artist: <input name="mp3search" type="text" />
<input type="submit" value="Search" />
</form>

This is a simple form. I want whatever the user entered in the text box to be concatenated into the URL in a new window when the user hits Submit:

EX:
This will open up as a new window showing google.com
http://www.google.com/search?(VARIABLE)<--this is the variable from the textbox.

so for example it would be: www.google.com/search?VARIABLE+fileextension

best,
Michael

Schmoopy
02-23-2009, 08:25 PM
Hi there, to achieve this, you will need to be using the method="get", so that google can get the query from the url. Using post would result in the data not being sent through the url bar and so google wouldn't be able to produce a result.

Here is the code:

search.html



<html>

<head>
<title>Google Search</title>
</head>

<body>

<form name="form" target="_new" method="get" action="mp3search.php">
Song or Artist: <input name="mp3search" type="text" />
<input type="submit" name="submit" value="Search" />
</form>



</body>

</html>


mp3search.php



<?php

if (isset($_GET['mp3search']))
{
$search = $_GET['mp3search'];

echo "<meta http-equiv=\"Refresh\" content=\"0; url=http://www.google.com/search?q=" . $search . "\">";
}
else
{
echo "You need to enter a search term";
}

?>

squaredvision
02-23-2009, 09:20 PM
http://img3.imageshack.us/img3/4231/36452236.jpg

this is what i get for the outcome. it doesn't take me to google.com

my firefox editor i think shows a break somewhere?

thank you for your help on this.

Schmoopy
02-23-2009, 09:23 PM
You're running the file straight from the source "C:/users/blabla". You need to either host it with your server provider or download WAMP (http://www.wampserver.com/en/) (Windows only) or similar.

Then execute the script through a local server. The reason it isn't working for you at the moment is because there is nothing there to interpret the PHP, just the HTML

squaredvision
02-23-2009, 09:44 PM
http://www.squaredvision.com/search/search.htm what do you think ? :D

Schmoopy
02-23-2009, 09:46 PM
:) Glad you got it working.

squaredvision
02-23-2009, 09:58 PM
what is isset?

Schmoopy
02-23-2009, 10:03 PM
isset (http://uk.php.net/manual/en/function.isset.php)

That site's a great resource, just put any function you want into the search bar and it tells you how to use it :)

squaredvision
03-01-2009, 01:26 AM
Hi Schmoopy,

I put checkboxes in my form so users can choose what they want to search. How do I implement it so that if a user chooses the mp3 it will do the mp3 and if the user chooses MP3 WAV and WMA it goes in the URL and search those



echo "<meta http-equiv=\"Refresh\" content=\"0; url=http://www.google.com/search?hl=en&q=-inurl%3A(htm|html|php)+intitle%3A%22index+of%22+%2B%22last+modified%22+%2B%22parent+directory%22+%2Bdescription+%2Bsize+%2B(.mp3|.wma|.wav)+%22". $search ."%22\">";


http://www.squaredvision.com/search/search.htm

thank you,
Michael

Schmoopy
03-03-2009, 11:11 AM
Ok, so do you just want it to add the extension of the file?

So if I searched for "Through the fire and the flames", it would add whatever button I checked to the end? Like "Through the fire and the flames.mp3"

Tried and tested, but using checkboxes would be a little bit more complicated:



<?php

if(isset($_GET['extension']))
{
$extension = "." . $_GET['extension'];
}
else
{
die("You need to select a format");
}

if (isset($_GET['mp3search']) && $_GET['mp3search'] != "")
{
$search = $_GET['mp3search'];

echo "<meta http-equiv=\"Refresh\" content=\"0; url=http://www.google.com/search?q=" . $search . $extension . "\">";
}
else
{
die("You need to enter a search term");
}

?>


I made the HTML into radio buttons to make it simpler:



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="search.css">
<title>G Search - by: Michael Fridman</title>
</head>

<body>

<div id="content">

<div class="searchMP3">
<FIELDSET><LEGEND>Search for MP3s</LEGEND><br />
<center><form name="form" target="_new" method="GET" action="Gsearch.php">

Song or Artist:
<input name="mp3search" type="text" />
<input type="submit" value="Search" />
<br />
<br />
<table width="296" border="0" cellpadding="0" cellspacing="0">
<tr>
<td width="70" height="20">
<input name="extension" value="mp3" type="radio" />

.MP3 <br /></td>
<td width="70"><input name="extension" value="wma" type="radio" />
.WMA</td>
<td width="70"><input name="extension" value="wav" type="radio" />
.WAV</td>
<td width="70"><input name="extension" value="ogg" type="radio" />
.OGG</td>

</tr>
<tr>
<td colspan="5">&nbsp;</td>
</tr>
</table>
</form>
</center>
</FIELDSET></div></div>
</body>
</html>

squaredvision
03-03-2009, 03:55 PM
*QUOTE*
So if I searched for "Through the fire and the flames", it would add whatever button I checked to the end? Like "Through the fire and the flames.mp3"
*END QUOTE*

It would only add the name to the URL:
http://www.google.com/search?hl=en&q=-inurl%3A(htm|html|php)+intitle%3A%22index+of%22+%2B%22last+modified%22+%2B%22parent+directory%22+%2Bdescription+%2Bsize+%2B(.mp3)+%22Through the fire and the flames%22

the .MP3 is hard coded in the URL already

so if the user chooses .WMA it would be:
http://www.google.com/search?hl=en&q=-inurl%3A(htm|html|php)+intitle%3A%22index+of%22+%2B%22last+modified%22+%2B%22parent+directory%22+%2Bdescription+%2Bsize+%2B(.wma)+%22Through the fire and the flames%22


thank you,
Michael

squaredvision
03-05-2009, 04:03 PM
Maybe this can be done with If statements, If user select mp3, put whatever is in search box in the mp3 URL, when user selects wma, put whatever is in search box in the wma URL.

hmm, i'm still trying to figure this out.

Schmoopy
03-05-2009, 07:19 PM
Try and figure it out for yourself and you'll probably learn quicker.

Here's something to get you started:



<?php

if (isset($_GET['extension']))
{

switch ($_GET['extension'])
{
case "mp3":
$ext = "mp3";
// Add your url code and add the .mp3 where necessary
break;

case "wma":
$ext = "wma";
// Add your url code and add the .wma where necessary
break;

// Etc...

// echo "http://www.google.com?q=" . $search . $ext;


}

}

?>


Sorry about the bad formatting but it's a bit hard to do indents on here.

Hopefully you can figure it out from here,

Good luck!