SitePoint Sponsor

User Tag List

Results 1 to 15 of 15
  1. #1
    SitePoint Zealot
    Join Date
    Jul 2002
    Posts
    173
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Coding Woes inserting drop-down $var

    I finally got error message stuff working ok, and can populate regular text fields with $vars no problem.

    Where I am stuck now is with the drop-down list choices on the form.

    If a selection has been made, I need to have that selection show. My attempted approach is:
    PHP Code:
    ...html form code
    <td class=addrlabel>State:<br>
    <?php
    if($state != "" ) {
    echo 
    "<select name=\"state\" id=\"select\">";
    echo 
    "<option value=\"" "$state"\">" "$state"// STUCK HERE
    echo "</select>";
    } else
    ?>
    <select name="state" id="select">
    <option value="" selected>SELECT STATE
    <option value="AK">Alaska
    ###snipped rest of lengthy list for post###
    1. I'm "STUCK HERE" (see above comment)
    2. Do I have to use </option> on each line?
    3. If I insert the <?php & ?> section with the { else { ... must I then recode the entire option list with php echo lines?

    Will this approach even work???

    Thanks a lot.

  2. #2
    orange pips! orange pips!
    Join Date
    Oct 2001
    Location
    Its all about location
    Posts
    652
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    After the user makes a selection, are you trying to get show what the user just selected? Like, if you had 50 states and I selected VA, I submitted the page and it came back with an error, you want the select list to have VA selected? If so, I think code would be this:

    PHP Code:
    echo("<select name='state' id='select'>");
       if (
    $state)
       {
          echo(
    "<option value='$state' selected>$state</option>")
       }
    echo(
    "</select>"); 
    Or did I totally miss the problem?

  3. #3
    SitePoint Zealot
    Join Date
    Jul 2002
    Posts
    173
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hello krayziepjf ...

    Well, essentially yes, thanks.

    However...Ijust realized another major problem here.

    The form gets processed SELF/POST, and I did just get another test to work with:[PHP
    if($state != "" ) {
    echo "<select name=\"state\" id=\"select\">";
    echo "<option value=\"" . "$state" . "\">" . "$state";
    echo "</select>";
    }

    [/PHP]But, two things appear to be a fly-in-the-ointment:

    1. If I keep the initial drop-down list, then what shows after the 'POST' are TWO sets of drop-downs...one with the Selected State and one below still saying "Select State".

    2. And also what happens, is that if there are other errors still to correct on the Page, clicking Submit wipes out the original choice (actually a set var for state in my test).

    3. Wouldn't I still have to use an { else } in there to basically show the original list of choices, and then hide it after the POST? But that seems to be the problem when other errors still exist.

    Thanks. Will be gone for about 8 hours now.

    (EDITA thought just came to mind, but not sure how to do it exactly or if it would work.

    That would be to use some kind of 'counter' *within* the insert loop, and if the selection has been made once, then somehow keep it 'fixed' in place. But then, what if the person screwed up and still needed to change it?

    ARRRGGGHHHH...this stuff gets mind boggling. Of course, being a 'n00b' doesn't help

  4. #4
    orange pips! orange pips!
    Join Date
    Oct 2001
    Location
    Its all about location
    Posts
    652
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Can you quickly post the link that we can view? I'm not sure how you're setup is going or what you're real intent is.

  5. #5
    SitePoint Zealot
    Join Date
    Jul 2002
    Posts
    173
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just leaving but saw your note.

    The whole thing is not completed yet, but just realized something else.

    I'm selecting a value (like "AR" for Arkansas), but the initial drop-down displays the full State Name.

    Umh, if a selection is made, then my approach just sticks in the "AR"...ahhhh, I guess that's okay.
    PHP Code:
    ...html code...
    <td class=addrlabel>State:<br>
    <?php
    if($state != "") {
    echo 
    "<select name=\"state\" id=\"select\">";
    echo 
    "<option value=\"" "$state"\">" "$state";
    echo 
    "</select>";
    }
    //} else
    ?>
              <select name="state" id="select">
              <option value="" selected>SELECT STATE
              <option value="AK">Alaska
    ...snipped for brevity
    This is the relevant part of what is not completely finished yet.

    ...snipped for brevity

  6. #6
    orange pips! orange pips!
    Join Date
    Oct 2001
    Location
    Its all about location
    Posts
    652
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    My opinion is this:

    PHP Code:
    echo("<form name='thisForm' action='post' method='whatever.php'>");
       
    //put your other contents here.
       
       //put your state selection here
       //build your list of states
       
    $statesArray = array("AR" => "Arkansas""PA" => "Pennsylvania" ...);
       echo(
    "<select name='state'>");
          
    //echo out all your states like such
          
    foreach($statesArray as $abbr => $name)
          {
             if (
    $state == $abbr)
             {
                echo(
    "<option value='$abbr' selected>$name</option>");
             }
             else
             {
                echo(
    "<option value='$abbr'>$name</option>");
             }
          }
       echo(
    "</select>");
    echo(
    "</form>"); 
    Here, you build an array of all the states, loop through them one by one to build the select list, and check if the $state variable matches one of the abbreviations. If it does, it will select that one for the user when the page is reloaded. If it's the first time the page is loaded, it won't select any since $state is null.

  7. #7
    SitePoint Zealot
    Join Date
    Jul 2002
    Posts
    173
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi again, krayziepjf

    I really appreciate your helpfulness here.

    RE:
    PHP Code:
    [font=Courier New][font=Verdana][color=#006600]$statesArray = array("AR" => "Arkansas", "PA" => "Pennsylvania" ...);[/color][/font][/font][color=#006600]
    [/color

    Gosh...won't that get pretty 'ugly' for 50 States all strung out? I'm trying to avoid a separate MySQL db/table at the moment, but that would probably be cleaner.

    FYI, I've just about got all the rest of the stuff working now,except for the darn 'checkbox' issue.

    If all the 'Billing' field info is filled in, I have a non-JS checkbox to assign all the Billing info to the related Shipping fields. But I can't figure out how to keep the checkbox 'checked' (and showing a checkmark) each time a form POST cycles.

    I tried some code another person gave me a while back, but it doesn't seem to work. My checkbox name is 'bill2ship' ...
    PHP Code:
    if ($bill2ship == "1" ) {
    $ship_to_first_name $first_name;
    $ship_to_last_name $last_name;
    $ship_to_address $address;
    $ship_to_city $city;
    $ship_to_state $state;
    $ship_to_zip $zip;
    $ship_to_country $country;
    } else {
    if ((
    $ship_to_first_name == "" ) && ($bill2ship != "1" )) {$error .= "Please Enter Shipping First Name<br>"; } 
    if ((
    $ship_to_last_name == "" ) && ($bill2ship != "1" )) {$error .= "Please Enter Shipping Last Name<br>"; }
    if ((
    $ship_to_company != "" ) && ($bill2ship != "1" )) {$error .= "Please Enter Shipping Company Name<br>"; }
    if ((
    $ship_to_address == "" ) && ($bill2ship != "1" )) {$error .= "Please Enter Shipping Address<br>"; }
    if ((
    $ship_to_city == "" ) && ($bill2ship != "1" )) {$error .= "Please Enter Shipping City<br>"; }
    if ((
    $ship_to_city == "" ) && ($bill2ship != "1" )) {$error .= "Please Enter Shipping City<br>"; }
    if ((
    $ship_to_state == "" ) && ($bill2ship != "1" )) {$error .= "Please Enter Shipping State<br>"; }
    if ((
    $ship_to_country == "" ) && ($bill2ship != "1" )) {$error .= "Please Enter Shipping Country<br>"; }

    Here's what I've got so far in the HTML section:
    PHP Code:
    <td align="left" colspan="4"><b><font color="#000000" size=-2>
    <?php
    if($bill2ship == "1" ) {
    echo 
    "<input type=\"checkbox\" class=submitbutton name=\"bill2ship\" value=\"1\">";
    } else {
    echo 
    "<input type=\"checkbox\" class=submitbutton name=\"bill2ship\" value=\"1\">";
    }
    ?>
    <---Click Here If Shipping & Billing Are Same.</strong></font></b>
    </td>
    I tried adding 'checked' to the PHP insert code, but that only displayed the word 'checked' [img]images/smilies/mad.gif[/img]

    Any ideas about how to solve this?

    Thanks much.

  8. #8
    orange pips! orange pips!
    Join Date
    Oct 2001
    Location
    Its all about location
    Posts
    652
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    To have a checkbox be checked, you must do

    <input type='checkbox' name='bill2ship' checked>

  9. #9
    SitePoint Zealot
    Join Date
    Jul 2002
    Posts
    173
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, thanks...but this does not keep the checkbox "checked" on-screen after doing a POST (all my $vars, etc. entered do show in the fields OK).
    PHP Code:
    <?php
    if($bill2ship == 1) {
    echo 
    "<input type=\"checkbox\" class=submitbutton name=\"bill2ship\" checked>";
    } else {
    echo 
    "<input type=\"checkbox\" class=submitbutton name=\"bill2ship\" value=\"1\">";
    }
    ?>
    Dunno why it won't stay checked!

    I now have this near the top:
    PHP Code:
    if (($bill2ship "checked" ) || ($bill2ship == 1)) {
    $ship_to_first_name $first_name;
    $ship_to_last_name $last_name;
    $ship_to_company $company// ADDED
    $ship_to_address $address;
    $ship_to_city $city;
    $ship_to_state $state;
    $ship_to_zip $zip;
    $ship_to_country $country;
    } else {
    ...
    error messages 
    And this in the form section:
    PHP Code:
    if(($bill2ship == 1) || ($bill2ship == "checked" )) {
    echo 
    "<input type=\"checkbox\" class=submitbutton name=\"bill2ship\" \"checked\">";
    } else {
    echo 
    "<input type=\"checkbox\" class=submitbutton name=\"bill2ship\" value=\"1\">";
    }
    ?> 
    ...but it still doesn't work
    Last edited by RocketMan; Oct 4, 2003 at 13:53.

  10. #10
    SitePoint Addict CeleronXL's Avatar
    Join Date
    Dec 2002
    Location
    United States of America
    Posts
    349
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just as a side note, make sure that you retain <option> closing tags.

    <option>Something</option>
    <option>Something</option>

    As opposed to

    <option>Something
    <option>Something else

    Not that that'll fix your problem.
    So hold me when I'm here
    Right me when I'm wrong
    Hold me when I'm scared
    Love me when I'm gone.

  11. #11
    SitePoint Zealot
    Join Date
    Jul 2002
    Posts
    173
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    CeleronXL:

    Thanks for the tip!

  12. #12
    orange pips! orange pips!
    Join Date
    Oct 2001
    Location
    Its all about location
    Posts
    652
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry Rocketman, I totally missed that in your last post. When you check $bill2ship, it will either be null if the box wasn't selected or "on" when the box is selected. So you have two options, either will work:

    PHP Code:
    if ($bill2ship) ...
    //or
    if ($bill2ship == "on") ... 

  13. #13
    SitePoint Zealot
    Join Date
    Jul 2002
    Posts
    173
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Gosh, thanks...but that still doesn't work...the checkbox does NOT remain showing "checked" after a SELF/POST, although any entered field $vars do OK.

    I'm dumfounded

  14. #14
    SitePoint Zealot
    Join Date
    Jul 2002
    Posts
    173
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    YaaaaaaHOOOOOOOOOOOOOOOOOOO...

    Gheez...thanks for all your kind efforts here,krayziepjf

    I just got this to work:
    PHP Code:
    if ($bill2ship == 1) {...misc 
    ...and inside the form:
    PHP Code:
    <?php
    if($bill2ship == 1) {
    echo 
    "<input type=\"checkbox\" name=\"bill2ship\" class=submitbutton checked>";
    } else {
    echo 
    "<input type=\"checkbox\" name=\"bill2ship\" class=resetbutton value=\"1\">";
    }
    ?>
    I figure about 5 hours on this thing, but what helped solve it was adding two of my different .css classess so I could watch...and now...the 'outside' of the box changes color too.

    (EDIT AWWW NUTS... it changes color after the 1st POST cycle, but on the 2nd POST cycle, it nukes out the selection again.

    That's why I was trying to also figure out if there is some way to put in an incremental counter to somehow have it 'hold state' REGARDLESS of how may POST cycles.

    Any thoughts are EXTREMELY welcomed!!!

    Thanks.

  15. #15
    SitePoint Zealot
    Join Date
    Jul 2002
    Posts
    173
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    HEYYYYYYY...

    I just kept trying anything which came to mind and NOW...FINALLY...I think I've got it:

    Up above...
    PHP Code:
    if ($bill2ship ==1) {
       
    $bill2ship == 1;
    } else {
       
    $bill2ship $_POST['bill2ship'];

    ...and
    PHP Code:
    if ($bill2ship == 1) {
     
    $ship_to_first_name $first_name;
     ...
    blah blah
     $keepbill2ship 
    == 1// TEST
     
    $bill2ship ==1// TEST { 
    ...and then inside the form:
    PHP Code:
    <?php
    if($bill2ship == 1) {
        echo 
    "<input type=\"checkbox\" name=\"bill2ship\" class=submitbutton value=1 checked>";
    } elseif (
    $keepbill2ship ==1) {
     echo 
    "<input type=\"checkbox\" name=\"bill2ship\" class=submitbutton value=1 checked>";
    } else {
     echo 
    "<input type=\"checkbox\" name=\"bill2ship\" class=resetbutton value=1>";
    }
    ?>
    OK...now I can finally go get something to eat

    Tnx.


Bookmarks

Posting Permissions

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