Javacadabra
Javacadabra

Reputation: 5758

Getting x amount of variables from POST Array

I'm wondering what the best way to approach this is. Basically I'm working on a registration form for a course. The user specifies how many people will be registering and this displays X amount of rows in the table to enter details for each user.

This form submits to a php file that will handle the data. However if I am to get the information from the POST array do I need to write out 100 statements to do this or can I do it dynamically.

For example currently this is my form:

<form action="#" method="POST">
        <table>
            <th></th>
            <th>Name</th>
            <th>Email</th>
            <th>Phone</th>
            <? for($i = 0; $i < $qty; $i++):?>
            <tr><td>Guest <?= $i + 1?></td>
                <td><input type="text" id="name" name="name<?= $i + 1 ?>"></td>
                <td><input type="text" id="email" name="email<?= $i + 1 ?>"/></td>
                <td><input type="text" id="phone" name="phone<?= $i + 1 ?>"/></td>
                <input type="hidden" name="course_id" value="<?= the_ID() ?>"/>
                <input type="hidden" name="course_title" value="<?= $course ?>"/>
                <input type="hidden" name="formSend2" value="1" />
                <input type="hidden" name="course_date" value="<?= $date ?>"/>
                <input type="hidden" name="course_location" value="<?= $location ?>"/>
                <input type="hidden" name="course_applicant" value="<?= $user_ID ?>"/>
            </tr>
            <? endfor; ?>
        </table>
        <input type="submit" value="Confirm Registration"/>
        </form>

This is the script it submits to

function register_course_handler() {

 var_dump($_POST);
    die();

    $course_id = $_POST['course_id'];
    $course_title = $_POST['course_title'];
    $course_date = $_POST['course_date'];
    $course_location = $_POST['course_location'];
    $course_applicant = $_POST['course_applicant'];

If the user selects a quantity of 8 the form will contain 8 rows and when posted the var_dump($_POST) outputs this:

array (size=30)
  'name1' => string 'd' (length=1)
  'email1' => string 's' (length=1)
  'phone1' => string 'd' (length=1)
  'course_id' => string '1063' (length=4)
  'course_title' => string 'Energy use in Mushroom Units' (length=28)
  'formSend2' => string '1' (length=1)
  'course_date' => string '23-07-2014' (length=10)
  'course_location' => string 'Teagasc, Thurles' (length=16)
  'course_applicant' => string '1' (length=1)
  'name2' => string '' (length=0)
  'email2' => string '' (length=0)
  'phone2' => string '' (length=0)
  'name3' => string '' (length=0)
  'email3' => string '' (length=0)
  'phone3' => string '' (length=0)
  'name4' => string '' (length=0)
  'email4' => string '' (length=0)
  'phone4' => string '' (length=0)
  'name5' => string '' (length=0)
  'email5' => string '' (length=0)
  'phone5' => string '' (length=0)
  'name6' => string '' (length=0)
  'email6' => string '' (length=0)
  'phone6' => string '' (length=0)
  'name7' => string '' (length=0)
  'email7' => string '' (length=0)
  'phone7' => string '' (length=0)
  'name8' => string '' (length=0)
  'email8' => string '' (length=0)
  'phone8' => string '' (length=0)

Do I have to write x amount of variables to check if they are set in the POST array?

Upvotes: 0

Views: 102

Answers (4)

kolomiets
kolomiets

Reputation: 97

Try this code.

Form Code:

<form action="#" method="POST">
    <table>
        <th></th>
        <th>Name</th>
        <th>Email</th>
        <th>Phone</th>
        <? for($i = 0; $i < $qty; $i++):?>
        <tr><td>Guest <?= $i + 1?></td>
            <td><input type="text" id="name" name="guest[<?= $i + 1 ?>][email]"></td>
        </tr>
        <? endfor; ?>
    </table>
    <input type="submit" value="Confirm Registration"/>
    </form>

In PHP script

foreach ($_POST ['guest'] as $guest)
{
     echo $guest['email']
}

Upvotes: 0

Useless Intern
Useless Intern

Reputation: 1302

Nah, PHP can purge all of the empty values for you

$test =   array(   'name1' =>  'd',
  'email1' =>  's' ,
  'phone1' =>  'd' ,
  'course_id' =>  '1063',
  'course_title' =>  'Energy use in Mushroom Units',
  'formSend2' =>  '1' ,
  'course_date' =>  '23-07-2014' ,
  'course_location' =>  'Teagasc, Thurles' ,
  'course_applicant' =>  '1' ,
  'name2' =>  '' ,
  'email2' =>  '',
  'phone2' =>  '',
  'name3' =>  '' ,
  'email3' =>  '',
  'phone3' =>  '',
  'name4' =>  '' ,
  'email4' =>  '',
  'phone4' =>  '',
  'name5' =>  '' ,
  'email5' =>  '',
  'phone5' =>  '',
  'name6' =>  '' ,
  'email6' =>  '',
  'phone6' =>  '',
  'name7' =>  '' ,
  'email7' =>  '',
  'phone7' =>  '',
  'name8' =>  '' ,
  'email8' =>  '',
  'phone8' =>  '');

  $test = array_filter($test);
  print_r($test);

  -> Array ( [name1] => d [email1] => s [phone1] => d [course_id] => 1063 [course_title] => Energy use in Mushroom Units
            [formSend2] => 1 [course_date] => 23-07-2014 
            [course_location] => Teagasc, Thurles [course_applicant] => 1 ) 

Upvotes: 0

nl-x
nl-x

Reputation: 11832

If you use

<input type="text" id="name" name="name<?= $j + 1 ?>">

Then in PHP you could do:

$j = 0;
while (++$j) {
    if (!isset($_POST['name' . $j]))
        break;
    $name = $_POST['name' . $j];
    $email = $_POST['email' . $j];
    $phone = $_POST['phone' . $j];
    // ...
}

But I would advise you to remove your for loop and go with arrays in HTML:

<input type="text" id="name" name="name[]">

Then in PHP you can do:

for ($j = 1; $j <= count($_POST['name']); $j++) {
    $name = $_POST['name' . $j];
    $email = $_POST['email' . $j];
    $phone = $_POST['phone' . $j];
    // ...
}

Upvotes: 1

colburton
colburton

Reputation: 4715

Just like you generate the rows in the html snippet you can collect the data from $_POST.

for ($i = 1; $i <= X; $i++) {
  if (isset($_POST['name'.$i]) && $_POST['name'.$i] != '') {
    // user entered something in this row
    // collect all data like that:
    $email = $_POST['email'.$i];
  }
}

Upvotes: 0

Related Questions