Solved

want to make a dynamic form

Posted on 2009-07-14
19
258 Views
Last Modified: 2012-05-07
want to make a dynamic form. like i want to provide the user a facility that he can add the fields ,delete the fields and update the fields according to his wish. and after that he can update the record.

there will be only 1 record , not more than that but that record can be updated.
0
Comment
Question by:designersx
19 Comments
 
LVL 10

Expert Comment

by:racmail2001
ID: 24856835
you want the user to customize the form?
how you will be able to make a layout for that? do you make each input on diferent line?

if that is true you can make a form with this fields:
1. label of the input
2. name of the input
3.  input type
4 - optional parameters - like size for text input or default value
5.  optional data type - helps you with the validation process
6. required or not - helps you with the validation process

all this values you can store them in a mysql table and generate the form based on that.

the problem will be when you will process the generated form.
you will need to make a foreach loop and validate all the values.

0
 

Author Comment

by:designersx
ID: 24857413
i am building the complete process and it is going good. just tell me

how to write the alter query for dropdown.

generally we write like below but how to write when the user wants any field to be dropdown.


$sql = "alter table $tname add `$field_name` $field_type(255) NOT NULL";

Open in new window

0
 

Author Comment

by:designersx
ID: 24857452
like this , i got it , tell me is it right?

$sql = "alter table `$tname` add `$field_name` enum(255) NOT NULL";
0
 
LVL 2

Expert Comment

by:dinu_sorin
ID: 24857994
no, is not right.
$sql = "alter table `$tname` add `$field_name` enum(255) NOT NULL";

The value in the () means the lenght*. For exemple varchar(255) means a 255 characters string. Text dont need to have a lenght. Floats are different, cause u must set the lenght and the precision.
*If field type is "enum" or "set", enter the values using this format: 'a','b','c'...

Read the sql(mysql) documentation.
0
 

Author Comment

by:designersx
ID: 24867446
hello sir, now consider i have a form like this below.

i want to send the values of dynamic field to the js file and then to the php page. how can i do it, please help.
i am trying like this

function save(){
      a=1;
      p= 5; //assume here
      for(a=1;a<=5;a++){
            if(field_type is textbox get its id)
                  document.getElementById('??')
            }
            
            if(field_type is upload get its id))      {
                  document.getElementById('??')
            }
      http.open('get','save_details.php????',true)      
            
      }
}

i have tried my best from further i don't know how to proceed. please help
<script language="javascript" type="text/javascript">

function save(){

	a=1;

	p= 5; //assume here

	for(a=1;a<=5;a++){

		if(field_type is textbox get its id)

			document.getElementById('??')

		}

		

		if(field_type is upload get its id))	{

			document.getElementById('??')

		}

	http.open('get','save_details.php????',true)	

		

	}

}	

</script>
 

<form action="javascript:save()" method="post" name="form1">

<table><tr><td>Form</td></tr>

<?php

$sql = "select * from `contactus_master`"; 

$result = mysql_query($sql) or die("could not select".mysql_error());
 

$i=1;

while($rec=mysql_fetch_array($result)){

	?><tr>

	  <td><?php $field_type = $rec['field_type'];

	  

		if($field_type == 'textbox'){ 

			?><input type="text" name="textbox_<?php echo $i; ?>" id="textbox_<?php echo $i; ?>" /><?php }

		if($field_type == 'upload'){

			?><input type="file" name="upload_<?php echo $i; ?>" id="upload_<?php echo $i; ?>" /><?php }

		?>

	  </td></tr>

	<?php $i++; 

} ?>

</table>

</form>

Open in new window

0
 

Author Comment

by:designersx
ID: 24867450
what to write at line 6,7,10,11, 13
0
 
LVL 33

Expert Comment

by:Slick812
ID: 24872251
Hello designersx, , Maybe I do not really understand what you are trying to do? But your javascript for form access is not so good, I think? You seem to want to use the getElementById for form elements, in JS forms have their own elements, so I never use getElementById for forms. Here is some code I might use to get the "VALUE" of the text in text and file elements in a form - - -

<html><head><title>Form Do</title><script language="JavaScript">
function sendForm(){if(document.cform[0].value.length<3){alert('ERROR, you must have at least 3 letters');document.cform[0].focus();return false;}
allText='Text Box Contents ';allFile='File Box Contents ';
for(i=0;i<document.cform.length;++i){if(document.cform[i].type=='text') allText+='|'+document.cform[i].value;}
for(i=0;i<document.cform.length;++i){if(document.cform[i].type=='file') allFile+='|'+document.cform[i].value;}
alert(allText);
alert(allFile);
return false; // this will NEVER submit , because it always returns false,
// you nee to change this to a working model
}
</script></head><BODY BGCOLOR="#D3E7F2" TEXT="#000000">
<form name="cform" action="save_details.php" method="post" onsubmit="return sendForm()">
Name: <input type="text" name="textbox_1" value="Bob"/><br/>
Address: <input type="text" name="textbox_2" value="123 Mn"/><br/>
A Image Up: <input type="file" name="upload1" value="C:/joke.txt"/><br/>
B Image Up: <input type="file" name="upload2" value="C:/an.gif"/><br/>
<input type="submit" value="Submit This"> <br/></form></body></html>

