Jump to content

Newbie needs help


niza

Recommended Posts

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.
Link to comment
https://forums.phpfreaks.com/topic/6440-newbie-needs-help/
Share on other sites

This is the code you'll want for your category menu:
[code]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>"[/code]
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:
[code]<?php

// 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";

?>[/code]And there you have it. You just needed a line of javascript which was this:
[i]document.cat.submit();[/i] 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.
Link to comment
https://forums.phpfreaks.com/topic/6440-newbie-needs-help/#findComment-23418
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.