Jump to content

very complex form question


pouncer

Recommended Posts

I create my form bu php code, creating a textbox for each field in my table:

 

$results = mysql_query("SELECT * FROM $cat");
$num = mysql_num_fields($results);
$i = 0;

while ($i < $num) {
$name  = mysql_field_name($results, $i);

if ($name != 'item_id' && $name != 'image_URL') {
	echo $name . ":<br>";

	echo "<input name=\"txt_" . $name . "\" type=\"text\" id=\"txt_" . $name . "\" size=\"60\"\>";
	echo "<br><br>";
}
$i++;
};

echo " <input class=\"style74\" name=\"Add_item\" type=\"submit\" id=\"Save_Item\" value=\"Add item\">";

 

So for e.g, if there was a field called 'title' in the table, it would create a textbox with name 'txt_title'

 

But now I need to insert the record into the form, when they fill the form in. How do i get all the $_POST's from the text boxes? can someone please help me.

Link to comment
Share on other sites

No. That code I posted creates the text boxes in the form, on the page, for every single field in the $cat table.

 

 

Now when I fill the form out, I need to somehow check each fields data with the $_POST and do the necessary insert into my table.

I'm not sure how to loop through it and get each fields $_POST though.

Link to comment
Share on other sites

Ok, that's easy enough.

 

<?php
// first snag the field data from POST
$name = $_POST['name'];
$address = $_POST['address'];
$email = $_POST['email'];

// match those to your field names and one for each field

// mysql stuff
$sql = "INSERT INTO tablename (field1, field2, field3, etc.) VALUES '$name', '$address', '$email' ";
$results = mysql_query($sql) or die(mysql_error());
if(!$results) {
echo "Insert did not work. Try again";
} else {
echo "Records inserted successfully!";
}
?>

Link to comment
Share on other sites

no mate. i don't mean like that. sorry i wasn't clear enough.

 

The fields are not a fixed set of fields, I've created forms for users to add/delete fields to the tables etc. So i can't do as you have posted incase users add new fields which means I will have to add a new $_POST variable.

 

So the code I posted creates the form by looping the $cat table and outputting a text box for each field in that $cat table.

 

 

I think i need to now loop the $cat table again, and get the field names and then somehow check the post like

$_POST['txt_$name'];

 

but that didnt work

 

I tried something like:

				if (isset($_POST['Add_item'])) {
					echo "Clicked on add item<br>";

					$results = mysql_query("SELECT * FROM $cat");
					$num = mysql_num_fields($results);

					$i = 0;

					while ($i < $num) {
						$name  = mysql_field_name($results, $i);

						if ($name != 'item_id' && $name != 'image_URL') {
							echo $_POST['txt_.$name'];
						}


						$i++;
					};


				}

but it just echos; Clicked on add item

it should echo the contents of each field which i filled in.

Link to comment
Share on other sites

Ok, so let's see if i'm clear on this. Not only are you getting the field names from the database but you are also allowing the user to create new ones named whatever they want and however many they want... is that right? Then you want to build a form based upon what they've created in the database... is that right for step 2? Then i'm also going to have to assume that you have a separate table for each person. Otherwise, exponentially, you could have someone looking at 1,000+ fields if they showed up late for the party. In other words, if i'm the 20th guy to come to the site and the previous 19 have each added a few fields and your code loops through that same table to display the form for me then it could contain things like "do you have pink bunny slippers". Not sure how this is structured in your database.

Link to comment
Share on other sites

Yes. These tables are category tables, like movies,dvds,cars etc. Users can add new fields to these tables.

 

Now I have created a form for the users, by looping the table.

Now I just need to insert the data when users fill the form in, into the table.

Link to comment
Share on other sites

I tried this

 

				$results = mysql_query("SELECT * FROM $cat");
					$num = mysql_num_fields($results);

					$i = 0;

					while ($i < $num) {
						$name  = mysql_field_name($results, $i);
						$txtbox = "txt_" . $name;

						if ($name != 'item_id' && $name != 'image_URL') {
							echo $_POST['$txtbox'];
						}


						$i++;
					};

 

the:

echo $_POST['$txtbox'];

 

it doesnt seem to work, anyone see what im doing wrong?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.