you can loop through all the elements with document.cform[i], (the form name is "cform"). and the "type" gets the type of input as text, file, button, etc.  What realy has me puzzled is your-
http.open('get','save_details.php????',true);

I do not believe that you can do a file upload through the "GET" submission to a PHP script. Why are you trying to use this method? ? ?
0
 
LVL 33

Expert Comment

by:Slick812
ID: 24872281
sorry, code went around lines, here is another try
<html><head><title>Form Do</title><script language="JavaScript">

function sendForm(){if(document.cform[0].value.length<3){alert('ERROR, you must have at least 3 letters');document.cform[0].focus();return false;}

allText='Text Box Contents ';allFile='File Box Contents ';

for(i=0;i<document.cform.length;++i){if(document.cform[i].type=='text') allText+='|'+document.cform[i].value;}

for(i=0;i<document.cform.length;++i){if(document.cform[i].type=='file') allFile+='|'+document.cform[i].value;}

alert(allText);

alert(allFile);

return false;}

</script></head><BODY BGCOLOR="#D3E7F2" TEXT="#000000">

<form name="cform" action="save_details.php" method="post" onsubmit="return sendForm()">

Name: <input type="text" name="textbox_1" value="Bob"/><br/>

Address: <input type="text" name="textbox_2" value="123 Mn"/><br/>

A Image Up: <input type="file" name="upload1" value="C:/joke.txt"/><br/>

B Image Up: <input type="file" name="upload2" value="C:/an.gif"/><br/>

<input type="submit" value="Submit This"> <br/></form></body></html>

Open in new window

0
 

Author Comment

by:designersx
ID: 24872556
sir this is my updated code and it works fine upto http.open

that means i am using this method to get the values of the field and i  have been successful on that . this is totally dynamic. let me clarify why i am doing like this because i can have any number of fields in the database and they can be enum , longtext , varchar anything as chosen by the user.

upto line 16  u need not worry. now we need to work on http.open function.

let me explain u the complete scenarios , there are 2 cases:- and after that i will tell u 1 thing.

1) when we write http.open inside the loop then that means we are going to php page again and again same as the number of fields that seems wrong.

2) so if we write http.open outside the loop then how we can store the values of fields ?

this is the problem, sir is there any solution for this??

finally from the above discussion , i think we need to take an array in which we will store all the values of all the fields and then send to the php page.

my line 18 is incomplete, we need to do work on this only..
function save(){

        var elements = document.frm.elements;

        for(var x =0;x<elements.length;x++){

         if(elements[x].type == "wysiwyg"){

                   document.getElementsByTagName(elements[x].name);

          }

         

         if(elements[x].type == "text"){

                   document.getElementsByTagName(elements[x].name);

          }

         if(elements[x].type == "file"){

                   document.getElementsByTagName(elements[x].name);

          } 

         alert("name="+elements[x].name);

         alert("type="+elements[x].type);

         alert("value="+elements[x].value);

                 

         http.open('get','save_details.php?',true)

         

        }

         http.onreadystatechange = function(){

         if(http.readyState == 4){

                var response = http.responseText;

                //document.getElementById('contact_save_details').innerHTML = response;

                }

        }

        http.send(null);        

}

Open in new window

0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

Author Comment

by:designersx
ID: 24872574
the form which i am making dynamic is like this below.

please do let me know if there is any confusion.
<form action="javascript:save()" method="post" name="form1">

<table><tr><td>Form</td></tr>

<?php

$sql = "select * from `contactus_master`"; 

$result = mysql_query($sql) or die("could not select".mysql_error());

 

$i=1;

while($rec=mysql_fetch_array($result)){

        ?><tr>

          <td><?php $field_type = $rec['field_type'];

          

                if($field_type == 'textbox'){ 

                        ?><input type="text" name="textbox_<?php echo $i; ?>" id="textbox_<?php echo $i; ?>" /><?php }

                if($field_type == 'upload'){

                        ?><input type="file" name="upload_<?php echo $i; ?>" id="upload_<?php echo $i; ?>" /><?php }

                ?>

          </td></tr>

        <?php $i++; 

} ?>

