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.
Page 1 of 2 12 LastLast
Results 1 to 15 of 28
  1. #1
    New Coder
    Join Date
    May 2007
    Posts
    56
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Select Drop Down Not Retaining DB Value

    After a user enters information into a db I have an edit.php page where the user can update the information they enter.

    While the edit.php page works correctly, it doesn't retain the value the user selected from select drop down / combo box type fields.

    For example, I have a <select><option> type field for country and while it will pull up all the countries in tluCountries, it doesn't remember the value the user selected.

    My code looks like this:

    PHP Code:
    //open db 
    //execute sql



    $countryName = array();
    //last name into array
    $query_country_name = "select CountryName from tluCountries";
    $search_country_name = mysql_query($query_country_name)or die(mysql_error());
    while ($rowN = mysql_fetch_assoc($search_country_name))
    {
    array_push($countryName, $rowN["CountryName"]);
    }

    $mineral_country = $_POST['mineral_country'];

    <select name="mineral_country"size="1" style="width: 50mm">
    <?php foreach($countryName as $valueN) { ?>
    <option value="<?= $valueN ?><? if ($_POST['CountryName'] == $valueN){ echo "selected=\"selected\"";}?>><?= $valueN ?></option>
    <?
    }
    ?>
    Does anyone see what is wrong? Thanks.

  • #2
    Senior Coder whizard's Avatar
    Join Date
    Jan 2005
    Location
    Philadelphia, PA, USA
    Posts
    1,662
    Thanks
    14
    Thanked 76 Times in 76 Posts
    Try printing out $_POST['CountryName'] to see what value it holds

    HTH
    Dan
    PHP Tip: If you want to use short tags (<? or <?=$var) then make sure short_open_tag is set to "1". It really helps.

    Don't forget to save everyone time and mark your thread as Resolved :)

    "Also note that it is your responsibility to die() if necessary."

    DON'T USE THE MYSQL_ EXTENSION

  • #3
    New Coder
    Join Date
    May 2007
    Posts
    56
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for the help.

    When I print the following I get:

    PHP Code:
    echo 'country name = '.$mineral_country;

    /* the output I get is:
    country name = Array
    */ 

  • #4
    teh Moderatorinator
    Join Date
    Sep 2004
    Location
    USA
    Posts
    2,472
    Thanks
    4
    Thanked 40 Times in 40 Posts
    You named it mineral_country but are referencing Countryname. Change it to mineral_country and give it go.

    Good luck.

  • #5
    New Coder
    Join Date
    May 2007
    Posts
    56
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Brandoe85 View Post
    You named it mineral_country but are referencing Countryname. Change it to mineral_country and give it go.

    Good luck.
    Thanks for the help. Here is the code now:

    PHP Code:
    //open db and conn



    $mineral_country = array();
    //last name into array
    $query_country_name = "select CountryName from tluCountries";
    $search_country_name = mysql_query($query_country_name)or die(mysql_error());

    while ($rowN = mysql_fetch_assoc($search_country_name))
    {
    array_push($mineral_country, $rowN["mineral_country"]);
    }

    $mineral_country = $_POST['mineral_country'];

    <select name="mineral_country"size="1" style="width: 50mm">
    <?php foreach($countryName as $valueN) { ?>
    <option value="<?= $valueN ?><? if ($_POST['mineral_country'] == $valueN){ echo "selected=\"selected\"";}?>><?= $valueN ?></option>
    <?
    }
    ?>
    </select>Country Name<br>

    /*
    Here are the echo statements:
    echo 'country name = '.$mineral_country.'<br>';
    echo "$mineral_country = ".$_POST['mineral_country'];

    And here is the output:

    country name = 
    = Array

    */
    Now, I get no countries shown when I click the <select><option> area for country. Any ideas?

  • #6
    Senior Coder whizard's Avatar
    Join Date
    Jan 2005
    Location
    Philadelphia, PA, USA
    Posts
    1,662
    Thanks
    14
    Thanked 76 Times in 76 Posts
    in your foreach loop, where is the variable '$countryName' getting it's value?

    Dan
    PHP Tip: If you want to use short tags (<? or <?=$var) then make sure short_open_tag is set to "1". It really helps.

    Don't forget to save everyone time and mark your thread as Resolved :)

    "Also note that it is your responsibility to die() if necessary."

    DON'T USE THE MYSQL_ EXTENSION

  • #7
    New Coder
    Join Date
    May 2007
    Posts
    56
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by whizard View Post
    in your foreach loop, where is the variable '$countryName' getting it's value?

    Dan
    Thanks, I missed that when I was changing it over. It now reads:

    PHP Code:
    <select name="mineral_country"size="1" style="width: 50mm">
    <?php foreach($mineral_country as $valueN) { ?>
    <option value="<?= $valueN ?><? if ($_POST['mineral_country'] == $valueN){ echo "selected=\"selected\"";}?>><?= $valueN ?></option>
    <?
    }
    ?>
    </select>Country Name<br>

    /*

    echo 'country name = '.$mineral_country.'<br>';
    echo "$mineral_country = ".$_POST['mineral_country'];

    And the output is:

    country name = 
    = Array

    */

  • #8
    Senior Coder whizard's Avatar
    Join Date
    Jan 2005
    Location
    Philadelphia, PA, USA
    Posts
    1,662
    Thanks
    14
    Thanked 76 Times in 76 Posts
    PHP Code:
    while ($rowN mysql_fetch_assoc($search_country_name))
    {
    array_push($mineral_country$rowN["mineral_country"]);
    }

    $mineral_country $_POST['mineral_country']; 
    First, you make $mineral_country into an array and fill it with the values from the database. Then it would appear that you overwrite it with the $_POST value....

    Dan
    PHP Tip: If you want to use short tags (<? or <?=$var) then make sure short_open_tag is set to "1". It really helps.

    Don't forget to save everyone time and mark your thread as Resolved :)

    "Also note that it is your responsibility to die() if necessary."

    DON'T USE THE MYSQL_ EXTENSION

  • #9
    New Coder
    Join Date
    May 2007
    Posts
    56
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by whizard View Post
    PHP Code:
    while ($rowN mysql_fetch_assoc($search_country_name))
    {
    array_push($mineral_country$rowN["mineral_country"]);
    }

    $mineral_country $_POST['mineral_country']; 
    First, you make $mineral_country into an array and fill it with the values from the database. Then it would appear that you overwrite it with the $_POST value....

    Dan
    Thanks for the help. I have now rimmed out
    PHP Code:
    //$mineral_country = $_POST['mineral_country'] 
    And I still get no countries in the <select><option> area. Any ideas?
    Last edited by rock_14; 05-29-2007 at 06:04 PM.

  • #10
    Senior Coder whizard's Avatar
    Join Date
    Jan 2005
    Location
    Philadelphia, PA, USA
    Posts
    1,662
    Thanks
    14
    Thanked 76 Times in 76 Posts
    PHP Code:
    echo 'country name = '.$mineral_country.'<br>';
    echo 
    "$mineral_country = ".$_POST['mineral_country']; 
    And the output is:

    country name =
    = Array

    Is $_POST['mineral_country'] an array? because then your foreach loop wouldn't work

    Dan
    PHP Tip: If you want to use short tags (<? or <?=$var) then make sure short_open_tag is set to "1". It really helps.

    Don't forget to save everyone time and mark your thread as Resolved :)

    "Also note that it is your responsibility to die() if necessary."

    DON'T USE THE MYSQL_ EXTENSION

  • #11
    New Coder
    Join Date
    May 2007
    Posts
    56
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by whizard View Post
    PHP Code:
    echo 'country name = '.$mineral_country.'<br>';
    echo 
    "$mineral_country = ".$_POST['mineral_country']; 
    And the output is:

    country name =
    = Array

    Is $_POST['mineral_country'] an array? because then your foreach loop wouldn't work

    Dan
    Thanks for the help. Yes, $_POST['mineral_country'] is an array. I know I need to change something and also that I need an array somewhere to get the values and loop through them. Any idea on what I can do to get this to work?

  • #12
    New Coder
    Join Date
    May 2007
    Posts
    56
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Can anyone see my mistake now? What the script returns is a <select><option> area but with no values although the drop down box does open up and go down quite a ways like there are values but they aren't visible.


    And the echo returns:

    mineral country = Array

    PHP Code:
    <?php
    //open conn db

    $mineral_country = array();
    $query_country_name "select CountryName from tluCountries";
    $search_country_name mysql_query($query_country_name)or die(mysql_error());

    while (
    $rowN mysql_fetch_assoc($search_country_name))

    {
    array_push($mineral_country$rowN["mineral_country"]);
    }

    ?>

    <select name="mineral_country"size="1" style="width: 50mm">

    <?php foreach($mineral_country as $valueN) { ?>

    <option value="<?= $valueN ?><? if ($_POST['mineral_country'] == $valueN){ echo "selected=\"selected\"";}?>><?= $valueN ?></option>

    <?
    }
    ?>

    </select>Country Name<br>
    <?php echo 'mineral country = '.$mineral_country;?>
    Does anyone see what I am doing wrong?

  • #13
    Senior Coder whizard's Avatar
    Join Date
    Jan 2005
    Location
    Philadelphia, PA, USA
    Posts
    1,662
    Thanks
    14
    Thanked 76 Times in 76 Posts
    Hard for me to say, since I'm not fully sure what it is you're trying to do, but try this:

    PHP Code:
    <?php
    //open conn db

    $mineral_country = array();
    $query_country_name "select CountryName from tluCountries";
    $search_country_name mysql_query($query_country_name)or die(mysql_error());

    while (
    $rowN mysql_fetch_assoc($search_country_name))

    {
    array_push($mineral_country$rowN["mineral_country"]);
    }

    ?>

    <select name="mineral_country"size="1" style="width: 50mm">

    <?php foreach($mineral_country as $valueN) { ?>

    <option value="<?= $valueN ?><? if (in_array($valueN,$_POST['mineral_country'])){ echo "selected=\"selected\"";}?>><?= $valueN ?></option>

    <?
    }
    ?>

    </select>Country Name<br>
    <?php echo 'mineral country = '.$mineral_country;?>
    HTH
    Dan
    PHP Tip: If you want to use short tags (<? or <?=$var) then make sure short_open_tag is set to "1". It really helps.

    Don't forget to save everyone time and mark your thread as Resolved :)

    "Also note that it is your responsibility to die() if necessary."

    DON'T USE THE MYSQL_ EXTENSION

  • #14
    New Coder
    Join Date
    May 2007
    Posts
    56
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by whizard View Post
    Hard for me to say, since I'm not fully sure what it is you're trying to do, but try this:

    PHP Code:
    <?php
    //open conn db

    $mineral_country = array();
    $query_country_name "select CountryName from tluCountries";
    $search_country_name mysql_query($query_country_name)or die(mysql_error());

    while (
    $rowN mysql_fetch_assoc($search_country_name))

    {
    array_push($mineral_country$rowN["mineral_country"]);
    }

    ?>

    <select name="mineral_country"size="1" style="width: 50mm">

    <?php foreach($mineral_country as $valueN) { ?>

    <option value="<?= $valueN ?><? if (in_array($valueN,$_POST['mineral_country'])){ echo "selected=\"selected\"";}?>><?= $valueN ?></option>

    <?
    }
    ?>

    </select>Country Name<br>
    <?php echo 'mineral country = '.$mineral_country;?>
    HTH
    Dan
    Dan,

    Thank you for the help. This generates a different type of error when I try it. The error I get is wrong data type for second argument:

    PHP Code:

    <option value="" <br />
    <
    b>Warning</b>:  in_array() [<a href='function.in-array'>function.in-array</a>]: Wrong datatype for second argument in <b>C:xampphtdocswbaseinput_mineralsvaem.php</bon line <b>40</b><br />
    ></
    option
    The structure of tluCountries is:

    tluCountries:
    CountryID (int 11)
    CountryName (varchar 255)

    Any ideas? And, thanks again for helping!

  • #15
    Senior Coder whizard's Avatar
    Join Date
    Jan 2005
    Location
    Philadelphia, PA, USA
    Posts
    1,662
    Thanks
    14
    Thanked 76 Times in 76 Posts
    That would mean that $_POST['mineral_country'] is not an array..

    do this somewhere:

    print_r($_POST['mineral_country']);

    and see what it returns

    I'm still trying to get the whole picture on what is happening here. Is it something like this?:

    User loads page, after submitting a form. One of the form fields is $_POST['mineral_country']. (what type of field is this?)

    Then, the page pulls out all the countries from a DB. The page compares each country to $_POST['mineral_country'] to see if that was the country the user selected in the last form.

    (I guess I'm having trouble with $_POST['mineral_country'] here. Where does it come from? is it an array, or not? If it is an array, how are you trying to select a certain country in the select box based on its data?)

    Dan
    PHP Tip: If you want to use short tags (<? or <?=$var) then make sure short_open_tag is set to "1". It really helps.

    Don't forget to save everyone time and mark your thread as Resolved :)

    "Also note that it is your responsibility to die() if necessary."

    DON'T USE THE MYSQL_ EXTENSION


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

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