Alright. I have a form.
(http://www.swhyperspace.com/expansion/form.php)
Now I am writing the update script, same thing as hte form, but contains information already from a specified row in the dbase.

I can handle the text fields, but what about the checkboxes and radio buttons? where do i inject the php code tomake the proper ones selected, and the inproper ones not?

    Hi,

    to mark a certain checkbox/radiobuttons as checked simply add checked e.g.

    <checkbox ... checked>
    <radiobutton ... checked>

    To be xhtml compliant it might be

    <checkbox ... checked="yes" />
    <radiobutton ... checked="yes" />

      if( $row[col] == 1 ) //check to see if it is a yes, or 1, or whatever
      {
      $checked = "checked";
      }
      do that before you echo the form. when you get to the checkbox, stick this in the tag:
      <checkbox name=blah $checked>

        ok this is what i have devised

        	for($i=1; $i<=11; $i++) { 
        		$c = "c" . $i;
        		 if ($row[$c] == 1 ) { //check to see if it is a yes, or 1 
        		 $chkStr = "checked" . $i;
        		 ${$chkStr} = "checked";
        		 } 
        	} 

        so when $i == 1
        $chkStr == $checked1 correct?

          <?php
          /*This (directly below) is designed for checkboxes, it is in a script i wrote
          that generates an update form with existing data from a certain row in the
          dbase, it checks each checkbox and if its 1 (true) it puts a variable that is
          set to 'checked' how can i modify it for radio buttons, the radio button has
          one group name.. and below it increments the field name. The values of the radio
          button (field named 'radio') has values from 1 to 30..*/ 
          for($i=1; $i<=12; $i++) {   
          $c = "c" . $i; //the fields are c1-c12 this line adjusts the loop to reflect if ($row[$c] == "1" ) {
          $chkOth = "checked" . $i;
          //define the variable to be put in each field if its 1 it will be checked ${$chkOth} = "checked";
          }
          }
          // ...WITHOUT doing the below (doing each one manually)... if ($row['radio'] == "0") {
          $chk0 = "checked";
          } elseif ($row['radio'] == "1") {
          $chk1 = "checked";
          } elseif ($row['radio'] == "2") {
          $chk2 = "checked";
          } elseif ($row['radio'] == "3") {
          $chk3 = "checked";
          . . . . //...and so on until all the radios in the field 'radio' are checked. } elseif ($row['radio'] == "n") {
          $chkn = "checked";
          } ?>

            EDIT: eh nevermind.. that didnt work. delete worthless code.

              Did you do this?

              <checkbox ... <?php echo $chk1; ?>>
              <radiobutton ...<? echo $chk2; ?>> 
              

                i did with the checkboxes but it doesnt work like that with the raido buttons, you obviously dont understand.

                the for loop won't work, with radio buttons, because the radio buttons all share the same field name, and the checkboxes don't.

                  Hi,

                  if all radio buttons have the same name they'll be a group. Only one radio button of a group can be checked at a time. If you want to change this you would have to use different names or to split them up in more groups.

                    yes yes yes, i know please tell me something i dont know.

                    i want them all in one group, under one name (which they are) the last code i posted with //coments explains perfectly well.. i think

                      Hi,

                      I hope I'm getting to the point this time. So what you want is to check one of the radio buttons according to the vaule in $row['radio'] ?

                      I noticed that you used $row['radio'] in your if ... else ... if ... else ... statement but $row['c1'] $row['c2'] and so on in your for loop.

                      Try this one:
                      First initialize all variables:

                      for ($i=1;$i<=30;$i++) {
                      ${"checked".$i} = "";
                      }

                      Then just set the checked variable referred by the field value:

                      ${"checked".$row['radio']} = "checked";

                      Do you have hmtl code like this ?

                      <input type="radio" ..... <?PHP echo $checked1; ?>>
                      <input type="radio" ..... <?PHP echo $checked2; ?>>
                      ....

                      Hope I'm right this time ?

                        Hi,

                        assuming that I am right:

                        I read the source code of your form on that site. Do you output the radio inputs automated in e.g. a for or while loop. If so you could just output "checked" within that loop if the $row['radio'] value is 1 for that radio button/character (since you would already access the $row variable from within that loop to output the name. Hope my description is clear enough.

                          Write a Reply...