View Full Version : isset() Demo?

12-04-2008, 10:40 PM
Its been a while since I played with the isset() function. What I am trying to create is a record update script for my database (MySQL). I have lots of records in the database that can get narrowed down by two columns.

1:Product Category.
2:Product Name.

What I want to have is when my php record edit page loads, to retrieve all product category's into an array that gets echoed out into a form select field.

<select id="product_category">

Then when a user chooses a product category from the drop down my php page uses that selected product category to retrieve all product names listed under that category and echo them out into another form select field.

<select id="product_name">

OK once a user then selects a product name from this second select form field I want to use both selected product_category and selected product_name to create a query that retrieves that specifically selected products information and echo it all out into form fields that the user uses to edit this products information.

Once an edit is finished I again want to use the product_category and product_name to update that specific row in my database.

WHERE DO I START... Like I said, its been a while since I played with isset() and I think this is where I start but since the last time I used it I can find a demo of what I am trying to create here on the net anymore nor in my archive of old scripts...

Anyone be kind enough to point me in the right direction here please???

12-05-2008, 12:07 AM
if (isset($_POST['postvar'])) {
echo "isset returns true as post var is populated";
} else {
echo "isset returns false as postvar is unpopulated";

12-05-2008, 12:12 AM
That seems to be a simple question. I could suggest you couple of solutions but you have asked about last part of the whole project, which by itself is the key of the problem.

First of all let’s find out how do you do this select category and select product? Are you using some AJAX or do you do simple POST/GET? Or may be you need the whole scenario how to implement this script?

Waiting for your reply

12-05-2008, 12:21 AM
Hey guys.
Well to be honest all this is just in theory I am currently not populating my select fields at all yet.
If I have to use ajax to populate them seamlessly (no page refresh) then I will but I was certain I could do the same seamless approach with PHP alone... maybe not.

I think I may need "the whole scenario on how to implement this script".

I know this is not a wishing well for people to ask and shale recive sort of thing and that's not what I am looking for but maybe some little examples per step of my desired script would really help me out a lot.


12-05-2008, 01:05 AM
Ok, now we have the basic requirements and we could make at list raw project :) Of cause we could do everything without AJAX but I will strongly recommend to study xAJAX it is really easy to use and once you will try it you will never do any project without it ;)

So the first thing is that we will need a form with two select fields and probably two submit button on the right side of them with caption 'load'. They should have different names so we could identify which of them was pressed (of cause we could do with just one button or even "onchange" even of list box but in that case you will need to use JS and that is not a good idea since users used to disable JS). Lets say we have two buttons with names: do_load_category and do_load_product. All data will be sent to index.php file.

Now in index.php file we have to check which button was pressed (could be that none of them :)). Here you will need your isset() function to check the existence of your button. The only one button name will be sent to the index.php, the one which was pressed. So you will have:

if (isset($_REQUEST['do_load_category'])) {
// here you have to check that category was selected, some stupid user could miss it :)
} else if (isset($_REQUEST['do_load_product'])) {
// the same here but now you have to check that both category and product is selected
} else if (isset($_REQUEST['update'])) {
// that is the button at the bottom of the page which user will press when he wants to
// update the data. Here you have to check that category and product fields are selected
// and if everything is ok then update your data in DB
} else {
// print default page, user have not pressed any button yet :)

The good idea is to disable product select field until the do_load_category button will not be pressed. The same for update button at the end of the page. You will enable it only when both category and product has the valid values.

I personally prefer to use HTML templates. I never mix PHP and HTML. Instead I like to use %%PRODUCTS%%, %%CATEGORIES%%, %%UPDATE_DISABLED%% marcros. That will allow you later to update your design without touching you PHP script ;)

Good luck

12-05-2008, 05:34 AM
yeah now that I am out of the office at home ha ha I see that I am making this harder than I need it to be.

I am going to go with Ajax I know it quite well been using it for sometime now, but I got so wrapped up into the whole php/MySQL database driven site that I actually made it harder than it needs to be.

My new plan of attack is to use php to populate both select fields and attach a JavaScript function to the onchange that collects their values and preform ajax get to my php file that will build my edit form to edit that particular products information.

Once edited again I will use the two select fields to preform an update query via ajax post to the database..

YAY everyone is happy, maybe its the lights at work that makes it so hard to concentrate lol.

12-05-2008, 10:19 AM
you dont need ajax, just use switch statement, your options act as a name and do this
just change id to name

$product_category = $_POST['product_category']
case "option1":
case "option2":