Dynamically added text fields passed to PHP script


 
Thread Tools Search this Thread
Top Forums Programming Dynamically added text fields passed to PHP script
# 1  
Old 03-26-2013
Dynamically added text fields passed to PHP script

If I am posting this to the wrong section please move it somewhere it fits. I apologize if this is not the correct section.

I have a site where I want to have form that in a "Visitor name" section to be able to add fieldets as needed. I think I have that worked out.

So the below code is the text area boxes. There is a First Name, Last Name and a Job Title field.
Code:
    <fieldset>
            <legend>Visitor Name</legend>
    <div id="placeholder">
            <div id="addmore">
                    <label for="firstname">First Name:</label>
                    <input type="text" name="name[]" value="">
                    <label for="lastname">Last Name:</label>
                    <input type="text" name="name[]" value"">
                    <label for="jobtitle">Job Title:</label>
                    <input type="text" name="name[]" value="">
            </div>
    </div>

and the js code to recreate the fieldset is
Code:
    var _counter = 0;
    function Add() {
        _counter++;
        var oClone = document.getElementById("addmore").cloneNode(true);
        oClone.id += (_counter + "");
        document.getElementById("placeholder").appendChild(oClone);
    }

That creates the fields when the "Add Visitor" button is pushed with no limit to the amount of dynamic fileds that can be created. It basically just copies the fieldset.

So I guess the PHP part is where I am getting hung up.

How do I pass those dynamic values into the php script and place them in a file.

When I use
Code:
    foreach($_POST['name'] as $info) {
          fwrite($file, "$some other form data,$info,some other form data \n");
        }

I get
Code:
Company,Fred,No,No,No,No,Yes,No,No,No,No,Yes,No,Yes
Company,Williams,No,No,No,No,Yes,No,No,No,No,Yes,No,Yes
Company,Hello,No,No,No,No,Yes,No,No,No,No,Yes,No,Yes
Company,Todd,No,No,No,No,Yes,No,No,No,No,Yes,No,Yes
Company,Williams,No,No,No,No,Yes,No,No,No,No,Yes,No,Yes
Company,Hello,No,No,No,No,Yes,No,No,No,No,Yes,No,Yes

Instead of
Code:
Company,Fred,Williams,Hello,No,No,No,No,Yes,No,No,No,No,Yes,No,Yes
Company,Todd,Williams,Hello,No,No,No,No,Yes,No,No,No,No,Yes,No,Yes

So I guess I am getting the data but I need it put in the above format. I assume that it is adding a line for each entry of the name $_POST.

Sorry for my ignorance on this.

when I do:
Code:
    $my_array = $_POST['name'];
    $visitor = implode(",",$my_array);

and then
Code:
    echo "$some other form data,$visitor,some other form data <br />";

and have more than one name it give me
Code:
Company,Billy,Williams,test,Bobby,Williams,test,No,No,No,No,No,No,No,No,No,No,No,No

to which I get that is is just taking the $visitor array and adding to the $visitor section. So it is getting the data I just still can't get it in a file one line per name.

and when I do
Code:
    print_r($visitor);

I get the information that was entered into the text boxes in one line.

Code:
Billy,Williams,test,Bob,Williams,test

and when I do

Code:
print_r($_POST);

I get

Code:
Array ( [companyname] => Buster [name] => Array ( [0] => bob [1] => Buddy [2] => Test [3] => bob [4] => Buddy [5] => Test ) [Submit] => Add Visitor )

So as of now I am stuck. I tried to search the forum and did not find anything, so if there is a post please point me in that direction.

Thanks in advance for your help.

Last edited by GroveTuckey; 03-26-2013 at 08:36 PM..
# 2  
Old 03-26-2013
So they are stored as names boxes1...boxesn? You could do a while-loop, checking whether each column exists in POST, adding to the end of a string each time. Then the rest of the fields once that's done.
# 3  
Old 03-26-2013
well

Code:
$boxes,$boxes1,$boxes2,$boxes3,$boxes4,$boxes5,$boxes6,$boxes7,$boxes8,$boxes9,$boxes10,$boxes11

is other data in the form.

I feel like I am misunderstanding your suggestion.

I should have left the boxes out of the post so not to confuse the subject. I will edit the original post.
# 4  
Old 03-26-2013
So you have a dozen textboxes with the exact same title?
# 5  
Old 03-26-2013
Just three until the copying of the fieldsets

Code:
 
<fieldset>
            <legend>Visitor Name</legend>
    <div id="placeholder">
            <div id="addmore">
                    <label for="firstname">First Name:</label>
                    <input type="text" name="name[]" value="">
                    <label for="lastname">Last Name:</label>
                    <input type="text" name="name[]" value"">
                    <label for="jobtitle">Job Title:</label>
                    <input type="text" name="name[]" value="">
            </div>
    </div>
</fieldset>

There is no limit to how many times the fieldset can be copied.

---------- Post updated at 10:18 PM ---------- Previous update was at 09:38 PM ----------

