Jump to content


Newbie needs help

  • Please log in to reply
2 replies to this topic

#1 niza

  • New Members
  • Pip
  • Newbie
  • 9 posts

Posted 03 April 2006 - 02:51 AM

Hi everybody.
I am building a webpage with cell phone and accessories ads. Basically when someone wants to sell his/her cell phone will post an ad on my webpage.

I want to create a list/menu with the 3 categories (phones, accessories, cards), and pass the 3 values to category.php page.

Here's the code for the list/menu so far which is contained in the choose.php file:

echo "<select name='category'>\n";
echo "<option name='category' value='phones'>Phones</option>\n";
echo "<option name='category' value='accessories'>Accessories</option>\n";
echo "<option name='category' value='cards'>Cards</option>\n";
echo "</select>";

The query for the category.php page looks like this:
$query = "SELECT * FROM ad WHERE (and I want the value passed from the list/menu in here)"

I would also like to mention that the list/menu should have a onChange auction (I do not want a submit button) and it should take you to category.php page(this is also the page where the values should be passed to).

Thank you in advance.

#2 shortj75

  • Members
  • PipPipPip
  • Advanced Member
  • 118 posts
  • LocationPort Huron,mi

Posted 03 April 2006 - 08:48 AM

are you useing a database
The Short One

hopfully this helps if it doesn't sorry
i won't write the code for you!!! you don't learn that way

#3 wildteen88

  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 03 April 2006 - 08:58 AM

This is the code you'll want for your category menu:
echo "<form name=\"cat\" action=\"category.php\" method=\"get\">";
echo "<select name=\"cat\" onChange=\"document.cat.submit()\">\n";
echo "<option>Choose a category:</option>\n";
echo "<option  value=\"phones\">Phones</option>\n";
echo "<option value=\"accessories\">Accessories</option>\n";
echo "<option value=\"cards\">Cards</option>\n";
echo "</select>";
echo "</form>"
Now when the user selects a category they will be sent to category.php. Now to retieve the sent data you'll want to use this code:

// make sure you connect to your database in order your query below to work

//check that $_GET['cat'] is actually set
$cat = isset($_GET['cat']) ? $_GET['cat'] : '';

$cats = array('phones', 'accessories', 'cards');

//if $cat is empty or if the value of $cat is not in the $cats array stop the script from running as the 
// user hasn't made a valid selection and could be trying to hack your site! This is a good technique 
// to use as it limits what a user can submit!
if(empty($cat) || !in_array($cat, $cats)) {
    die("<b>WARNING</b><br />No selection/an invalid selection was made. Please submit a valid value.");

//query time
$query = "SELECT * FROM ad WHERE category = $cat";

And there you have it. You just needed a line of javascript which was this:
document.cat.submit(); to submit the form with the onChange event! Then if you read the comments (//) in the secound code block you should see how it's done.

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users