Jump to content

validating a form


Nodral

Recommended Posts

Hi

 

I have a form that returns 5 variables from user submitted fields when the submit button is pressed.  I need my script to react in different ways depending on how many of the fields are completed.

 

The form is made up of 20 rows with each line having a unique id number which prefixes each variable, for example

$1forename, $1surname, $1address, $1phone, $1email

$2forename, $2surname, $2address, $2phone, $2email

.

.

.

.

.

.

.

.

$20forename, $20surname, $20address, $20phone, $20email

 

The user can complete as many or as few lines as they require

 

If no fields on a line are completed, i need it to ignore the script, if all fields on a line are completed write to a DB, and if the line on the form is partially completed return an error message.

 

I can't use isset, as even if the user just clicks on submit then a null/blank value is returned and the variable is set for every field on every line

 

 

Anyone any ideas how I can acheive this without using a seperate if(strlen($variable)>0){ declaration for every variable.

 

Hope this makes sense and I look forward to some of you wonderful guys giving me a nice concise solution.

 

Cheers

 

Link to comment
https://forums.phpfreaks.com/topic/241134-validating-a-form/
Share on other sites

You'd be better off using arrays in your form:

 

<input type="text" name="data[1][forename]">
<input type="text" name="data[1][address]">
<input type="text" name="data[1][phone]">
<input type="text" name="data[1][email]">
<input type="text" name="data[2][forename]">
<input type="text" name="data[2][address]">
<input type="text" name="data[2][phone]">
<input type="text" name="data[2][email]">

 

Then loop through and check:

 

foreach($_POST['data'] as $data) {
   //check vars $data['forename'], $data['address'] etc...
}

Link to comment
https://forums.phpfreaks.com/topic/241134-validating-a-form/#findComment-1238586
Share on other sites

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.