You are here:  » Drop down


Drop down

Submitted by chrisst1 on Fri, 2006-04-21 14:28 in

David

Is it posible to create a html search form with text search input plus a drop down menu to seperate database categories i.e. socks in clothes department.

After working with 1 db and the vast amount of products under one roof we decided to break it down a bit. I have created all the departments by duplicating your script and set up a db for each, the only problem is getting the above search form to work

Any suggestions

Chris

Submitted by support on Fri, 2006-04-21 14:34

Hi Chris,

Have you seen the following thread:

http://www.pricetapestry.com/node/205

I think this is the same scenario - let me know if that helps!

Cheers,
David.

Submitted by chrisst1 on Fri, 2006-04-21 15:26

Had a go but still can't get it to work

{link saved}

Chris

Submitted by support on Fri, 2006-04-21 15:52

Hi Chris,

Can you post your code for test.php, and describe your directory layout for each category's installation of Price Tapestry.

It's all down to how you're handling the form submission...

Cheers,
David.

Submitted by chrisst1 on Fri, 2006-04-21 16:02

David

{links saved}

And so on

Submitted by support on Fri, 2006-04-21 16:16

Hi Chris,

I have notice an error in the code in the other thread; the submit form field has no name='' attribute; so it wouldn't actually be handled. I've corrected it in the original, and posted a customised version for your directory layout below:

<?php
  
if ($_POST["submit"] == "Search")
  {
    
$url "/productsearch/".$_POST["category"]."/search.php?q=".$_POST["q"];
    
header("Location: ".$url);
    exit();
  }
  print 
"<form method='POST'>";
  print 
"<input type='text' name='q' />";
  print 
"<select name='category'>";
  print 
"<option value='computershops'>Computing</option>";
  print 
"<option value='beautyshops'>Beauty</option>";
  print 
"<option value='motoring'>Motoring</option>";
  print 
"</select>";
  print 
"<input type='submit' name='submit' value='Search' />";
  print 
"</form>";
?>

Submitted by chrisst1 on Fri, 2006-04-21 16:19

Brilliant!

Many Many thanks

Chris

Submitted by mmolaire on Sat, 2006-04-22 03:16

I am trying to implement this I am getting the following error. What am i doing wrong?

Warning: Cannot modify header information - headers already sent by (output started at /home/mmolaire/public_html/verysmartshopping/index.php:7) in /home/mmolaire/public_html/verysmartshopping/html/searchform.php
on line 8

Mike M

Submitted by support on Sat, 2006-04-22 06:44

Hi Mike,

The header() function must be called before any HTML has been generated by the script. It sounds like you may have coppied the example code into your searchform.php file, which unfortunately won't work because HTML has alredy been generated before searchform.php is included.

You can make the modifications to the search form in that file; but the part of the code that handles the redirect must go right at the very top of search.php, where it will pick up the POST; and then redirect to the appropriate search.php with a GET request.

This is the part to go at the top of search.php:

<?php
  
if ($_POST["submit"] == "Search")
  {
    
$url "/".$_POST["category"]."/search.php?q=".$_POST["q"];
    
header("Location: ".$url);
    exit();
  }
?>

(remember to modify $url as requred for your server)

Remove that code from serachform.php - but leave the form modifications (which I think you have already done) in place; and make sure that you have changed the form to use the POST submit method...

Hope this helps!
David.

Submitted by dthardy on Mon, 2006-07-31 01:09

I am getting the following error:
Notice: Undefined index: submit in c:\websites\estonishcom\html\search.php on line 3

I have tried to correct but can't find anything to work:

Here is my code:

Search.php

<?php
  
if ($_POST["submit"] == "Search")
  {
    
$url "/".$_POST["category"]."/search.php?q=".$_POST["q"];
    
header("Location: ".$url);
    exit();
  }
  require(
"includes/common.php");
.....
?>

SearchForm.php

I can't get this to post with full SearchForm.php. My search form is the same as the original searchform.php with the following additions.

added -
method='POST' to form line
<select name='category'>
   <option value='Books'>Books</option>
   <option value='Computers'>Computers</option>
  </select>
added name='submit' to input type=submit

I have set up my site with seperate installations of pricetapestry in sub folders. Books & Computers

you can view source at http://www.estonish.com

Can anyone point the way?

Submitted by support on Mon, 2006-07-31 10:46

Hi There,

