[SOLVED] Retrieve values from dynamic textboxes

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
Cezar
Forum Newbie
Posts: 5
Joined: Tue Oct 12, 2004 8:21 am

[SOLVED] Retrieve values from dynamic textboxes

Post by Cezar »

nigma | Help us, help you. Please use

Code: Select all

and

Code: Select all

tags where approriate when posting code. Read: :arrow: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]

I have a form with dynamic textboxes. The code is something like this:

Code: Select all

<html> 
<head> 
<title>Dynamic Form</title> 
<script language="javascript"> 
function changeIt() 
{ 
var a = document.form.requiredquantitystd1.value; 
for (i=1; i<a;i++){ 
my_div.innerHTML = my_div.innerHTML +"<br><input type='text' name='mytext'+ i>" 
} 
} 
</script> 

</head> 
<body> 
<form name="form" action="post" method=""> 
<input type="text" name="requiredquantitystd1"> <input type="button" value="test" onClick="changeIt()"> 
<input type="submit" name="submit" value="Submit" ID="Submit1"> 
<div id="my_div"></div> 

</body> 
</html>
Using this code I get some dynamic textboxes and later on I would like to retrive the values typed in these textboxes into a php file (using a submit button).

Thanks for help!

nigma | Help us, help you. Please use

Code: Select all

and

Code: Select all

tags where approriate when posting code. Read: :arrow: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Post by nigma »

Tell your form to send its data to the php file after it is submitted by setting your forms action attribute to the name of the php file.

Code: Select all

<form action="nameofphpfile.php" method="post">
Cezar
Forum Newbie
Posts: 5
Joined: Tue Oct 12, 2004 8:21 am

Post by Cezar »

Sorry, my mistake!

The php file name is specified in the form. It is sometring like this:

Code: Select all

<html> 
<head> 
<title>Dynamic Form</title> 
<script language="javascript"> 
function changeIt() 
&#123; 
var a = document.form.requiredquantitystd1.value; 
for (i=1; i<a;i++)&#123; 
my_div.innerHTML = my_div.innerHTML +"<br><input type='text' name='mytext'+ i>" 
&#125; 
&#125; 

</script> 
</head> 
<body> 
<form name="form" action="myfile.php" method="post"> 
<input type="text" name="requiredquantitystd1"> <input type="button" value="test" onClick="changeIt()"> 
<input type="submit" name="submit" value="Submit" ID="Submit1"> 
<div id="my_div"></div> 

</body> 
</html>


I hope now it's more clear!

Thanks.
kettle_drum
DevNet Resident
Posts: 1150
Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England

Post by kettle_drum »

I would personally create a hidden var to be posted along with the form that holds the number of text boxes shown on the page - so update the value of it when you add the new text box. Then you can loop through all the values on the processing page for the text boxes.

Code: Select all

for($x=1;$x<$_POST['number'];$x++){
   echo $_POST["mytext$x"];
}
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Post by nigma »

In myfile.php :

Code: Select all

$text_fields = array();
for ($idx=1; array_key_exists("mytext$idx", $_POST); $idx++) {
  $text_fields[] = $_POST['mytext'.$idx];
}
Now all the values of the fields that the user created are stored in text_fields for later manipulation.
Cezar
Forum Newbie
Posts: 5
Joined: Tue Oct 12, 2004 8:21 am

Post by Cezar »

I got no errors, but the $text_fields[] seems to be empty, so it does not get the values from the textboxes.
Any idea why?

Thanks
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Post by nigma »

I'm not quite sure why. Edit your php file to say the following:

Code: Select all

array_map($_POST); array_map($text_fields);
And then dynamically create some text fields and enter some data into them and submit the form. Let me know what your script outputs.
Cezar
Forum Newbie
Posts: 5
Joined: Tue Oct 12, 2004 8:21 am

Post by Cezar »

It's working! The problem was in the source file:

Code: Select all

function changeIt()
&#123;
var a = document.form.quantitystd1.value;
for (i=1; i<a;i++)&#123;
my_div.innerHTML = my_div.innerHTML +"<br><input type='text' &#1111;b]name='mytext"+i+"'>"&#1111;/b]&#125;
my_div.innerHTML = my_div.innerHTML+"<br><input type='hidden' name='field' value="+a+">"
&#125;
</script>
I have also added a hidden field to pass the number of the dynamic textboxes to the php file. It wouldn't work with array_key_exists("mytext$idx", $_POST), but it's working in this way! It wasn't necessay to use array_map().

Thanks guys!
Post Reply