PHP Form - Checkbox Issue

Discussion in 'Web Design and Development' started by linux2mac, May 11, 2010.

  1. linux2mac macrumors 65816

    linux2mac

    Joined:
    Aug 29, 2009
    Location:
    "City of Lakes", MN
    #1
    Hello,

    I have a PHP web form that populates from a MySQL database. I have check boxes on that form that will be marked as checked depending on a "yes" or "no" value from the database. What is strange is that in FireFox, the checkboxes that are flagged to be checked from the database are indeed checked. When viewing the same form in Safari, the checkboxes are blank.

    Does anyone know why the checkboxes display as blank in Safari but not Firefox. I am running Firefox 3.6 and Safari 4.0.5. MySQL version is 5.0.26 and PHP is 5.2.5.

    Thanks.

    Mike
     
  2. citizenzen macrumors 65816

    Joined:
    Mar 22, 2010
    #2
    I just completed a PHP form and got it to work. But it's hard to tell what's going on without seeing your code.
     
  3. Darth.Titan macrumors 68030

    Darth.Titan

    Joined:
    Oct 31, 2007
    Location:
    Austin, TX
    #3
    Without a code sample, I'm just making a stab in the dark, but sometimes people just output selected checkboxes like this:
    PHP:
    <input type="checkbox" name="name" value="true" checked />
    And that works... in some browsers. To be completely correct it should really be:
    PHP:
    <input type="checkbox" name="name" value="true" checked="checked" />
    See if that might be the issue.
     
  4. linux2mac thread starter macrumors 65816

    linux2mac

    Joined:
    Aug 29, 2009
    Location:
    "City of Lakes", MN
    #4
    <?php
    require_once("globals.php");
    include_once("$path/api.inc");


    $obj = formFetch("form_candidate_review", $_GET["id"]); #Use the formFetch function from api.inc to get values for existing form.

    function chkdata_CB(&$obj, $nam, $var)
    {
    $objarr = explode(',',$obj{$nam});
    foreach ($objarr as $a)
    {
    if ($a == "$var")
    {
    $result = "\"checked\"";
    }
    }
    return $result;
    }

    ?>


    <td><label><input type="checkbox" name="first_time_check" readonly="ReadOnly" disabled="true" value="yes" <?php $result = chkdata_CB($obj,"first_time_check","yes"); echo $result;?>/></label></td>

    This code works in FireFox but not Safari. Fields that have a checked value in the database display check marks in the corresponding check boxes in FireFox when viewing the form but display without check marks in Safari.

    I would prefer to use Safari to view my web form and not sure if this is a Safari setting issue, PHP version issue, or code issue. Thanks.

    -Mike
     
  5. Darth.Titan macrumors 68030

    Darth.Titan

    Joined:
    Oct 31, 2007
    Location:
    Austin, TX
    #5
    If it's a browser issue, then what we need to see is the actual HTML output of the script. The PHP code is run server side, so all the browser sees is the resulting HTML.
    I'm still curious as to how your code outputs the 'checked' attribute.

    Edit:
    On second viewing of your post, I believe I'd try changing the line:
    PHP:
    $result "\"checked\""
    to
    PHP:
    $result "checked=\"checked\"";
     
  6. linux2mac thread starter macrumors 65816

    linux2mac

    Joined:
    Aug 29, 2009
    Location:
    "City of Lakes", MN
    #6
    Here is the HTML output for both browsers that is created by the PHP code.

    FIREFOX
    <td><label><input type="checkbox" name="first_time_check" readonly="ReadOnly" disabled="true" value="yes" "checked"/></label></td>

    SAFARI
    <td><label><input type="checkbox" name="first_time_check" readonly="ReadOnly" disabled="true" value="yes" "checked"/></label></td>

    I am going to manipulate the PHP code that generates the HTML and see what happens when I use:
    Code:
     checked = "checked"
     
  7. linux2mac thread starter macrumors 65816

    linux2mac

    Joined:
    Aug 29, 2009
    Location:
    "City of Lakes", MN
    #7
    Yes, that worked!!

    I needed to make the PHP code generate the HTML per your suggestion:
    Code:
    checked = "checked"

    SAFARI HTML
    <td><label><input type="checkbox" name="first_time_check" readonly="ReadOnly" disabled="true" value="yes" checked="checked"/></label></td>


    Thanks Darth.Titan!!

    -Mike
    P.S. What Mac editor do you use? I am thinking of getting Coda or Coda2 (if it releases soon).
     

Share This Page