Dynamically added text fields passed to PHP script | Unix Linux Forums | Programming

  Go Back    


Programming Post questions about C, C++, Java, SQL, and other programming languages here.

Dynamically added text fields passed to PHP script

Programming


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 03-26-2013
GroveTuckey GroveTuckey is offline
Registered User
 
Join Date: Jan 2011
Last Activity: 27 February 2014, 4:31 PM EST
Posts: 37
Thanks: 0
Thanked 2 Times in 2 Posts
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 07:36 PM..
Sponsored Links
    #2  
Old 03-26-2013
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 24 April 2014, 7:42 PM EDT
Location: Saskatchewan
Posts: 18,539
Thanks: 682
Thanked 3,036 Times in 2,856 Posts
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.
Sponsored Links
    #3  
Old 03-26-2013
GroveTuckey GroveTuckey is offline
Registered User
 
Join Date: Jan 2011
Last Activity: 27 February 2014, 4:31 PM EST
Posts: 37
Thanks: 0
Thanked 2 Times in 2 Posts
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
Corona688 Corona688 is offline Forum Staff  
Mead Rotor
 
Join Date: Aug 2005
Last Activity: 24 April 2014, 7:42 PM EDT
Location: Saskatchewan
Posts: 18,539
Thanks: 682
Thanked 3,036 Times in 2,856 Posts
So you have a dozen textboxes with the exact same title?
Sponsored Links
    #5  
Old 03-26-2013
GroveTuckey GroveTuckey is offline
Registered User
 
Join Date: Jan 2011
Last Activity: 27 February 2014, 4:31 PM EST
Posts: 37
Thanks: 0
Thanked 2 Times in 2 Posts
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

Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Swap of fields dynamically i150371485 Shell Programming and Scripting 4 10-22-2012 07:48 AM
How to create dynamically associative array in PHP? vanitham Shell Programming and Scripting 1 10-18-2010 05:24 AM
Dynamically creating text files using shell script KiranKumarKarre Shell Programming and Scripting 2 05-07-2009 08:02 AM
I want Trailer to be added into the text file. Haque123 Shell Programming and Scripting 5 04-02-2009 07:13 AM
Get the latest added part of a text file? pcrs Shell Programming and Scripting 3 10-27-2008 03:33 AM



All times are GMT -4. The time now is 05:15 AM.