Passing values from a dynamicaly created form to new page

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
johnblaze00
Forum Newbie
Posts: 4
Joined: Wed Jun 03, 2009 2:04 pm

Passing values from a dynamicaly created form to new page

Post by johnblaze00 »

First I have to say I am terrible at PHP, like i get it, but i just can't do it (if that makes any sense). I am not a natural programmer, client side i'm good, server side :(. Anyways, here is my issue.

I have a page that will dynamically create a form. The purpose of this form is to create a mysql table using the values of the form. That I am more that confident I can do, the problem I am having is trying to figure out how to pass those values to another PHP page.

Here is the code of the page

Code: Select all

<!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>Form Generator</title><script src="ajax.js" type="text/javascript"></script><link rel = "stylesheet" type = "text/css" href = "tsehkay.css" /><script language="javascript" type="text/javascript">function validate() {    alert('function called'); }//Add more fields dynamically.function addField(field,fname,limit) { if(!document.getElementById) return; //Prevent older browsers from getting any further. var field_area = document.getElementById(field); var field_name = document.getElementById(fname).value; var all_inputs = field_area.getElementsByTagName("input"); //Get all the input fields in the given area. //Find the count of the last element of the list. It will be in the format '<field><number>'. If the  //  field given in the argument is 'friend_' the last id will be 'friend_4'. if (all_inputs.length > 0) { // something has been entered    var last_item = all_inputs.length - 1;    var last = all_inputs[last_item].id;    var count = Number(last.split("_")[1]) + 1; } else { // nothing has been entered yet    count = 0; } //If the maximum number of elements have been reached, exit the function. //  If the given limit is lower than 0, infinite number of fields can be created.    //W3C Dom method.  var remove_entry = "<a style='cursor:pointer;' onclick='this.parentNode.parentNode.removeChild(this.parentNode);'>Remove Field</a> &nbsp; &nbsp;"; //var ne = document.createElement("span");// ne.innerHTML=remove_entry;  var li = document.createElement("li");  li.innerHTML = remove_entry + " FIELD" + (count+ 1) + ": ";  var input = document.createElement("input");  input.id = "field_"+ count; // alert (input.id);  input.name = "field_"+ count;  input.type = "text"; //Type of field - can be any valid input type like text,file,checkbox etc.  input.value = field_name; li.appendChild(input);  //li.appendChild(ne);   field_area.appendChild(li);  } </script> </head> <body>  <div id="content2"> <h1>Form Generator</h1>   <strong>NOTE</strong>: This system is currently under development, and there may be changes. <form name="frm" method="POST"><H3>ADD NEW FIELD:</h3><br />Field Name: <input type="text" name="field_name" id="field_name" maxlength="50" /><br />Data Type: <select name="data_type" id="data_type"><option>Text</option><option>Numeric</option></select><br /> <input type="button" value="Add Field" onclick="addField('new_fields','field_name', 'field_type');" /> </form> <hr /><H3>FIELDS ADDED:</H3>  <form action="table.php" method="POST" name="form_gen"><strong>Table Name: </strong><input type="text" name="new_table" id="new_table" /><br /><ol id="new_fields"> </ol><input type="hidden" name="create_form" id="create_form" value="Y" /><input type="submit" name="form_gen" value="Finished" /></form>  </div> </div></body>   </html>
How would i get it to pass the values of this dynamic form to another PHP page?

THanks for your help
User avatar
Chalks
Forum Contributor
Posts: 447
Joined: Thu Jul 12, 2007 7:55 am
Location: Indiana

Re: Passing values from a dynamicaly created form to new page

Post by Chalks »

there are two ways to handle this. The easiest way to get information from one page to another is to use sessions and access the data using:

Code: Select all

<?php echo $_SESSION['variableName']; ?>
If you want to actually use a form as it is intended (which it looks like you should be doing here) edit line 67 to:

Code: Select all

<form name="frm" method="POST" action="anotherPage.php">
on anotherPage.php you can access all your form elements using the names as variables. like so:

Code: Select all

<?php echo $_POST['field_name'];
echo $_POST['data_type'];
//etc...
?>
johnblaze00
Forum Newbie
Posts: 4
Joined: Wed Jun 03, 2009 2:04 pm

Re: Passing values from a dynamicaly created form to new page

Post by johnblaze00 »

Chalks wrote:there are two ways to handle this. The easiest way to get information from one page to another is to use sessions and access the data using:

Code: Select all

<?php echo $_SESSION['variableName']; ?>
If you want to actually use a form as it is intended (which it looks like you should be doing here) edit line 67 to:

Code: Select all

<form name="frm" method="POST" action="anotherPage.php">
on anotherPage.php you can access all your form elements using the names as variables. like so:

Code: Select all

<?php echo $_POST['field_name'];
echo $_POST['data_type'];
//etc...
?>
thank you for your help. But the problem i seem to running into is how is that next page suppose to know what those form values are or how many they are going to be because the user will be able to generate a table with 1 or 10000 values.

Also when i hit the finish button on my page, nothing shows up on the "anotherPage.php"
User avatar
Chalks
Forum Contributor
Posts: 447
Joined: Thu Jul 12, 2007 7:55 am
Location: Indiana

Re: Passing values from a dynamicaly created form to new page

Post by Chalks »

johnblaze00 wrote:thank you for your help. But the problem i seem to running into is how is that next page suppose to know what those form values are or how many they are going to be because the user will be able to generate a table with 1 or 10000 values.

Also when i hit the finish button on my page, nothing shows up on the "anotherPage.php"
Oh, I see... I misread the form. There's an easy way to fix that too... add a "[]" to each named form element, like so:

Code: Select all

Field Name: <input type="text" name="field_name[]" id="field_name" maxlength="50" /><br />
Data Type: <select name="data_type[]" id="data_type"><option>Text</option><option>Numeric</option></select><br />
 
<input type="button" value="Add Field" onclick="addField('new_fields','field_name[]', 'field_type[]');" />
then the php code is:

Code: Select all

<?php
foreach($_POST['field_name'] AS $fn) {
echo $fn; }
 
foreach($_POST['data_type'] AS $dt) {
echo $dt; }
?>
johnblaze00
Forum Newbie
Posts: 4
Joined: Wed Jun 03, 2009 2:04 pm

Re: Passing values from a dynamicaly created form to new page

Post by johnblaze00 »

Chalks wrote:
johnblaze00 wrote:thank you for your help. But the problem i seem to running into is how is that next page suppose to know what those form values are or how many they are going to be because the user will be able to generate a table with 1 or 10000 values.

Also when i hit the finish button on my page, nothing shows up on the "anotherPage.php"
Oh, I see... I misread the form. There's an easy way to fix that too... add a "[]" to each named form element, like so:

Code: Select all

Field Name: <input type="text" name="field_name[]" id="field_name" maxlength="50" /><br />
Data Type: <select name="data_type[]" id="data_type"><option>Text</option><option>Numeric</option></select><br />
 
<input type="button" value="Add Field" onclick="addField('new_fields','field_name[]', 'field_type[]');" />
then the php code is:

Code: Select all

<?php
foreach($_POST['field_name'] AS $fn) {
echo $fn; }
 
foreach($_POST['data_type'] AS $dt) {
echo $dt; }
?>
doing that kills the javascript form, it no longer adds a new field.. Also what does adding the "[]" exactly do? Shouldn't i create some sort of loop or array to dynamically create the names of the fields?
User avatar
Chalks
Forum Contributor
Posts: 447
Joined: Thu Jul 12, 2007 7:55 am
Location: Indiana

Re: Passing values from a dynamicaly created form to new page

Post by Chalks »

adding the [] turns it into an array. Which is what you need. I don't know how you have the javascript set up (I didn't really inspect the code), but it should be trivial to have the added field have the name automatically set to be "nameofthefield[]" instead of "nameofthefield"

edit: if you want to have it dynamically create the field names, you'll want to have it simply add a number (i.e. 0-X) and then do a for loop to access all of them.
johnblaze00
Forum Newbie
Posts: 4
Joined: Wed Jun 03, 2009 2:04 pm

Re: Passing values from a dynamicaly created form to new page

Post by johnblaze00 »

I have some progress going. Chalks I really appreciate the help so far but there is just one final request. When each field is created the name they are given is "field_0, field_1, field_2 etc...". Now how would i get the next page to retrieve those values?

edit: figured it out using a for loop

Code: Select all

<?php
for ($i=0; $i <=100; $i++)
echo $_POST['field_' . $i++]
?>
figured it out, thanks!
User avatar
mikemike
Forum Contributor
Posts: 355
Joined: Sun May 24, 2009 5:37 pm
Location: Chester, UK

Re: Passing values from a dynamicaly created form to new page

Post by mikemike »

You should really be using arrays for this
Post Reply