Jump to content

Advice on how to save this data from form


AdRock

Recommended Posts

I have a form with a lot of fields but they are in groups with checkboxes and text fields.

 

The question is what is the best way to store this data in the database?  Would i store the whole $_POST array as a single field and explode when i want to pull from the database or is it best to have a separate field for each value (that would make the table huge with possibly a lot of empty fields)?

 

The one that bothers me the most of the text fields beucase there is 4 text boxes for one row on my form and I have about 10 rows.

 

The form is like an application form where you put in your work experience

 

Here is part of my example form

 

checkboxes

<div class="left">
<p class="label_checkbox_pair">
	<input type="checkbox" name="quals[]" value="carpenter" /><label for="carpenter">Carpenter:</label>
</p>
<p class="label_checkbox_pair">
	<input type="checkbox" name="quals[]" value="mechanic" /><label for="mechanic">Mechanic</label>
</p>
<p class="label_checkbox_pair">
	<input type="checkbox" name="quals[]" value="electrician" /><label for="electrician">Electrician</label>
</p>
<p class="label_checkbox_pair">
	<input type="checkbox" name="quals[]" value="plumber" /><label for="plumber">Plumber</label>
</p>
<p class="label_checkbox_pair">
	<input type="checkbox" name="quals[]" value="fire warden" /><label for="fire_warden">Fire Warden</label>
</p>
</div>

 

and text fields

<li>
<input name="Experience0 From"     type="text" size="3" maxlength="4" />
- <input name="Experience0 To"       type="text" size="3" maxlength="4" />
<input class="exprol" name="Experience0 Role"     type="text" size="18" />
<input class="expfest" name="Experience0 Festival" type="text" size="30" />
</li>
<li>				
<input name="Experience1 From"     type="text" size="3" maxlength="4" />
- <input name="Experience1 To"       type="text" size="3" maxlength="4" />
<input class="exprol" name="Experience1 Role"     type="text" size="18" />
<input class="expfest" name="Experience1 Festival" type="text" size="30" />
</li>
<li>				
<input name="Experience2 From"     type="text" size="3" maxlength="4" />
- <input name="Experience2 To"       type="text" size="3" maxlength="4" />
<input class="exprol" name="Experience2 Role"     type="text" size="18" />
<input class="expfest" name="Experience2 Festival" type="text" size="30" />
</li>

 

 

That's what I thought

 

I've done that with the checkboxes and I created 10 fields for the last part (10 rows on the form) and i using the implode/explode to get each row in it's own field.

 

Just another quick question, the first 2 columns are dates and the other 2 are text.  Can you do form validation suing something like:

<li>				
<input name="Experience4[] From"     type="text" size="3" maxlength="4" />
- <input name="Experience4[] To"       type="text" size="3" maxlength="4" />
<input class="exprol" name="Experience4[] Role"     type="text" size="18" />
<input class="expfest" name="Experience4[] Festival" type="text" size="30" />
</li>

 

 

check_input($_POST['experience4 From'])

I've been trying different things and i got the checkboxes to implode with a comma but i'm having trouble with every text field.  It still tries to implode the empty fields and i'm getting an error.

 

Warning: implode() [function.implode]: Invalid arguments passed in
for each row of text fields

 

//variables for checking the user's name
    	$userid = $_SESSION['userid'];
    	$quals = check_input(implode(",",$_POST['quals']));
    	$roles = check_input(implode(",",$_POST['roles']));
	$region = check_input(implode(",",$_POST['region']));

	$experience0 = check_input(implode(",",$_POST['experience0']));
	$experience1 = check_input(implode(",",$_POST['experience1']));
	$experience2 = check_input(implode(",",$_POST['experience2']));
	$experience3 = check_input(implode(",",$_POST['experience3']));
	$experience4 = check_input(implode(",",$_POST['experience4']));
	$experience5 = check_input(implode(",",$_POST['experience5']));
	$experience6 = check_input(implode(",",$_POST['experience6']));
	$experience7 = check_input(implode(",",$_POST['experience7']));
	$experience8 = check_input(implode(",",$_POST['experience8']));
	$experience9 = check_input(implode(",",$_POST['experience9']));

 

I was thinking about checking if they are empty before doing the implode but is there an effieicent way of doing it like if a for loop?

This should be the last question.

 

Is there a way i can loop through each of these without having to do loads of if statements?

if(!empty($_POST['experience0'])) $experience0 = check_input(implode(",",$_POST['experience0']));
if(!empty($_POST['experience1'])) $experience1 = check_input(implode(",",$_POST['experience1']));
if(!empty($_POST['experience2'])) $experience2 = check_input(implode(",",$_POST['experience2']));
if(!empty($_POST['experience3'])) $experience3 = check_input(implode(",",$_POST['experience3']));
if(!empty($_POST['experience4'])) $experience4 = check_input(implode(",",$_POST['experience4']));
if(!empty($_POST['experience5'])) $experience5 = check_input(implode(",",$_POST['experience5']));
if(!empty($_POST['experience6'])) $experience6 = check_input(implode(",",$_POST['experience6']));
if(!empty($_POST['experience7'])) $experience7 = check_input(implode(",",$_POST['experience7']));
if(!empty($_POST['experience8'])) $experience8 = check_input(implode(",",$_POST['experience8']));
if(!empty($_POST['experience9'])) $experience9 = check_input(implode(",",$_POST['experience9']));

 

I though about using variable variables but i can't get it to work in a for loop

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.