Looping thru multiple form fields

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
nkom
Forum Newbie
Posts: 1
Joined: Sat Nov 05, 2005 3:57 am

Looping thru multiple form fields

Post 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
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post 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;
}
}
yum-jelly
Forum Commoner
Posts: 98
Joined: Sat Oct 29, 2005 9:16 pm

Post 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
egmax
Forum Newbie
Posts: 5
Joined: Mon Nov 07, 2005 10:19 pm
Location: &#20013;&#22269;CHINA

Post by egmax »

input[]
input[]
input[]input[]
input[]
input[]


for(){
}
Post Reply