Problem in PHP-HTML Posting of Forms

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

dream2rule
Forum Contributor
Posts: 109
Joined: Wed Jun 13, 2007 5:07 am

Problem in PHP-HTML Posting of Forms

Post 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
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Post 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?
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

Put print_r($_POST); at the beginning of the page and submit the form. ;)
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
dream2rule
Forum Contributor
Posts: 109
Joined: Wed Jun 13, 2007 5:07 am

Post 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 :( :(
dream2rule
Forum Contributor
Posts: 109
Joined: Wed Jun 13, 2007 5:07 am

Post 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 )
dream2rule
Forum Contributor
Posts: 109
Joined: Wed Jun 13, 2007 5:07 am

Post by dream2rule »

I am getting only the last field name, not the complete array of field names!
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Post by aceconcepts »

How are you dealing with the array you submit?
dream2rule
Forum Contributor
Posts: 109
Joined: Wed Jun 13, 2007 5:07 am

Post 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");
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Post 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
}
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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.
dream2rule
Forum Contributor
Posts: 109
Joined: Wed Jun 13, 2007 5:07 am

Post 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... :( :(
dream2rule
Forum Contributor
Posts: 109
Joined: Wed Jun 13, 2007 5:07 am

Post 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??
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Post 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.
dream2rule
Forum Contributor
Posts: 109
Joined: Wed Jun 13, 2007 5:07 am

Post by dream2rule »

it doesn't seem to work..
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Post 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[]'>
Post Reply