Jump to content

Recommended Posts

Hello all,

 

 

I am trying to code a form that will store phone and address info for a directory. I want to be able to indicate if the person is a head of the house hold so that all others living in that house would be collected under him/her. This is especially important because the directory will likely have several different houses with the same last name. My final output would look like:

 

Smith, John & Sally

Jenny & Joe

123 Here Ave

hometown, PA 17782

 

Smith, Terri & Barbra

Christi

432 There Dr.

hometown, PA 17782

 

etc.

 

Currently I have created the form with a radio button for House Head? (yes, no).

But now I need to set it up so that if the answer is NO - they get to select a House Head from a select box. Then I need to save a parent_id for all children.

 

Any ideas??

 

thanks,

Link to comment
https://forums.phpfreaks.com/topic/205663-parent-child-relationship-on-form/
Share on other sites

Unless I am misunderstanding something this isn't complicated at all. For the select list of heads of household you would need to queery the list and their IDs to populate the select list. I think the problem you are having is that based upon whther the entry is a head of household or not, you need them to enter data into different fields. You can use javascript to hide/show the correct fields to display - but you will need to validate on the server-side as well.

 

Another option that can be used in combination with the javascript would be to put the different group of fields into boxes that make it intuitive to the user which options they need to fill out.

 

Example:

<html>
<body>

Name: <input type="text" name="name" />
<br /><br />

Is this entry a head of household?
<table border="0">
  <tr valign="top">
    <td><input type="radio" name="hoh" value="yes" /> Yes</td>
    <td><fieldset>
        Address: <input type="text" name="address" /><br />
        City, ST ZIP: <input type="text" name="city" />,
                      <input type="text" name="state" size="2" />
                      <input type="text" name="zip" size="5" /></td>
        </fieldset>
  </tr>
  <tr valign="top">
    <td><input type="radio" name="hoh" value="no" /> No</td>
    <td><fieldset>
        Select the head of household:
        <select name="hoh_id">
          <option value="1">Name of HOH1</option>
          <option value="2">Name of HOH2</option>
          <option value="3">Name of HOH3</option>
        </select>
        </fieldset>
    </td>
  </tr>
</table>
</body>

</html>

Hi mjdamato,

 

yep - that is my problem. I plan on using some ajax to make the form user friendly and only include fields that need to be filled out based on the users response.

 

My issue is first - saving the parent_id. If the user is a head of household then the parent_id should = 0 if the user is not then the parent_id will come from the select box (this part I know how to do).

 

So how can I use js to check the radio for head of house and supply a 0 if user is a head...

 

this is what I thought of:

 

<h2>Directory Form</h2>

    <label class="cf_label" style="width: 150px;">Head of House?</label>
    <div class="float_left">
      <input value="yes" title="" class="radio validate-one-required" id="head00" name="head0" type="radio" onclick="javascript:hoh()" />
      <label for="head00" class="radio_label">yes</label>
      <br />
      <input value="no" title="" class="radio validate-one-required" id="head01" name="head0" type="radio" onclick="javascript:nhoh()" />
      <label for="head01" class="radio_label">no</label>
      <br />
      
    </div>
    


<input type="hidden" value="" name="parent_id" id="parent_id" />

function hoh() {
document.directory.parent_id.value=0;
}
function nhoh() {
//code for retreiving the select_hoh id
document.directory.partent_id.value=/*cf_id of hoh goes here */;
}

 

Does this make sense?

No, it doesn't. You do not need to use javascript to set the hidden value. You would still have to validate that value on the server-side, so the hidden field really provides no useful information. So, on the processing page, if the Head of Household option is yes, just set the parent ID (in the PHP code) to 0.

 

In the processing page you would want to check the user input and validate based upon the selections. Here is a quick example (I left off a lot of validations needed, empty fieldsm escaping for the query, ensuring the parent id is valid, etc)

    $name = trim($_POST['name']);
    //Determine if entry is head of household
    $parent = ($_POST['hoh']=='yes');

    if($parent)
    {
        //Entry was head of household validate address data
        $address = trim($_POST['hoh']);
        $city = trim($_POST['city']);
        $state = trim($_POST['state']);
        $zip = trim($_POST['zip']);
        $query = "INSERT INTO table (name, parent_id, address, city, state, zip)
                  VALUES ('{$name}', 0, '{$address}', '{$city}', '{$state}', '{$zip}')";
    }
    else
    {
        //Entry was NOT head of household validate hoh selection
        $parent_id = (int) $_POST['hoh_id'];
        $query = "INSERT INTO table (name, parent_id)
                  VALUES ('{$name}', {$parent_id})";
    }

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.