PDA

View Full Version : "form" help.



unknownerrors
02-01-2007, 12:02 AM
i never was great with forms mainly due to the fact that i have never before had anything but a free server (no Db, no php, no cgi) thats changed and i'd like to use some of these features in a form i'm having issue putting together the logic for it. a little help from you fine peeps would be great.

this form will allow user to search different search engines from one place.

so a seach box

Code:

search for:
<input name="searchbox" type="text">


then a list of seach engines that the user can select via radio button..

Code:

<p> google:
<input type="radio" name="google" value="google's query URL=">
</p>
<p>hotbot:
<input type="radio" name="hotbot" value="Hotbot's query URL=">


the a simple submit button..

i want the form to take the text from the "searchbox" and add it to the end of the value of the checked radio button then send the combined url to my targeted frame..

seems so simple in theory..
any help here would be great, thanks in advance.

you can find my first post about this question http://www.ynot.com/index.php?name=PNphpBB2&file=viewtopic&p=610842#610842

thetestingsite
02-01-2007, 12:13 AM
Do you want a PHP solution or a javascript solution? The following is a PHP solution:



<?php
if ($_POST['act'] == "do_search") { //if the action is do_search
if ($_POST['q'] == "" || $_POST['se'] == "") { //if query or search engine is blank
header('Refresh:3; url='.$_SERVER["PHP_SELF"]);
echo 'You must enter a search query and select a search engine to search!';
}

else {
if ($_POST['se'] == "google") {

header('Location: http://www.google.com/search?hl=en&q='.$_POST["q"].'&btnG=Google+Search');

}

elseif ($_POST[''] == "hotbot") {

header('Location: http://hotbot.com/?query='.$_POST["q"].'&ps=&loc=searchbox&tab=web&mode=search&currProv=ask ');
}

else {
header('Location: '.$_SERVER["PHP_SELF"]);
}

}

else { //if form not submitted
?>

<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="POST">
<input type="hidden" name="act" value="do_search">

Search: <input type="text" name="q" value=""><BR>
<input type="radio" name="se" value="google">Google <input type="radio" name="se" value="hotbot"><BR>
<input type="submit" value="Search">

</form>

<?php
}
?>


Added Later: If you want to make the search form on a seperate page, then have it target the specified frame, try the following:




<form action="filename.php" method="POST" target="framename">
<input type="hidden" name="act" value="do_search">

Search: <input type="text" name="q" value=""><BR>
<input type="radio" name="se" value="google">Google <input type="radio" name="se" value="hotbot"><BR>
<input type="submit" value="Search">

</form>


Where filename.php is the filename of the script above, and the framename is your target frame.

Hope this helps.

unknownerrors
02-01-2007, 11:19 AM
thank you ~!

unknownerrors
02-01-2007, 01:24 PM
ok i uploaded a test of the code you posted, and its loading the script but doing nothing..

heres what i have it set up as..

searchform.htm



<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<form action="search.php" method="POST" target="framename">
<input type="hidden" name="act" value="do_search">

Search: <input type="text" name="q" value=""><BR>
<input type="radio" name="se" value="google">Google <input type="radio" name="se" value="hotbot">
Hotbot<BR>
<input type="submit" value="Search">

</form>
</body>
</html>


and search.php



<?php
if ($_POST['act'] == "do_search") { //if the action is do_search
if ($_POST['q'] == "" || $_POST['se'] == "") { //if query or search engine is blank
header('Refresh:3; url='.$_SERVER["PHP_SELF"]);
echo 'You must enter a search query and select a search engine to search!';
}

else {
if ($_POST['se'] == "google") {

header('Location: http://www.google.com/search?hl=en&q='.$_POST["q"].'&btnG=Google+Search');

}

elseif ($_POST[''] == "hotbot") {

header('Location: http://hotbot.com/?query='.$_POST["q"].'&ps=&loc=searchbox&tab=web&mode=search&currProv=ask ');
}

else {
header('Location: '.$_SERVER["PHP_SELF"]);
}

}

else { //if form not submitted
?>


i dont claim to know anything about Php, so i already guess that my error is very noobler.

you can see what happens here (http:/http://unknownerrors.net/search/searchform.htm)

BLiZZaRD
02-01-2007, 01:29 PM
invalid link :) It should be like this (http://unknownerrors.net/search/searchform.htm) :D

Edit.. it looks good though...

You didn't specify a "framename"

If you aren't using frames then remove that "target=" part.

unknownerrors
02-01-2007, 02:04 PM
i will be using frames when i put it together for use, but for now, if it doesn't find the frame it will default back to _blank...

but the search results aren't being displayed after search.php has run. the page remain blank.

thetestingsite
02-01-2007, 05:44 PM
OK...try the following:

search.php


<?php
if ($_POST['act'] == "do_search") { //if the action is do_search
if ($_POST['q'] == "" || $_POST['se'] == "") { //if query or search engine is blank
header('Refresh:3; url='.$_SERVER["PHP_SELF"]);
echo 'You must enter a search query and select a search engine to search!';
}

else {

header('Location: '.$_POST["se"].$_POST["q"]);

}

}

else { //if form not submitted
?>

<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="POST">
<input type="hidden" name="act" value="do_search">

Search: <input type="text" name="q" value=""><BR>
<input type="radio" name="se" value=" http://www.google.com/search?hl=en&btnG=Google+Search&q=">Google <input type="radio" name="se" value="http://hotbot.com/?loc=searchbox&tab=web&mode=search&currProv=ask&query=">HotBot<BR>
<input type="submit" value="Search">

</form>

<?php
}
?>


Standalone form:




<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="POST">
<input type="hidden" name="act" value="do_search">

Search: <input type="text" name="q" value=""><BR>
<input type="radio" name="se" value=" http://www.google.com/search?hl=en&btnG=Google+Search&q=">Google <input type="radio" name="se" value="http://hotbot.com/?loc=searchbox&tab=web&mode=search&currProv=ask&query=">HotBot<BR>
<input type="submit" value="Search">

</form>


Tested and works.

Hope this helps.

unknownerrors
02-01-2007, 06:13 PM
a search for "anything" with google checked, sends me to http://unknownerrors.net/search/&#37;3C?php%20echo%20$_SERVER['PHP_SELF'];?%3E

hotbot is the same result.

i just noticed the first post was leaving error_log,
[01-Feb-2007 23:15:53] PHP Parse error: syntax error, unexpected T_ELSE in /home/unknowne/public_html/search/search.php on line 34

unknownerrors
02-01-2007, 06:43 PM
another peice of info, i use firefox, i had the page with this form in it named searchform.htm, that was giving the above error.. i renamed it to searchform.php, now when i submit the form it reloads the page and hides the form.

thetestingsite
02-01-2007, 06:45 PM
try changing the file extension from html to php and see if that works. Also, are you calling the form from an html page with the action as search.php? I looked at your website from the link you posted and noticed that you need to change it to php as compared to html.

Hope these help.

unknownerrors
02-01-2007, 06:55 PM
i droped it into the root to see if that helped anything.. it didn't. heres the link.

http://unknownerrors.net/searchform.php

unknownerrors
02-01-2007, 07:51 PM
very weird, when i make the search page in Dreamweaver, the form action is set to <?php echo $_SERVER['PHP_SELF'];?>

once up loaded to the server, if you view source the action is changed to what ever the file name of the search page is.

BLiZZaRD
02-01-2007, 09:05 PM
Sounds like dreamweaver.

Try doing it in notepad. (you can copy and paste). DW has a knack for parsing scripts during publishing, which is a bad thing.

See what happens if you do it in a plain text editor.

unknownerrors
02-01-2007, 09:29 PM
wow, never realized how much crap DW puts in its files.

anywho,

this is what the file i made looks like.


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01

Transitional//EN">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html;

charset=iso-8859-1">
</head>
<body>


<?php
if ($_POST['act'] == "do_search") { //if the action is

do_search
if ($_POST['q'] == "" || $_POST['se'] == "") { //if

query or search engine is blank
header('Refresh:3; url='.$_SERVER["PHP_SELF"]);
echo 'You must enter a search query and select a

search engine to search!';
}

else {

header('Location: '.$_POST["se"].$_POST["q"]);

}

}

else { //if form not submitted
?>

<form action="<?php echo $_SERVER['PHP_SELF'];?>"

method="POST">
<input type="hidden" name="act" value="do_search">

Search: <input type="text" name="q" value=""><BR>
<input type="radio" name="se" value="

http://www.google.com/search?hl=en&btnG=Google+Search&q=">G

oogle <input type="radio" name="se"

value="http://hotbot.com/?loc=searchbox&tab=web&mode=search

&currProv=ask&query=">HotBot<BR>
<input type="submit" value="Search">

</form>

<?php
}
?>
</body>
</html>


Now, this is what the file looks like on the server.



<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>



<form action="/searchform.php" method="POST">
<input type="hidden" name="act" value="do_search">

Search: <input type="text" name="q" value=""><BR>
<input type="radio" name="se" value=" http://www.google.com/search?hl=en&btnG=Google+Search&q=">Google <input type="radio" name="se" value="http://hotbot.com/?loc=searchbox&tab=web&mode=search&currProv=ask&query=">HotBot<BR>

<input type="submit" value="Search">



</form>


</body>
</html>



theres some code missing.. how is it getting lost from my computer to smartftp though my server to my browser?!

BLiZZaRD
02-01-2007, 09:34 PM
Well, php is a server-side language. Everything in php coding tags is being parsed/read before it get's to the browser.

The browser's source code only displays what you actually physically see on the page (in reference to html vs. php here)

So anything between the <?php and the ?> won't show in the source code. Unless you TELL php to show some things, i.e. the echo ' statement.

thetestingsite
02-01-2007, 11:09 PM
Ok, here's a for sure working version. Just edit the parts in red (commented) to suit your needs.

search.php


<?php

/* Below is the header section (the part before the search form) of your page. Place it between the HERE tags! */

$head = <<<HERE
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
HERE;

/* Below is the footer section (the part after the search form) of your page. Place it between the HERE tags! */

$footer = <<<HERE

<iframe src="" name="myFrame"></iframe>
</body>
</html>
HERE;

if ($_POST['act'] == "do_search") { //if the action is do_search
if ($_POST['q'] == "" || $_POST['se'] == "") { //if query or search engine is blank
header('Refresh:3; url='.$_SERVER["PHP_SELF"]);

echo $head;

echo 'You must enter a search query and select a search engine to search!';

echo $footer;

}

else {

header('Location: '.$_POST["se"].$_POST["q"]);

}

}

else { //if form not submitted

echo $head;
?>

<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="POST" target="myFrame">
<input type="hidden" name="act" value="do_search">

Search: <input type="text" name="q" value=""><BR>
<input type="radio" name="se" value=" http://www.google.com/search?hl=en&btnG=Google+Search&q=">Google <input type="radio" name="se" value="http://hotbot.com/?loc=searchbox&tab=web&mode=search&currProv=ask&query=">HotBot<BR>
<input type="submit" value="Search">

</form>

<?php
echo $footer;
}
?>


To see what it does, go to this page (http://phphost.smackum.com/test/do_search.php) (Note, this is my home server. If you can't access it, I may be doing updates or it is turned off.)

Hope this helps.

unknownerrors
02-02-2007, 09:02 PM
works like a charm.. thank you very much.

unknownerrors
02-02-2007, 11:49 PM
if you want to see what you guys help turned into.

http://alphapirates.com/

far from completed. but working.

thetestingsite
02-03-2007, 12:02 AM
Great, at least it's working for you. Let us know if you need any more help.

BLiZZaRD
02-03-2007, 01:10 AM
Nice. And it works too

Check the spacing in the <title> though ;)

unknownerrors
02-03-2007, 01:29 AM
this whole ordeal has really opened my eyes to the disadvantages of not knowing Php in the slightest. so i picked up a few ebooks going to start grinding into them on my breaks at work.