Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7

Thread: isset() Demo?

  1. #1
    ubh
    ubh is offline
    Regular Coder ubh's Avatar
    Join Date
    Apr 2008
    Location
    Portland, Oregon U.S.A.
    Posts
    443
    Thanks
    108
    Thanked 15 Times in 14 Posts

    isset() Demo?

    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.

    Code:
    <select id="product_category">
       <option></option>
       ...
       ...
    </select>
    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.

    Code:
    <select id="product_name">
       <option></option>
       ...
       ...
    </select>
    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???

  • #2
    Regular Coder hinch's Avatar
    Join Date
    Sep 2005
    Location
    UK
    Posts
    923
    Thanks
    25
    Thanked 80 Times in 80 Posts
    if (isset($_POST['postvar'])) {
    echo "isset returns true as post var is populated";
    } else {
    echo "isset returns false as postvar is unpopulated";
    }
    A programmer is just a tool which converts caffeine into code

    My work: http://www.fcsoftware.co.uk && http://www.firstcontactcrm.com
    My hobby: http://www.angel-computers.co.uk
    My life: http://www.furious-angels.com

  • #3
    Regular Coder PHP6's Avatar
    Join Date
    Aug 2008
    Location
    Czech Republic
    Posts
    239
    Thanks
    18
    Thanked 34 Times in 33 Posts
    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

  • #4
    ubh
    ubh is offline
    Regular Coder ubh's Avatar
    Join Date
    Apr 2008
    Location
    Portland, Oregon U.S.A.
    Posts
    443
    Thanks
    108
    Thanked 15 Times in 14 Posts
    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.

    Thanks.

  • #5
    Regular Coder PHP6's Avatar
    Join Date
    Aug 2008
    Location
    Czech Republic
    Posts
    239
    Thanks
    18
    Thanked 34 Times in 33 Posts
    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:

    PHP Code:
    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

  • #6
    ubh
    ubh is offline
    Regular Coder ubh's Avatar
    Join Date
    Apr 2008
    Location
    Portland, Oregon U.S.A.
    Posts
    443
    Thanks
    108
    Thanked 15 Times in 14 Posts
    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.

  • #7
    New Coder
    Join Date
    Jun 2008
    Posts
    64
    Thanks
    25
    Thanked 0 Times in 0 Posts
    you dont need ajax, just use switch statement, your options act as a name and do this
    just change id to name
    PHP Code:
    $product_category $_POST['product_category']
    if(!empty(
    $product_category){
        switch(
    $product_category){
            case 
    "option1":
            
    //.....
            
    break;
            case 
    "option2":
            
    //.....
            
    break;
        }

    Last edited by johnish; 12-05-2008 at 10:27 AM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •