Page 1 of 1

[SOLVED] Retrieve array values posted in a form

Posted: Sat Aug 11, 2007 12:52 am
by dream2rule

Code: Select all

<?php

$db_name = $_POST['db_name'];
$table_name = $_POST['table_name'];
$num_fields = $_POST['fields'];
echo $db_name."<br>".$num_fields."<br>".$table_name."<br>";

//retrieving posted hidden values
$num_flds = $_POST['num_flds'];
echo $num_flds."<br>";


if(isset($_POST['create_table_submit']))
{
	
//how to get the posted values information as those are stored in arrays in the form below

}

?>



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<body>
<?php
if(!empty($num_fields))
{
	$form = "<form action='' method='post' name='create_table_form' id='create_table_form' >";
	$form.="<table border='0' cellpadding='0' cellspacing='0' width='95%'><tr><td><br />";
	$form.="<table border='0' cellpadding='0' cellspacing='0' width='100%'>";
	$form.="<tr><td>&nbsp;Database Name:</td>";
	$form.="<td><input type='text' name='db_name' size='20' value='$db_name'></td></tr>";
	$form.="<tr><td><br>&nbsp;Table Name:</td>";
	$form.="<td><br><input type='text' name='table_name' size='20' value='$table_name'>";
	$form.="</td></tr>";
	
	for($i=0; $i < $num_fields; $i++)
	{
		$fld_name = array();
		$form.="<tr><font face='verdana' size='1'>";
		$form.="<td><br>&nbsp;Field: <input type='text' name='$fld_name[$i]' size='10'>";
		$types = array("varchar","tinyint","text","smallint","mediumint","int","bigint","float","double","decimal",
		"char","tinyblob","tinytext","blob","mediumblob","mediumtext","longblob","longtext","binary","varbinary");
		$form.="<td><br>&nbsp;Type: <select name='fld_type[$i]'>";
		
		for($j=0; $j < count($types); $j++)
		{
			$form.="<option value=$types[$j]>$types[$j]</option>";
		} 		
		$form.="</select></td>";
		$form.="<td><br>&nbsp;Size: <input type='text' name='fld_size[$i]' size='5'></td>";
		$form.="<td><br>&nbsp;Null: <select name='fld_null[$i]'>";
			$form.="<option value='NOT NULL'>Not Null</option>";
			$form.="<option value='NULL'>Null</option>";
		$form.="</select></td>";
		$form.="<td><br><input type='checkbox' name='fld_extra[$i]' value='auto_increment' size='10'> Auto-increment</td>";
		$form.="<td><br><input type='radio' name='fld_keys[$i]' value='primary key' size='10'> Primary</td>";
		$form.="<td><br><input type='radio' name='fld_keys[$i]' value='unique key' size='10'> Unique</td>";
		$form.="</font></tr>";
	} 
	$form.="<tr><td><br>&nbsp;<input type='submit' name='create_table_submit' value='Create Table'></td>";
	$form.="<td><input type='hidden' name='num_flds' value=$num_fields /></td>";
	$form.="</tr>";
	$form.="</table>";
	$form.="</td></tr></table>";
	$form.="</form>";
	echo($form);
}
?>

</body>
</html>
Hello,

I want to retrieve all the posted values from the above form. Any ideas how to go about it... I have been stuck with this from a really long time now... :(

Any help would be appreciated. Thanks

Posted: Sat Aug 11, 2007 1:52 am
by Benjamin
What do you see when you submit the form to a page containing this..

Code: Select all

echo '<pre style="font-size: 12px;">' . print_r($_POST, true) . "</pre>";
?

Posted: Sat Aug 11, 2007 3:02 am
by dream2rule
Before submitting the form


Array
(
[table_name] => mytable3
[fields] => 3
[submit] => Submit
[db_name] => test11
)

After submitting the form:

Array
(
)

//But when i retrieve the values of post not considering the arrays i get the last field being displayed the number of times i loop it up

Array
(
[db_name] => test11
[table_name] => mytable3
[fld_name] => email
[fld_type] => varchar
[fld_size] => 50
[fld_null] => NOT NULL
[fld_extra] => auto_increment
[fld_keys] => unique key
[create_table_submit] => Create Table
[num_flds] => 3
)

email varchar(50) NOT NULL auto_increment , email varchar(50) NOT NULL auto_increment , email varchar(50) NOT NULL auto_increment ,

Posted: Sat Aug 11, 2007 3:39 am
by volka
first thread on same topic: viewtopic.php?t=72090


might be easier if you structure the data instead of
name[0], name[1], name[2], name[3]
type[0], type[1], type[2], type[3]
null[0], null[1], null[2], null[3]
like
field[0] = array(name,type,null)
field[1] = array(name,type,null)
field[2] = array(name,type,null)
...

Code: Select all

<?php
$num_fields = 5;
$types = array(
    "varchar","tinyint","text","smallint","mediumint","int","bigint",
    "float","double","decimal","char","tinyblob","tinytext","blob",
    "mediumblob","mediumtext","longblob","longtext","binary","varbinary"
  );
$options_type = join('', array_map(create_function('$x', 'return "<option>$x</option>";'), $types));
?>
<form method="post" action="?">
  <table>
    <tr>
      <th>name</th><th>type</th><th>allow null</th>
    </tr>
<?php for($i=0; $i<$num_fields; $i++) { ?>
    <tr>
      <td><input type="text" name="field[<?php echo $i; ?>][name]" /></td>
      <td>
        <select name="field[<?php echo $i; ?>][type]"><?php echo $options_type; ?></select>
      </td>
      <td>
        <select name="field[<?php echo $i; ?>][null]">
          <option>not null</option>
          <option>null</option>
        </select>
      </td>
    </tr>
<?php } ?>
    <tr>
      <td colspan="3">
        <input type="submit" />
      </td>
    </tr>
  </table>
</form>
<?php
if ( isset($_POST['field']) && is_array($_POST['field']) ) {
  foreach($_POST['field'] as $fielddef) {
    echo 'name: ', $fielddef['name'], ' type: ', $fielddef['type'], ' null:', $fielddef['null'], "<br />\n";
  }
}

Posted: Mon Aug 13, 2007 1:52 am
by dream2rule
hey Volka excellent... it works exactly fine... :) :) :)

Thanks a tonne..