The site isn't generating that warning at the moment; but it is not redirecting upon submitting the form.

The first thing I have noticed is that the warning is referring to line 3 of the script; and since $_POST["submit"] is on line 2 of the PHP segment that implies that there may be some white space in the file above the opening php tags.

PHP would have sent the whitespace to the browser (by default it empties the buffer on every new-line); and therefore the redirection header would not work. If this is the case, removing the empty line at the top of the file should fix it.

Hope this helps,
Regards,
David

Submitted by dthardy on Mon, 2006-07-31 11:13

Thanks for the info on the whitespace. I did notice that sometimes it would not return anything. I have removed the whitespace. I now get the same error but on line 2 where the first line of the code is. Please look at it again and see what you find.

Thanks for the help.

Submitted by support on Mon, 2006-07-31 11:45

I'm still not seeing an error message, just a blank page. What I would do at this point is print out some debug message on search.php. To do this, simply add the following code to the very first line of search.php:

<?php
  print_r
($_POST);
?>

Your HTML looks OK, it is being POST'ed, and the Submit button has the name "submit", so there is no reason why it should not be set. This debug code will print out what search.php is seeing when the form is submitted - if you could post what is displayed when you click the form that should help!

Cheers,
David.

Submitted by dthardy on Mon, 2006-07-31 14:47

I continue to get same error code. It doesn't return only the white page now. Does this have something to do with the database index? I have added new fields for this datafeed.

Submitted by support on Mon, 2006-07-31 16:10

Are you not seeing the output from the print_r() command?

I would modify the debug code as follows:

<?php
  
print "POST TEST:";
  
print_r($_POST);
?>

This must display some output, otherwise something funny is going on!

Cheers,
David.

Submitted by support on Mon, 2006-07-31 16:13

Just looking at your site, I've just realised something else that you need to do. The reason it is coming up with the message is because you have PHP's warnings turned all the way up; and it is testing for $_POST["submit"] when you first view the script even before you have submitted the form.

To fix this, you need to add the following ifset() code around your form:

<?php
  
if (isset($_POST["submit"]))
  {
    if (
$_POST["submit"] == "Search")
    {
      
$url "/".$_POST["category"]."/search.php?q=".$_POST["q"];
      
header("Location: ".$url);
      exit();
    }
  }
  ...
?>

That should stop the warning message.

Cheers,
David.

Submitted by dthardy on Mon, 2006-07-31 17:59

I am a Cold Fusion man and fairly new to PHP. Should I change the warning settings?

Submitted by dthardy on Mon, 2006-07-31 18:18

I put this code in search.php with no whitespace after php but continue to have problems. I put the debug code back in. I tried searching for "printers" in "Computers" and got the following error message.

Array ( [q] => printer [category] => Computers [submit] => Search )
Warning: Cannot modify header information - headers already sent by (output started at c:\websites\estonishcom\html\Computers\search.php:2) in c:\websites\estonishcom\html\Computers\search.php on line 8

Searched for "friend" in "Books" and received the following output:

Array ( [q] => friend [category] => Books )
returned no errors but no results. There are several books with friends in the title that could have been found.

Submitted by support on Tue, 2006-08-01 11:32

Hi,

The debug output looks OK, so the next thing to do is to look at the URL being generated in the redirection and try to understand why it does not work when it issued in a HTTP 302 redirect.

To do this, remove the debug code and change the code you have written to the following:

<?php
  
if (isset($_POST["submit"]))
  {
    if (
$_POST["submit"] == "Search")
    {
      
$url "/".$_POST["category"]."/search.php?q=".$_POST["q"];
      print 
"Redirecting to: ".$url;
      
// header("Location: ".$url);
      
exit();
    }
  }
  ...
?>

If you can let me know when you have uploaded this code i'll look at the URLs being generated and then view those URLs directly to see where the problem lies.

Remember that you need to make sure that the search form has been modified and the search.php script contains the same redirect code in every sub-dirctory installation.

Cheers,
David.

Submitted by dthardy on Tue, 2006-08-01 12:48

New code is posted and working.

I can't get my searchform.php to post in a comment. Can I email it to you or can someone give me the trick to getting it to post. I have enclosed it in or

<?php
 
?>
but the forum kicks it out.

Submitted by support on Tue, 2006-08-01 13:42

Hi,

No problem - I can see the URLs on your site.

They look fine. When I search for "Test" in Books, it prints out:

