Everybody! This is important. In a few days, these forums will be moving over to using the totally sweet Discourse platform. To ensure this migration happens smoothly with no loss of content, these forums are currently in a read-only mode. I do apologize for the inconvenience.

There is never a good time to turn the forums off for an extended period of time, but I promise the new forums will be a billion times better. I'm pretty sure of it.

See you all on the other side in a few days, and if you have any (non-technical) questions, please e-mail me at kirupa@kirupa.com. For technical questions, try to find a tutorial that corresponds to what you are looking for and post in the comments section of that page.

Cheers,
Kirupa

Results 1 to 7 of 7

Thread: Figure this out?

  1. #1

    Figure this out?

    I got the form to search right for one dropdown posting to a session.
    I am trying to duplicate it for 2 more dynamically generated dropdowns.
    The session looks like this:
    PHP Code:
    <?PHP 
    session_start
    (); // start session
    $_SESSION['search']= $_POST['cat'];

    ?>
    Then like this on the query page:
    PHP Code:
    $_POST['cat']=$_SESSION['search']; 
    I have been trying to add the value of $_POST['city'] in a variety of ways and nothing seems to work.

    I added another $_SESSION['city'] =$_POST['city']; (// on both sides)
    nada... didn't work.
    I tried to make $_SESSION['search'] = $_POST['cat'] && $_POST['city'];
    didn't work.

    I tried to make an array $sarr['cat']=$_POST['cat']; and have $_SESSION['search = $_POST['$sarr'];

    None of this has worked.
    Anybody got any ideas?
    There are 3 dropdown values that need to be passed with the session.
    I tried assigning $sarr=array(1 =>$_POST['cat'],2=>$_POST['city']);
    that didn't work either.
    What am I doing wrong please???
    Thanks.

  2. #2
    Step1: Breeeaaath

    I have little problems like this alot, most of the time it's caused by me staring at the code for too long.

    you can't set any variable to a global variable like POST.. post is declared when people fill in a form element you can't change it with code.

    so:
    PHP Code:
    $_POST['cat']=$_SESSION['search']; 
    Is invalid markup.. it wont work. POST globals are declared by forms.

    ------------------------------------------

    so first of all you have to set the value you want to the session:
    PHP Code:
    $_SESSION['search']= $_POST['cat']; 
    that is perfecly fine. You can declare and change the value of session variables.

    but when you go to refer to the stored value you have to set it to a standard variable like:
    PHP Code:
    $cat $_SESSION['search']; 
    you don't really need to do this.. its usually done for ease of use. You can refer to it in code using the long global name of $_SESSION['search'] every time you need it... but thats not very clean.

    then to pass that value to another dropdown element (which is what im guessing your trying to do) you would do this:
    PHP Code:
    echo '<form>
    <select name="search">
            <option value=" ' 
    $cat .' ">' $cat '</option>
            <option value="other value">Other Values</option>
    </select>
    </form>'

    of course the form would have to be properly completed.

    But as you can see because the top option is set with the variable of what they selected it will carry over the info from the last page.

    If this isn't what you were talking about let me know.

    It was kinda hard to tell from you frantic "OMG why isn't it working" post
    Last edited by birdwing; July 24th, 2009 at 06:50 PM.

    Twitter / The Human Conditions

    biznuge: "that doesn't grammatical sense..."

  3. #3
    Quote Originally Posted by birdwing View Post
    Step1: Breeeaaath

    I have little problems like this alot, most of the time it's caused by me staring at the code for too long.

    you can't set any variable to a global variable like POST.. post is declared when people fill in a form element you can't change it with code.

    so:
    PHP Code:
    $_POST['cat']=$_SESSION['search']; 
    Is invalid markup.. it wont work. POST globals are declared by forms.

    ------------------------------------------

    so first of all you have to set the value you want to the session:
    PHP Code:
    $_SESSION['search']= $_POST['cat']; 
    that is perfecly fine. You can declare and change the value of session variables.

    but when you go to refer to the stored value you have to set it to a standard variable like:
    PHP Code:
    $cat $_SESSION['search']; 
    you don't really need to do this.. its usually done for ease of use. You can refer to it in code using the long global name of $_SESSION['search'] every time you need it... but thats not very clean.

    then to pass that value to another dropdown element (which is what im guessing your trying to do) you would do this:
    PHP Code:
    echo '<form>
    <select name="search">
            <option value=" ' 
    $cat .' ">' $cat '</option>
            <option value="other value">Other Values</option>
    </select>
    </form>'

    of course the form would have to be properly completed.

    But as you can see because the top option is set with the variable of what they selected it will carry over the info from the last page.

    If this isn't what you were talking about let me know.

    It was kinda hard to tell from you frantic "OMG why isn't it working" post
    LOL you are right... I did need to breathe!!!!

    Ok, here it is live. I got the category to work and am trying to add the city.
    It is a form, the part I posted does work for the categories. I cannot seem to find a way to .= the city. Hey would that work???? LOL

    www.weatherradar.us/c.php

  4. #4
    this form is supposed to add a business with its name and a category and city?

    because no matter what I do i get the same results.


    Is this PHP with a sql database for the information?

    Twitter / The Human Conditions

    biznuge: "that doesn't grammatical sense..."

  5. #5
    Quote Originally Posted by birdwing View Post
    this form is supposed to add a business with its name and a category and city?

    because no matter what I do i get the same results.


    Is this PHP with a sql database for the information?
    No, its a test form.
    There are 2 categories in the db with ??? howevermany cities. Both drop downs are dynamically generated from mysql db.
    If you select category play and click submit you get 2 results back in your list. Selecting Sights gives you just the sights.
    I need the city to work the same way at the same time.
    If I switch $_POST['cat'] with $_POST['city'] the city dropdown works so I know it isn't that.
    I need to know how to add BOTH the cat and city post data to the $_SESSION[' search'].
    There will be three dropdowns eventually. I got one to work but cannot seem to properly write the code to add a second post data to the same session.
    I actually think it is just my array syntax, just can't see the trees for the forest so to speak.
    If I set $_SESSION['search'] = $_POST['$sarr'];
    $sarr=array($_POST['cat'],$_POST['city']);
    I get nothing with either dropdown.
    I used the $_POST['$sarr[0]'] for my value in the search.
    Not clear why it didn't work. I have been studying the array tutorials, I must be missing something.

  6. #6
    I was playing with the array syntax... sorry. Go look now I will leave it alone while I have dinner...my bad.

  7. #7
    its fine

    ok the problem is probably how your pulling the information from the database.. the AND statement can be very useful.

    lets say everything is in a table named "bus".

    Each row holds one entry and each has 4 columns: Name, cat, city, and bus_id(set to auto_increment for identification purposes).

    the global $_SESSION variables isn't 1 variable per session. You can use as many of those as you would like.
    So set your variables to be sent to the next page.

    PHP Code:
    $_SESSION['cat'] = $_POST['cat'];
    $_SESSION['city'] = $_POST['city']; 
    Now these can be retrieved on the next page.

    PHP Code:
    $cat $_SESSION['cat'];
    $city $_SESSION['city']; 
    The query you need to use to pull the proper information from you sql database with the table set up the way I suggested

    Code:
    SELECT name, cat, city FROM bus WHERE cat='$cat' AND city='$city';
    this will create a multidimensional array holding the data (when pulled into php)

    so if the info was
    row1: Waterville USA, Gulf Shores, Play
    row2: Birmingham Zoo, Birmingham, Sights

    $array[0][1] would = "Gulf Shores"
    $array[0][0] would = "Waterville USA"
    $array[1][2] would = "Sights"

    ect...

    If you have any questions.. or problems let me know

    Twitter / The Human Conditions

    biznuge: "that doesn't grammatical sense..."

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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

Home About kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012