Jump to content

Archived

This topic is now archived and is closed to further replies.

kenwvs

Ignore Empty Fields

Recommended Posts

I have a form that has seven fields for part numbers and seven fields for descriptions.  In some cases all seven fields will be filled in, and in other cases, some of the fields will be empty.  I need to create some code so that if the part number and description fields are empty, they get ignored.  In the end, I don't want empty rows being added to the database table.

here is what I have, and it is putting seven rows in the database.  It is also not putting the form field data in the DB.  In the number field it is just putting a zero and in the description field it is putting description[]

Here is what I have, and I would appreciate any help in putting together some code to eliminate the empty fields from posting.

This is the form fields:
[code]<input type="text" size="10" maxlength="10" name="number[]">
<input type="text" size="93" maxlength="93" name="description[]"><BR>
<input type="text" size="10" maxlength="10" name="number[]">
<input type="text" size="93" maxlength="93" name="description[]"><BR>
<input type="text" size="10" maxlength="10" name="number[]">
<input type="text" size="93" maxlength="93" name="description[]"><BR>
<input type="text" size="10" maxlength="10" name="number[]">
<input type="text" size="93" maxlength="93" name="description[]"><BR>
<input type="text" size="10" maxlength="10" name="number[]">
<input type="text" size="93" maxlength="93" name="description[]"><BR>
<input type="text" size="10" maxlength="10" name="number[]">
<input type="text" size="93" maxlength="93" name="description[]"><BR>
<input type="text" size="10" maxlength="10" name="number[]">
<input type="text" size="93" maxlength="93" name="description[]"><BR>[/code]

This is the code that I have put together.

[code]

if (!isset($_POST['Submit']))
{

$_POST['description'] = array(
    'description[]',
    'description[]',
    'description[]',
    'description[]',
    'description[]',
    'description[]',
    'description[]',
    );

$_POST['number'] = array(
    'number[]',
    'number[]',
    'number[]',
    'number[]',
    'number[]',
    'number[]',
    'number[]',
    );

{
if (!empty($_POST['number']))
  foreach($_POST['description'] as $key=>$description) {
  if ($description == '') continue;
  $description_esc = mysql_real_escape_string($description);
  $number = intval($_POST['number'][$key]);
  $query = "INSERT INTO parts (description,number) VALUES ("
    . "'{$description_esc}', "
    . "{$number} )";
mysql_query($query) or die(mysql_error().$query);
}[/code]

Share this post


Link to post
Share on other sites
With the below code:
[code=php:0]$_POST['description'] = array(
    'description[]',
    'description[]',
    'description[]',
    'description[]',
    'description[]',
    'description[]',
    'description[]',
    );

$_POST['number'] = array(
    'number[]',
    'number[]',
    'number[]',
    'number[]',
    'number[]',
    'number[]',
    'number[]',
    );[/code]

You are crearting a new array called _POST['description'], which holds the string 'description[]' seven times in the array, and thus you are getting decription[] added to the database. The same applies $_POST['number'].

With the code above you are overwriting the POST'd data being sent from the form.

Share this post


Link to post
Share on other sites
so, if I understand correctly, if I remove those two arrays, it will post the proper data.  But I was trying it without them, and it would still post empty rows.  Am I missing something to get it to not post the empty lines

Share this post


Link to post
Share on other sites
If a form is field is empty it will not create a post var for that form field. Also you should try to name your fileds properly by giving them names more discriptively rather than just description[] and number[].

Share this post


Link to post
Share on other sites
That did solve the problem.  I actually thought the descriptions were ok as it is part number and part description.

Share this post


Link to post
Share on other sites

×

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.