Redirecting to: /Books/search.php?q=Test

If you then go to:

http://www.estonish.com/Books/search.php?q=Test

...it works fine.

Therefore, there is no reason why it shouldn't work, so first of all, I would suggest going back to the correct code:

<?php
  
if (isset($_POST["submit"]))
  {
    if (
$_POST["submit"] == "Search")
    {
      
$url "/".$_POST["category"]."/search.php?q=".$_POST["q"];
      
header("Location: ".$url);
      exit();
    }
  }
  ...
?>

If that still doesn't work; the next thing I would try is providing the full host name in the URL, like this:

<?php
  
if (isset($_POST["submit"]))
  {
    if (
$_POST["submit"] == "Search")
    {
      
$url "http://www.estonish.com/".$_POST["category"]."/search.php?q=".$_POST["q"];
      
header("Location: ".$url);
      exit();
    }
  }
  ...
?>

Cheers,
David.

Submitted by dthardy on Tue, 2006-08-01 15:00

Still having problems after trying code both ways.

It must be in searchform.php because if I type "?q=test on the end of the "www.estonish.com/search.php" address I get results. If I type "test" in the searchform box the address returned is "www.estonish.com/search.php". How can I get the code to post or email it somewhere? The forum keeps restricting the post. I think there must be a problem in the form.

Submitted by support on Tue, 2006-08-01 16:27

Hi,

At the moment it's trying to redirect to here:

http://www.estonish.com/www.estonish.com/Books/search.php?q=test

That implies that it is missing the "http://" off the start of $url in the second version of the code in my earlier example...

Check that you have $url = "http:// ...... and it should then work properly.

Cheers,
David.

Submitted by dthardy on Tue, 2006-08-01 21:12

Turns out there were two issues on the latest attempt. It still didn't work for me without the "http://www.estonish.com/, but did with it. In typing the long url in I inadvertently left out one "/" after http:. I had typed "http:/www.estonish.com/" instead of "http://www.estonish.com/".

Also, under "/Books" I had a couple spaces after the start of php and this was causing a return of whitespace at times. Long story short, everything is working fine now. I greatly appreciate all the help and guidance.

Best Wishes.

Submitted by dthardy on Wed, 2006-08-02 19:00

I am still getting mixed results. I may have found the problem from the beginning. When I type in the search text box and click the search button, everything works properly every time. If I type in the search text box, but press the "enter" key on the keyboard the page returns to the search page but does not submit the "q" variable and nothing is returned.

How do we work with people that are going to press the "enter" key?

Try it yourself:

www.estonish.com
www.estonish.com/book/
www.estonish.com/computers/

Submitted by dthardy on Thu, 2006-08-03 03:34

I have been researching the "enter" key issue and it turns out to be a browser issue with certain browser versions. I haven't found a good solution yet. I'll keep you posted, but if anyone knows the answer or finds it, please post.

Submitted by support on Thu, 2006-08-03 05:21

Hi,

I think the easiest way to deal with this issue is to create a hidden form field and check for that rather than $_POST["submit"]. For example, if you add the following code to the search form:

<input type='hidden' name='search' value='1' />

And then, instead of:

if (isset($_POST["Submit"]))

use...

if (isset($_POST["search"]))

That may be a way around it.

Cheers,
David.

Submitted by dthardy on Thu, 2006-08-03 12:55

Made the changes suggested. Received the following error:
Notice: Undefined index: submit in c:\websites\estonishcom\html\search.php on line 4

The code for line 4 is: if ($_POST["submit"] == "Search")
full code

<?php
  
if (isset($_POST["search"]))
  {
    if (
$_POST["submit"] == "Search")
    {
      
$url "http://www.estonish.com/".$_POST["category"]."/search.php?q=".$_POST["q"];
      
header("Location: ".$url);
      exit();
    }
  }
?>

I changed submit to search in line 4 but received no search results and the q variable was not passed.

Submitted by support on Thu, 2006-08-03 13:38

Hiya,

You should just be able to remove the second if statement now; as it is redundent when using the "search" variable...

<?php
  
if (isset($_POST["search"]))
  {
    
$url "http://www.estonish.com/".$_POST["category"]."/search.php?q=".$_POST["q"];
    
header("Location: ".$url);
    exit();
  }
?>

Submitted by dthardy on Thu, 2006-08-03 18:34

It works! Your fantastic! Many thanks.