Forum Moderators: open

Message Too Old, No Replies

Hiding the "?variable=" if GET input is empty

         

iceman22

4:41 am on Jan 15, 2005 (gmt 0)

10+ Year Member



I'm looking for a way to hide the?variable= from the URL if the input field is empty using GET. Using script.php or script.php?search= give the same result.

Another problem I can't figure out is why there is a trailing "1" at the end of every string in the input field, which it gets from the URL. Whatever I search, a "1" appears at the end of it in the input field, even if the URL is script.php?search= I get a "1" in the input field.

Here is the code I'm using, thanks.

<form action=<?= $PHP_SELF?> method=get>
<input name="search" value=<?= print $_GET['search'];?>>
<input type="submit" value="Search">
</form>

iceman22

7:09 am on Jan 18, 2005 (gmt 0)

10+ Year Member



If anyone is having a similar problem, I solved the trailing 1 by making the entire html form outputted using PHP.

echo "<form action=\"$PHP_SELF\" method=\"get\">\n";
echo "<input type=\"text\" name=\"search\" size=\"25\" value=\"".$_GET['search']."\" />&nbsp;\n";
echo "<input type=\"submit\" value=\"Search\" /></form>";

On the hiding the?search=, the best I could do was to use JavaScript:

<script language="JavaScript">
if ( location.href == "http://www.domain.com/path/script.php?search=" ) {
location.href = "script.php";
}
</script>

It is doesn't work well though, it only changes the URI after it loads, then reloads which takes a few minutes because the page is large.

Hester

9:35 am on Jan 18, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You could write those 3 echo lines as one. Instead of ending on each line, just carry on with a linebreak. You can also do away with all those slashes by using single quote marks instead of doubles. (Variables will have to be concatenated though.) Try this:

echo '<form action="'.$PHP_SELF.'" method="get">
<input type="text" name="search" size="25" value="'.$_GET['search'].'" />&nbsp;
<input type="submit" value="Search" /></form>';

rocknbil

5:11 pm on Jan 18, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Is there a particular reason you're using get instead of post? If you use the post method instead of get, not only can you submit more data it shouldn't return a query string.

twist

8:14 pm on Jan 18, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Cant think of anyway to clean up the url unless you want to use a redirect. Send the variables to a page that checks them and then redirect the person back to the search page.

You can echo entire blocks of code though. It will keep your formatting.


echo'
<form action="'. $PHP_SELF .'" method="get">
<input type="text" name="search" size="25" value="'. $_GET['search'] .'" />
&nbsp;<input type="submit" value="Search" />
</form>
';