Page 1 of 2

Problem in PHP-HTML Posting of Forms

Posted: Thu Aug 09, 2007 4:08 am
by dream2rule

Code: Select all

$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>
	<td><input type='text' name='db_name' size='20' value='$create_db_name' disabled='disabled'></td></tr>";
	$form.="<tr><td><br>&nbsp;Table Name:</td>
	<td><br><input type='text' name='table_name' size='20' value='$create_table_name' disabled='disabled'></td></tr>";
	
	for($i=0; $i < $table_num_field; $i++)
	{
		$form.="<tr><font face='verdana' size='1'>";
		$form.="<td><br>&nbsp;Field: <input type='text' name='fld_name[$i]' size='10'></td>";
		$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><option value='null'>Null</option>";
		$form.="</select></td>";
		$form.="<td><br><input type='checkbox' name='fld_extra' value='auto' size='10'> Auto-increment</td>";
		$form.="<td><br><input type='radio' name='fld_keys' value='primary' size='10'> Primary</td>";
		$form.="<td><br><input type='radio' name='fld_keys' value='unique' size='10'> Unique</td>";
		$form.="</font></tr>";
	}
	$form.="<tr><td><br>&nbsp;<input type='submit' name='create_table' value='Create Table'></td></tr>";
	$form.="</table>";
	$form.="</td></tr></table></form>";
	echo($form);
In the above code, I want to retrieve all the posted values. Can anyone help.. I am stuck and $_POST[] does not seem to work.. :( :( :(

Thanks and Regards,
Dream2rule

Posted: Thu Aug 09, 2007 5:07 am
by aceconcepts
How are you "getting" your "posted" values?

And I take it you are submitting the form to the same page that resides due to your "action" declaration?

Posted: Thu Aug 09, 2007 5:13 am
by s.dot
Put print_r($_POST); at the beginning of the page and submit the form. ;)

Posted: Thu Aug 09, 2007 5:14 am
by dream2rule
yes i am posting all the values to the same page and i am trying to use for loops to retrieve data from form but in vain :( :(

Posted: Thu Aug 09, 2007 5:17 am
by dream2rule
scottayy wrote:Put print_r($_POST); at the beginning of the page and submit the form. ;)
I get this when i put print_r($_POST); at the beginning of the form

Code: Select all

Array ( [fld_name] => name [fld_type] => varchar [fld_size] => 25 [fld_null] => not null [fld_extra] => auto [fld_keys] => primary [create_table_submit] => Create Table )

Posted: Thu Aug 09, 2007 5:24 am
by dream2rule
I am getting only the last field name, not the complete array of field names!

Posted: Thu Aug 09, 2007 5:33 am
by aceconcepts
How are you dealing with the array you submit?

Posted: Thu Aug 09, 2007 5:37 am
by dream2rule
If i loop it up its giving me errors,

Code: Select all

$fld_name = $_POST['fld_name'];
$fld_type = $_POST['fld_type'];
$fld_size = $_POST['fld_size'];
$fld_null = $_POST['fld_null'];
$fld_extra = $_POST['fld_extra'];
$fld_keys = $_POST['fld_keys'];
if(!empty($fld_extra))
	echo $fld_extra;
if(!empty($fld_keys))
	echo $fld_keys;
echo("$fld_name"."&nbsp;"."$fld_type"."&nbsp;"."$fld_size"."&nbsp;"."$fld_null");

Posted: Thu Aug 09, 2007 5:47 am
by aceconcepts
declare a variable that will store the array submitted from the form:

Code: Select all

$varArray=array();
Then, to loop the array values use:

Code: Select all

foreach($varArray as $tmpArray)
{
      //do something
}

Posted: Thu Aug 09, 2007 5:52 am
by volka
dream2rule wrote:I get this when i put print_r($_POST); at the beginning of the form

Code: Select all

Array ( [fld_name] => name [fld_type] => varchar [fld_size] => 25 [fld_null] => not null [fld_extra] => auto [fld_keys] => primary [create_table_submit] => Create Table )
That doesn't match the code you've posted.

Posted: Thu Aug 09, 2007 6:00 am
by dream2rule
volka wrote:
dream2rule wrote:I get this when i put print_r($_POST); at the beginning of the form

Code: Select all

Array ( [fld_name] => name [fld_type] => varchar [fld_size] => 25 [fld_null] => not null [fld_extra] => auto [fld_keys] => primary [create_table_submit] => Create Table )
That doesn't match the code you've posted.

Code: Select all

$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><option value='null'>Null</option>";
                $form.="</select></td>";
                $form.="<td><br><input type='checkbox' name='fld_extra' value='auto' size='10'> Auto-increment</td>";
                $form.="<td><br><input type='radio' name='fld_keys' value='primary' size='10'> Primary</td>";
                $form.="<td><br><input type='radio' name='fld_keys' value='unique' size='10'> Unique</td>";
I did.. it does match... how do i loop it up? Because there's an array within an array... :( :(

Posted: Thu Aug 09, 2007 6:07 am
by dream2rule
aceconcepts wrote:declare a variable that will store the array submitted from the form:

Code: Select all

$varArray=array();
Then, to loop the array values use:

Code: Select all

foreach($varArray as $tmpArray)
{
      //do something
}
This way i would have to create an array for each field i.e, an array for storing field names then the field types etc...

Any simpler wa to this??

Posted: Thu Aug 09, 2007 6:19 am
by aceconcepts
Dont declare an "array" variable to store your posted array.

Just use the foreach loop with you posted values.

Code: Select all

foreach($_POST['form_field'] as $tmpArray)
{
      //do something
}
Maybe this will work as you have already declared the "value" as an array.

Posted: Thu Aug 09, 2007 6:56 am
by dream2rule
it doesn't seem to work..

Posted: Thu Aug 09, 2007 7:50 am
by aceconcepts
Try removing the "php value" from within the "[" brackets of "fld_type".

So the select element will look like this:

Code: Select all

<select name='fld_type[]'>