Jump to content

I THINK this is possible to do, can someone verify?


BadGoat

Recommended Posts

Hello!

I have a form-driven PHP script that I have been adding on to for some time. The newest attribute I wanted to add to it was to try to make a 'form within a form', where in the overall 15ish variables of the form, I can add a few grouped variables. Being still noobish to PHP I am not sure how to describe it well, but I'll try with an example.

Var 1: Husband Name
Var 2: Wife Name
Var3: Child #1 Name Var 4: Child 1 age Var 5: Child 1 sex



What would be ideal is to be able to add multiple children before submitting the form. Is this possible?

I am not asking anyone to code something, I would like to get a hint or to be explained the concept so I can try to code it on my own. (Best way to learn!)

Thank you
Link to comment
Share on other sites

well you can make the name for the children text input fields an array. i think that's what you're asking? like so:

[code]
$form = "<form blah=blah etc...>";
$form.="Husband <input type = 'text' name='husband'><br>";
$form.="Wife <input type='text' name='wife'><br>";

$x = 1;
while ($x < 4) { // will count 1 to whatever.
   $form.="Child ".$x." <input type='text name='child[]'><br>";
}

$form.="<input type='submit' value='submit'>";

echo $form;
[/code]

then when the form submits, you would get the info from these variables:

$_POST['husband']
$_POST['wife']

and to make it easier for coding, i suggest doing this for the children:

$children = $_POST['child'];

that way you will have an array called children that holds the children. you can use this array in a loop to quickly list them later like so:

[code]
$x = 0;
while ($children[$x]) {
   echo $children[$x] . "<br>";
}
[/code]

or just refer to them directly with the (in this example) 3 individual variables:

$children[0]
$children[1]
$children[2]

Link to comment
Share on other sites

Hi Crayon,

Yes, I believe that is what I'm looking to accomplish. Would it also be possible to have only one set of child input variables visible, which when filled in display the sets of children below the inputs, so I remember which kids I already entered?

In the meantime,I'm going to go back to my PHP books and read up on arrays.

Thank you kindly for the guidance!
Link to comment
Share on other sites

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.