Ok I got this to do what I wanted to do. Here is the code I used

Code:
 
foreach($firstname as $key => $value){
fwrite($file, "$companyname,$value,$lastname[$key],$jobtitle[$key],$boxes,$boxes1,$boxes2,$boxes3,$boxes4,$boxes5,$boxes6,$boxes7,$boxes8,$boxes9,$boxes10,$boxes11 \n");

My output file now looks like

Code:
 
Company,FirstName,Lastname,JobTitle,ProcessCenter,Receiving,Warehouse,DigitalPrint,Bindery,Mailing,Picking,QualityControl,Packing,Collation,Shipping,ClientServices
Company,FirstName,Lastname,Print,No,No,No,No,No,No,No,No,No,No,No,No
Company,FirstName,Lastname,Print,No,No,No,No,No,No,No,No,No,No,No,No
Company,FirstName,Lastname,Print,No,No,No,No,No,No,No,No,No,No,No,No

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Shell script to create runtime variables based on the number of parameters passed in the script

Hi All, I have a script which intends to create as many variables at runtime, as the number of parameters passed to it. The script needs to save these parameter values in the variables created and print them abc.sh ---------- export Numbr_Parms=$# export a=1 while do export... (3 Replies)
Discussion started by: dev.devil.1983
3 Replies

2. UNIX for Dummies Questions & Answers

Sending mail to multiple recipient added in a text file

I am trying to find a code that can help me mail to a list of recipients which are in a text file. Sample code $cat recipient.txt me@test.com me1@test.com me2@test.com I want a mailx step that can read contents of recipient.txt and mail to all the users. I don't want to use mails... (1 Reply)
Discussion started by: Gurkamal83
1 Replies

3. Shell Programming and Scripting

Swap of fields dynamically

Dear Friends, I have file a.txt 1|2|3|4|5|6|7|8 a|b|c|d|e|f|g|h i am using the below code to swap the fields in file awk -F\| '{print $5,$1,$2,$3,$4,$6,$7,$8}' OFS=\| a.txt > output.txt output.txt 5|1|2|3|4|6|7|8 e|a|b|c|d|f|g|h The above command is working fine. I am... (4 Replies)
Discussion started by: i150371485
4 Replies

4. Shell Programming and Scripting

Shell script to find the sum of argument passed to the script

I want to make a script which takes the number of argument, add those argument and gives output to the user, but I am not getting through... Script that i am using is below : #!/bin/bash sum=0 for i in $@ do sum=$sum+$1 echo $sum shift done I am executing the script as... (3 Replies)
Discussion started by: mukulverma2408
3 Replies

5. Shell Programming and Scripting

How to create dynamically associative array in PHP?

Hi, I have an html page like this: <html> <body> <form action="test.php" method = "post"> Enter your name:<input name="search" type = "text" size ="40"> <br> Enter your age:<input name="age" type = "text" size ="20"> <input type = "submit" name="submit" value="search"> <input type =... (1 Reply)
Discussion started by: vanitham
1 Replies

6. Shell Programming and Scripting

Need to echo a text where a (.) is added to the end of line during on-going copy or ftp

Hello All, I would like to create a script to echo a text where a (.) dot is added every 2 seconds to the end of this text (with a limit of 10 dots) as long as a file copy or ftp is ongoing and once the copy is finished it adds "done" to the end of this text line. please see the below example: ... (6 Replies)
Discussion started by: Dendany83
6 Replies

7. Shell Programming and Scripting

Dynamically creating text files using shell script

Hi All, I want to create a shell script which dynamically create text files. i am using the following script $i=1 while do cat > test_$i.txt done but while running the script it was stopping(the cursor not going to next step, i have to enter ctrl+c to make it stop). it is creating only... (2 Replies)
Discussion started by: KiranKumarKarre
2 Replies

8. Shell Programming and Scripting

I want Trailer to be added into the text file.

Hi folks, I want Trailer to be added into the txt file the format is below. flatfile-> abc.txt count of the file is 500 records. I want the trailer in this format: TRAILER|500 (pipe delimeter). Please suggest the comands ASAP. Rgds Ann (5 Replies)
Discussion started by: Haque123
5 Replies

9. Shell Programming and Scripting

Get the latest added part of a text file?

Hi, I want to get the latest added part of a text file sent over the network to for analysis. Is there a tool to use to keep track of which part of a text file that has already been analysed so only the "new" lines will be sent and handled? I have checked a few tools but I still donīt know how to... (3 Replies)
Discussion started by: pcrs
3 Replies

10. Shell Programming and Scripting

combining fields in two text fields

Can someone tell me how to do this using sed, awk, or any other basic shell scripting? Basically I have two text files with the following contained in each file: File A: a b c d e f g h i File B: 1 2 3 I want the final outcome to look like this: a b c 1 d e f 2 g h i 3 How... (3 Replies)
Discussion started by: shocker
3 Replies
Login or Register to Ask a Question