need php mysql query to populate a scroll list

Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

tmjhayward

Thread Starter
Joined
Jul 17, 2005
Messages
36
Hello everyone. I'm trying to create a page that will allow the user to enter a partial/full search term, which will be used to query the database. The results of the query will then be used to populate a scroll or drop down list. The user should then be able to select an item from the final list for ordering purposes. I'd like to do this with just html, php and mysql, and preferable no javascript. I get the search box and button, but when I click the button, I get the error that it can't find the URL. I've searched tons of sites, but maybe I'm just trying to combine too many different ways of doing this.

My 2 scripts are attached (there may be some funky stuff in there from things I was trying at 1:30 this morning, that I didn't remove!). If anyone can tell me what I'm missing or doing wrong, or if there's a much cleaner/simpler way of doing this, I'd appreciate it.
Thanks!
MJ
 

Attachments

tmjhayward

Thread Starter
Joined
Jul 17, 2005
Messages
36
Okay, I figured out a way to get my initial search query to work (I called product_search.php as the POST action and took the function out of it) among a few other corrections. I can now get the output from the query, but am not sure how to put it in a form for the user to select from. Do I have to create a separate page for that since it needs to have its own POST or can I somehow continue it in the original script (ss_prodsearch_test.php)?
Thanks.
MJ
 
Joined
Oct 25, 2007
Messages
551
Can't you take the output of the search query and populate a select list with it?

PHP:
echo "<select name='opt'>";
foreach ($results as $r) {
echo "<option value='$r'>$r</option>";
}
echo "</select>";
Or have I misunderstood what you want to do?:eek:
 

tmjhayward

Thread Starter
Joined
Jul 17, 2005
Messages
36
Hi Robatron. Thanks for your response. You did understand me correctly, but I ended up just using a while loop with mysql_fetch_array. instead. I tend to decide what I want to do and then go looking for how to do it - guess I'm a bit impatient and just want to get to the final product. Frequently I'll find what I need but don't necessarily have all of the appropriate building block basics necessary to understand all of it, so it gets confusing.

But I do have another question if you don't mind. The search results are being populated on a new page. However, if a search returns no results, I want to put out a message stating so, but return the user to the initial search page, WITH the message, so they can search again. I've tried using header('Location: xx') but I get the 'Cannot modify header information' error. If you're trying to go back to a page that has code on it already, I don't see how you CAN'T get this error, but again, it's probably just something I don't understand yet.

How else can I get back to the original search page without requiring the user to click to get there?
Thanks!
MJ
 
Joined
Oct 25, 2007
Messages
551
You may be getting the error because you have already put output on the page. You can't print anything to the page before you use the header command. If that's not the reason then I'm not sure what is.

Another solution would be to make the results page on the same page as the search page:
PHP:
$searchstring = $_REQUEST['search']
if (isset($searchstring)) {
// Display search results or if there are none, drop through to print an error message
}
if (!isset($searchstring) || $searchresults = 0) {
// Search box
Alternatively, you could just put a search box on your results page that pointed to itself (emulating the original search page).
 

tmjhayward

Thread Starter
Joined
Jul 17, 2005
Messages
36
Thanks again, Robatron. I was able to use the header() command by rearranging the code on my results page so that all of the php was at the top and the html was after the header() command. I have it redirected back to my search page if there are no results. But in this case I want to write additional text on the search page like 'Your search returned no results.' I would think the only way to do this is to pass a global of some sort from the results page back to the search page, and add the additional text based on the value of that global. And the only way I've seen that you can do that, is to use sessions. Can sessions be used without login logic? (I'll be adding that soon, but don't have it set up yet.) This site has a couple of links to tutorials for sessions, but it's a bit confusing now. Do you know of a 'quick and dirty' way to set them up for this purpose?
Thanks!
MJ
 
Joined
Oct 25, 2007
Messages
551
Thankfully there is an easy way to do this. :D In the header command you can add on to the end a GET variable:
PHP:
header: "Location: www.yoursite.com/search?noresults=true";
And then on you search page:
PHP:
$noresults = $_REQUEST['noresults'];
// Place the if statement anywhere you want it to display the no results found message
if ($noresults) { // if there are no results
echo "There were no results found. Please search again.";
}
It's probably not the best way to do it, but it works.
 

tmjhayward

Thread Starter
Joined
Jul 17, 2005
Messages
36
Thanks - that worked great! I'll be using a variation of that on a few of my pages.
Regarding my first post, I populated a form (form 1) with an array from the query based on a search string entered in a previous form. Code from form 1 page looks like this:

echo "<form name='results_form' method='post' action='ss_quantity.php' >";
echo "<select name='results_list[]' multiple size='8' multiple='multiple'> ";
while ($list = mysql_fetch_array($query_result))
{
echo "<option value=\" ". $list['0'] ."\">" . $list['1']. " " . $list['2']. " " . $list['3']." </option> ";
}
echo "</select>";
echo "<input type='submit' name='Select' value='Select' />";
echo "</form>";

But when I select one or more values from form 1 and hit submit, I want it to populate the selected values in form 2. I'm not getting any values at all returned from the $_POST.

$product_list = ($_POST['results_list'][0]);

I have tried every suggestion I could find to get it to return something - with and without [] in the 'name=' phrase, the same in the $POST command in the page for form. I've seen some things about explode/implode - is this the only way to do this? Everything says just put [] after the name="xxx" and it'll work. But it's driving me crazy!
Can you help???
 
Joined
Oct 25, 2007
Messages
551
It should work if you put [] after the name in the HTML - it's the same as submitting multiple checkboxes. You don't need to put it in the PHP; just the name will do.
HTML:
<select name='results_list[]' multiple>
PHP:
$product_list = $_POST['results_list'];
I've done this before with checkboxes, so it should work fine. There shouldn't be a need to use explode() or implode() as the values aren't returned as a single string.
 

tmjhayward

Thread Starter
Joined
Jul 17, 2005
Messages
36
But then how to I populate the new form with this? I've tried using 'while' but I can't seem to get the syntax right. If I put in an echo after the $_POST it's only returning one value. If my $_POST command is this: $product_list = ($_POST['results_list']); (which may be incorrect) and I have the following, what do I put where the xxxxxxxxxxxs are?

echo "<form name='select_form' method='post' action='ss_prodorder.php' >";
echo "<select name='sel_list[]' multiple size='8'> ";
xxxxxxxxxxxxxxxxx
echo "</select>";
echo "<input type='submit' name='Add to Order' value='Add to Order' />";
echo "</form>";

Thanks again!
 
Joined
Oct 25, 2007
Messages
551
Put where the Xs are:
PHP:
foreach ($product_list as $p) {
echo "<option value='$p'>$p</option>";
}
This will print each entry in the array. If you just want to see everything that's in the array (for debugging purposes) us print_r($product_list).
 

tmjhayward

Thread Starter
Joined
Jul 17, 2005
Messages
36
Hi again. Sorry but that's not working. Here is the code from the first page:

echo "<form name='results_form' method='post' action='ss_quantity.php' >";
echo "<select name='results_list' multiple size='8' multiple='multiple'> ";
while ($list = mysql_fetch_array($query_result)) // $list = name of array indexed by col name
{
echo "<option value=\" ". $list['0'] ."\">" . $list['1']. " " . $list['2']. " " . $list['3']." </option> ";
}
echo "</select>";
echo "<input type='submit' name='Select' value='Select' />";
echo "</form>";

And here is my $_POST and the additional code from the 2nd page. The print_r is returning the ID from the array on the first page. Am I missing something?

$product_list = ($_POST['results_list']);

echo "<form name='select_form' method='post' action='ss_prodorder.php' >";
echo "<select name='sel_list[]' multiple size='8'> ";
foreach ($product_list as $p)
{
echo "<option value='$p'>$p</option>";
}
echo "</select>";
echo "<input type='submit' name='Add to Order' value='Add to Order' />";
echo "</form>";
print_r($product_list);

Thanks!
MJ
 
Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 807,865 other people just like you!

Latest posts

Members online

Top