Jump to content

Form Class Radio Field Positioning Issue


OldWest

Recommended Posts

I am doing some practice with classes. MY class below echoes out input and radio fields based on the instantiation of the Form class (the code below is working!), but I am trying to resolve how to get my form radio fields so they are not in between every other form field as they are supposed to be next to eachother. If you run the below, you will see what I mean. And also is my approach at all sensible? I am only doing this for learning experience, so any advice please.

 

<?php
  class Form
  {
      private $fields = array();
      private $radios = array();
      private $actionValue;
      private $submit = "Submit Form";
      private $Nradios = 0;
      private $Nfields = 0;
      function __construct($actionValue, $submit)
      {
          $this->actionValue = $actionValue;
          $this->submit = $submit;
      }
      function displayForm()
      {
          echo "<form action='{$this->actionValue}' method='post'>\n\n";
          for ($j = 1, $i = 1; $j <= sizeof($this->fields), $i <= sizeof($this->radios); $j++, $i++) {
              echo "<p>\n<label>{$this->fields[$j-1]['label']} : </label>\n";
              echo "<input type='text' name='{$this->fields[$j-1]['name']}'>\n</p>\n\n";
              echo "<p>\n<label>{$this->radios[$i-1]['rlabel']} : </label>\n";
              echo "\n<input type='radio' name='{$this->radios[$i-1]['rname']}' value='{$this->radios[$i-1]['rvalue']}'>\n</p>\n\n";
          }
          echo "\n\n<input type='submit' value='{$this->submit}'>\n</form>";
      }
      function addField($name, $label)
      {
          $this->fields[$this->Nfields]['name'] = $name;
          $this->fields[$this->Nfields]['label'] = $label;
          $this->Nfields = $this->Nfields + 1;
      }
      function addRadio($rname, $rvalue, $rlabel)
      {
          $this->radios[$this->Nradios]['rname'] = $rname;
          $this->radios[$this->Nradios]['rvalue'] = $rvalue;
          $this->radios[$this->Nradios]['rlabel'] = $rlabel;
          $this->Nradios = $this->Nradios + 1;
      }
  }
?>

<?php
  $contact_form = new Form("process.php", "Submit Data >>");
  $contact_form->addField("first_name", "First Name");
  $contact_form->addField("last_name", "Last Name");
  $contact_form->addRadio("gender", "male", "Male");
  $contact_form->addRadio("gender", "femail", "Female");
  $contact_form->displayForm();
?>

 

they are in between because you are looping through each time for each input and each radio

 

i tested this try it this way

 

echo "<form action='{$this->actionValue}' method='post'>\n\n";
    for ($j = 1; $j <= sizeof($this->fields); $j++) {
        echo "<p>\n<label>{$this->fields[$j-1]['label']} : </label>\n";
        echo "<input type='text' name='{$this->fields[$j-1]['name']}'>\n</p>\n\n";
    }
    for ($i = 1; $i <= sizeof($this->radios); $i++) {
        echo "\n<label>{$this->radios[$i-1]['rlabel']} : </label>\n";
        echo "\n<input type='radio' name='{$this->radios[$i-1]['rname']}' value='{$this->radios[$i-1]['rvalue']}'>\n\n";
    }		  	
echo "\n<br/>\n\n<input type='submit' value='{$this->submit}'>\n</form>";

Nice idea. The double for loops seem like the best solution. . Thx.

 

they are in between because you are looping through each time for each input and each radio

 

i tested this try it this way

 

echo "<form action='{$this->actionValue}' method='post'>\n\n";
    for ($j = 1; $j <= sizeof($this->fields); $j++) {
        echo "<p>\n<label>{$this->fields[$j-1]['label']} : </label>\n";
        echo "<input type='text' name='{$this->fields[$j-1]['name']}'>\n</p>\n\n";
    }
    for ($i = 1; $i <= sizeof($this->radios); $i++) {
        echo "\n<label>{$this->radios[$i-1]['rlabel']} : </label>\n";
        echo "\n<input type='radio' name='{$this->radios[$i-1]['rname']}' value='{$this->radios[$i-1]['rvalue']}'>\n\n";
    }		  	
echo "\n<br/>\n\n<input type='submit' value='{$this->submit}'>\n</form>";

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.