</table>

</form>

Open in new window

0
 

Author Comment

by:designersx
ID: 24872605
it was my opinion to take an array , it can be wrong but i am not sure for that. but if it is right then we should proceed it like this

a=array();
for(){
a[]=//...get the values.
}
0
 
LVL 33

Expert Comment

by:Slick812
ID: 24873175
?? I had NO IDEA, you were sending this  as an AJAX responce, sorry. . . First in your javascript you have the code-
if(elements[x].type == "file"){ document.getElementsByTagName(elements[x].name);}

I do not believe this does anything, shouldn't those 3 lines  be setting some value like -
aElemt  =  document.getElementsByTagName(elements[x].name);
but you dont need this, as far as I can see

as to the send to ajax, This is some code that I used to send to ajax - -


inSt = domIn.value.replace(/\^/g, "~");

inSt += '^'+siv;// siv is a settings value

//to send an array to PHP I use the ^ as a delimiter

inSt += '^end of settings';// you can replace with what you need
 

send2 = 'job1=22&tim='+encodeURIComponent(verHex)+'&cit='+encodeURIComponent(inSt);
 

ajaxObj.open('POST', 'skytalk2.php', true);

ajaxObj.onreadystatechange=ajaxCalled;

ajaxObj.setRequestHeader('Content-Type','application/x-www-form-urlencoded');

ajaxObj.setRequestHeader ("Content-length", send2.length);

ajaxObj.setRequestHeader ("Connection", "close");

ajaxObj.send(send2);
 

- - - - - - - - -

on the PHP side I used -

$arySet = explode('^', $SeRe);

to get an array from the ^ delimited text I sent

Open in new window

0
 

Author Comment

by:designersx
ID: 24873325
real thanks for ur response, it seems as if u have much experience in ajax and php app's. sir i am just a beginner in ajax, ur code was really out of my mind but i will definitely going to use ur code and check if it is useful to me, after that i will also be able to implement this later in my apps.

thanks for ur code and time respected sir. let me check , let me confirm some of the things from you.

1) does ur code picks the values of the form fields and sends to the php page? form fields can be any

ok let me try.
0
 
LVL 33

Expert Comment

by:Slick812
ID: 24873924
In the code example that I sent the
domIn.value.replace
is the text 'value' of the domIn, ,  a form TEXT input, maybe I should have used -
document.cform[0].value.replace(/\^/g, "~");
for you to see more clearly
the   siv   is string (text)  variable That has several "settings" values in it, these are NOT from any user input, but instructions for the PHP to know what to do

the  verHex   and  inSt  are both from Input boxes, but I add information to that text, as a delimited ^ so I can get it out later in PHP. . . and yes, form fields can be any

also, I almost always send data back to the ajax as a delimited text array, with javascript like this -
infoArray = ajaxObj.responseText.split('^');
if (infoArray[0] == '11') {verHex = infoArray[[1];}
0
 

Author Comment

by:designersx
ID: 24885149
firstly sir thanks for ur code but sir i think that the code is really of very different stuff, either u r trying to do something different. and also u have not given the exact code, so really i am not able to use it either sry to say it is of no use for me because in ur above code, i can't see any line of code where we fetching the textbox value or any field value, so it confusing me a lot.

if we take the hidden fields then i think we can do but i don't know that method .
 if u can help other ways that would be great.
0
 
LVL 33

Expert Comment

by:Slick812
ID: 24886367
? ? , , I begin to see that you have not ever made an AJAX-PHP web page that has worked , to send data back and forth, and dynamically change the web page in your browser. If you have not made a AJAX page that works, you should start by learning the AJAX methods. and then go on to including other things (like the Data base).
I will do an AJAX page for you, if you need to learn AJAX, but your original question here is not about that (ajax-php). Do you need code to do an AJAX page update? or do you know ajax methods already?
0
 

Author Comment

by:designersx
ID: 24886727
i know ajax methods very clearly and fine.
0
 
LVL 33

Accepted Solution

by:
Slick812 earned 125 total points
ID: 24887246
That is good that you know some AJAX methods. . . But from your statement -
"i am not able to use it either sry to say it is of no use for me because in ur above code, i can't see any line of code where we fetching the textbox value or any field value, so it confusing me a lot"

I can not understand what you might want me to explain, or give code examples for? ?
Did you use my code -
ajaxObj.open('POST', 'adata.php', true);
to successfully send and receive ajax data and update some page area with innerHTML javascript?
I do not know what to do next.
0
 

Author Comment

by:designersx
ID: 24888910
sir i know that this is my fault that i put you in vein , sry for that. i will respond earlier.
0

Featured Post

Easy Project Management (No User Manual Required)

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now