Forum Moderators: coopster

Message Too Old, No Replies

HTML Form query

         

PeteM

7:34 pm on Dec 6, 2004 (gmt 0)

10+ Year Member



I wish to create a form on my website. There will be two fields; a text area where the visitor enters a search term and a dropdown box containing merchant names. When the visitor presses submit he is redirected to search results on the approriate merchants website.

e.g.

when the user enters
search="fab widgets"
drop-down="widget.com"

he is taken to....

http://www.example.com/widget.asp?searchtype=allproducts&searchstring=fab+widgets

How do I do this? Do I need to create a PHP script to handle the form?

Thanks, Pete

[edited by: tedster at 8:52 pm (utc) on Dec. 6, 2004]
[edit reason] use example.com [/edit]

rocknbil

7:52 pm on Dec 6, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you don't oppose using JAVASCRIPT, it would be easy, create an input type=button with an onClick event handler, build the string in Javascript, and print it to the document.location. I hacked it a bit to get it to ignore the button and the last &, but you get the idea:


<html><HEAD><TITLE></TITLE></HEAD></BODY>

<form method="get" action="foo.cgi">
<INPUT TYPE="text" NAME="term"><br>
<select name="url">
<option value="widget.com">widget.com</option>
<option value="wadget.com">wadget.com</option>
<option value="wudget.com">wudget.com</option>
</select>

<input type="button" name="myButton" onclick="buildURL(this.form)" value="Send it">
</form>

<script language="Javascript">

function buildURL(form) {
var str,fld,val = '';
var sendIt=false;
str = 'http://.www';

for (i=0;i<form.elements.length;i++) {
var obj = form.elements[i];
if (obj.name!= 'myButton') {
fld = escape(obj.name); //in case you use special characters
if (obj.type=='select-one') {
val = obj.options[obj.selectedIndex].value;
}
else { val = obj.value; }
if (val!= '') {
val = escape(val);
str += obj.name+'='+val;
if (i< (form.elements.length-2)) { str += '&'; }
}
}
}
sendIt = confirm('OK to send " ' + str + ' "?');
if (sendIt == true) { form.submit(); } // or document.location=str, since it's get
else { alert('URL string " ' + str + ' " cancelled.'); }
}

</script>

</body>
</html>

PeteM

8:41 pm on Dec 6, 2004 (gmt 0)

10+ Year Member



Thanks rocknbil.

Any PHP wizards care to try?

Pete

PeteM

7:41 pm on Dec 7, 2004 (gmt 0)

10+ Year Member



I've got the functionality I was after by coding a handle form php page containing a redirect.....

header ("location:http:\\www.widget.com");

.....but it's really slow.

Is this a sympton of doing it server side?

Could one of the mods move this to the PHP forum?

Thanks, Pete

ergophobe

2:52 am on Dec 8, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Pete,

There's no reason that redirect should be slow. So I take it you figured out how to use $_GET to break out the search terms and you are redirecting based on those?

All of those steps should be quite fast. It sounds like you're redirecting to a merchant on another server. Could it be the merchant server is slow?

Tom

PeteM

7:22 am on Dec 8, 2004 (gmt 0)

10+ Year Member



Thanks Tom, Not sure what the $_get command is.

All help is much appreciated.

Thanks, PEte

[edited by: ergophobe at 8:50 am (utc) on Dec. 8, 2004]

ergophobe

8:57 am on Dec 8, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Pete,

It's not a $get command, but rather a language feature in PHP. PHP has a set of so-called "super-global" variables. These variables are available anywhere in your script, even inside functions where normally only local variables are available.

One of these is $_GET which contains all parameters sent as part of a query string.

[example.com?q=qword&t=time...]

would yield two get parameters

$_GET['q'] with a value of "qword"
$_GET['t'] with a value of "time"

Sometimes you can access these values like so

$t with a value of "time"

This happens when register_globals [us2.php.net] is on. Generally this is considered sloppy and somewhat dangerous practices. It is not inherently insecure, as many people contend, but it's more a matter of making your code less secure. For example, you can also use the "post" method to pass data. So if I have

$_POST['t']
$_GET['t']

and register globals is on, what is the value of $t?

Tom

PeteM

10:51 pm on Dec 9, 2004 (gmt 0)

10+ Year Member



Thanks Tom for such a concise explanation. I've often heard people going on about globals, now I know why!

Pete