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