Page 1 of 1

Looping thru multiple form fields

Posted: Sat Nov 05, 2005 4:13 am
by nkom
Hi all,

Sorry, this might be a newbie question, just started working on PHP for few months.

I have a form which looks like

1. <input name="fullname1" type="text" > <input name="Passport1" type="text" >.......(more fields)

until

20. <input name="fullname20" type="text" > <input name="Passport20" type="text" >.......

When the form was submitted, I want to use a loop to go thru all fields to do some condition checkings and if ok, then insert into MySQL. How do I dynamically reference to the filed names, e.g. "fullname1", "fullname2".... and so on???

Thanks in Advance.

NK

Posted: Sat Nov 05, 2005 4:29 am
by timvw
Your posted values will be in the array $_POST.
If you keep an array with values you are expecting, you easily test if they are all present, append them to an sql clause and perform your query. A next step is that you generate the sql somewhere else...

Code: Select all

$keyvals = array();
$expected = array('firstname', 'lastname', 'street', ...);
foreach($expected as $name)
{
    if (isset($_POST[$name]))
    {
         $keyvals[$name] = $_POST[$name];
    }
}

...
$sql =  $sqlgenerator->makeInsertSQL($keyvals);
...

class SQLGenerator 
{
    var $tablename;
     ...

function makeInsertSQL($keyvals)
{
     $sql1 = "INSERT INTO $this->tablename (";
     $sql2 = ") VALUES (";

     foreach($keyvals as $key => $val)
     {
           $sql1 .= "`$key`, ";
           $sql2 .= "'" . mysql_real_escape_string($val) . "', ";
     }

      $sql =   rtrim($sql1, ", ") . rtrim($sql2, ", ") . ");";
      return $sql;
}
}

Posted: Sat Nov 05, 2005 4:55 am
by yum-jelly
Maybe it would be better to put them into array reference! It's makes handling things much easier! Why, because you don't have to manipulate the variable name only it's value!

// form

Code: Select all

<input name="fullname[]" type="text" >
<input name="Passport[]" type="text" >
<input name="fullname[]" type="text" >
<input name="Passport[]" type="text" >
<input name="fullname[]" type="text" >
<input name="Passport[]" type="text" >
<input name="fullname[]" type="text" >
<input name="Passport[]" type="text" >
// the process


// does nothing just for example viewing value

Code: Select all

define ( 'AUTO_INCREMENT', '' );

$query = array ();
$form = array ();

if ( isset ( $_POST['fullname'] ) && is_array ( $_POST['fullname'] ) )
{
	foreach ( $_POST['fullname'] AS $k => $v )
	{
		if ( isset ( $_POST['Passport'][$k] ) ) // only process if fullname & Passport are set
		{
			$fn = trim ( $v );
			$pp = trim ( $_POST['Passport'][$k] );

			if ( ! empty ( $fn ) && ! empty ( $pp ) ) // build the insert query
			{
				$query[] = "( '" . AUTO_INCREMENT . "', '" . addslashes ( $fn ) . "', '" . addslashes ( $pp ) . "' )";
			}
			else // build the form error control ( reset values )
			{
				$form[] = array ( htmlspecialchars ( $fn, ENT_NOQUOTES ), htmlspecialchars ( $pp, ENT_NOQUOTES ) );
			}
		}
	}
}

// do we have a query, if so insert the data

if ( ! empty ( $query ) )
{
	mysql_connect ( 'localhost', 'u', 'p' );
	mysql_select_db ( 'd' );

	$sql = "INSERT INTO table_name VALUES " . implode ( ', ', $query );

	mysql_query ( $sql );
}

// do we have any errors

if ( ! empty ( $form ) )
{
	/* call the form function and fill the inputs */

	// example

	for ( $i = 0; $i < sizeof ( $form ); $++ )
	{
echo '<input name="fullname[]" type="text" value="' . $form[$i][0] . '" />
<input name="Passport[]" type="text" value="' . $form[$i][1] . '" />
<br />
';
	}
}
else
{
	/* nice redirect or thanks for using our service... */
}

just a basic example

yj

Posted: Mon Nov 07, 2005 11:11 pm
by egmax
input[]
input[]
input[]input[]
input[]
input[]


for(){
}