Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 15 of 15
  1. #1
    New Coder
    Join Date
    Jul 2013
    Posts
    21
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Question Display value to several input type text from text file

    I have 100 input type text boxes and would like to display the value to each box from a txt file.
    I couldn't figure it out in PHP and thought it might be easier in JavaScript. But I am not familiar with JavaScript. My text fields are being displayed as:

    Code:
        <input type="text" size="13" name="contacts[]" id="contact0">
        <input type="text" size="13" name="contacts[]" id="contact1">
        <input type="text" size="13" name="contacts[]" id="contact2">
        <input type="text" size="13" name="contacts[]" id="contact3">
    So I need to add value to the text boxes from contacts.txt which has peoples names in line by line.
    To show as:
    Code:
        <input type="text" size="13" name="contacts[]" id="contact0" value="David">
        <input type="text" size="13" name="contacts[]" id="contact1" value="Erick">
        <input type="text" size="13" name="contacts[]" id="contact2" value="John">
        <input type="text" size="13" name="contacts[]" id="contact3" value="Frank">

  • #2
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,462
    Thanks
    23
    Thanked 634 Times in 633 Posts
    Easy to do in php. Hard to do in javascript if your not familiar with JavaScript.
    Before HTML5 JS could not do this.
    You should check if you can. Run this javascript:
    Code:
    <script type="text/javascript">
    if (window.File && window.FileReader && window.FileList && window.Blob) {
      alert('Great success! All the File APIs are supported.');
    } else {
      alert('The File APIs are not fully supported in this browser.');
    }
    </script>
    I can't read files with JS. But I can in php. Try this:
    PHP Code:
    <?php
    $file 
    fopen("contacts.txt""r") or exit("Unable to open file!");
    $i 0;
    //Output a line of the file until the end is reached
    while(!feof($file))
      {
      
    $line fgets($file);
      echo 
    "<input type=\"text\" size=\"13\" name=\"contacts[]\" id=\"contact".$i."\" value=\"".$line."\">";
      
    $i++;
      }
    fclose($file);
    ?>
    PS This will give you a long line of input boxes. Maybe you should put a <br />after the echo like this ......value=\"".$line."\"><br />";
    Last edited by sunfighter; 07-21-2013 at 03:43 AM. Reason: Info on the long line without breaks inserted
    Evolution - The non-random survival of random variants.

    "If you leave hydrogen alone, for long enough, it begins to think about itself."

  • #3
    Senior Coder
    Join Date
    Sep 2010
    Posts
    2,217
    Thanks
    15
    Thanked 253 Times in 253 Posts
    You may be able to do it with javascript too, but the getElementsByName function will balk at the square brackets, so give them a class as well.
    Code:
    <input type="text" size="13" class="contacts" name="contacts[]" id="contact0">
    
     <input type="text" size="13" class="mycontacts" name="contacts[]" id="contact0">
      ////
    var cont = document.getElementsByClassName("contacts");
    var mycont = document.getElementsByClassName("mycontacts");
    
    for(i=0;i<cont.length;i++)
    {
    mycont.value = cont.value;
    }
    Of course you can add conditions to that.
    Welcome to http://www.myphotowizard.net

    where you can edit images, make a photo calendar, add text to images, and do much more.


    When you know what you're doing it's called Engineering, when you don't know, it's called Research and Development. And you can always charge more for Research and Development.

  • Users who have thanked DrDOS for this post:

    lnsflive (07-21-2013)

  • #4
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,462
    Thanks
    23
    Thanked 634 Times in 633 Posts
    @DrDOS, The question was not how to get information from a text input box via the JS function 'document.getElementsByClassName()', but to read a text file with JS and then insert each line of that text file into the 'value="" 'attribute.
    Evolution - The non-random survival of random variants.

    "If you leave hydrogen alone, for long enough, it begins to think about itself."

  • #5
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,361
    Thanks
    61
    Thanked 530 Times in 517 Posts
    Quote Originally Posted by sunfighter View Post
    @DrDOS, The question was not how to get information from a text input box via the JS function 'document.getElementsByClassName()', but to read a text file with JS and then insert each line of that text file into the 'value="" 'attribute.
    Look at the blue line in DrDos's signature. It kind of explains how his mind works
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #6
    Senior Coder
    Join Date
    Sep 2010
    Posts
    2,217
    Thanks
    15
    Thanked 253 Times in 253 Posts
    Yes, I often don't quite get the point, and my methods aren't always kosher, but I do solve problems.

    You can use file(), but it has the annoying newline problem, which has to be cleaned up after. If you're on a Linux server, or at least have BASH shell access, you can use the builtin 'while read' which has no equivalent in php. It requires that the last line of the file be completely empty, and you have to be careful of line endings. To use it in php use backslahes.

    `while read line; do { echo \"$line\", >> list.php } done < list.txt `;

    which will append "Smith", to list.php, if the line is Smith. You read $line as 'the value of the line'. So you can use it to make arrays very nicely, JS or php, and use them however you like. You just need to open the file being made and put a beginning to it, and after appended to, append an ending. You can do all those with BASH too.

    It's also very simple and dependable to convert textbox entries, using javascript, into an array, where each line is an element of the array. You could make a textbox with cols=1 rows=1 and the value is your list, and do that. Just use:
    Code:
    var lines = document.getElementById("mylist").value;
    line = lines.split("\n");
    Welcome to http://www.myphotowizard.net

    where you can edit images, make a photo calendar, add text to images, and do much more.


    When you know what you're doing it's called Engineering, when you don't know, it's called Research and Development. And you can always charge more for Research and Development.

  • #7
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,361
    Thanks
    61
    Thanked 530 Times in 517 Posts
    Quote Originally Posted by DrDOS View Post
    but I do solve problems.

    you can use the builtin 'while read' which has no equivalent in php.
    Some bold statements there

    Whats this about?

    Quote Originally Posted by DrDOS View Post
    You can use file(), but it has the annoying newline problem, which has to be cleaned up after.




    I personally think file() is fine and I don't see the new line as a problem at all
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #8
    Senior Coder
    Join Date
    Sep 2010
    Posts
    2,217
    Thanks
    15
    Thanked 253 Times in 253 Posts
    Quote Originally Posted by tangoforce View Post
    Some bold statements there

    Whats this about?





    I personally think file() is fine and I don't see the new line as a problem at all
    That was added in 5.0, they finally made it smart. A whole new ball game, I can revise some of my old code Thank you for that.
    Welcome to http://www.myphotowizard.net

    where you can edit images, make a photo calendar, add text to images, and do much more.


    When you know what you're doing it's called Engineering, when you don't know, it's called Research and Development. And you can always charge more for Research and Development.

  • #9
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,462
    Thanks
    23
    Thanked 634 Times in 633 Posts
    @DrDOS,
    Yes, I often don't quite get the point, and my methods aren't always kosher,....
    Lol, Me too, welcome to the club. I blame mine on bad eyes and age.
    Evolution - The non-random survival of random variants.

    "If you leave hydrogen alone, for long enough, it begins to think about itself."

  • #10
    Senior Coder
    Join Date
    Sep 2010
    Posts
    2,217
    Thanks
    15
    Thanked 253 Times in 253 Posts
    Quote Originally Posted by sunfighter View Post
    @DrDOS, Lol, Me too, welcome to the club. I blame mine on bad eyes and age.
    Me too. Combined with people often not stating the problem very well.

    Your solution, combined with the newer version of file() would be the most elegant way. They, php, should have a function with that default behavior. It should have been the default to begin with, and their flags are way too long winded, IGNORE_NEW_LINES should be INL or _INL. It's like they think of good things but don't always think of all the ramifications of it. And then they tack awkward stuff onto it.
    Welcome to http://www.myphotowizard.net

    where you can edit images, make a photo calendar, add text to images, and do much more.


    When you know what you're doing it's called Engineering, when you don't know, it's called Research and Development. And you can always charge more for Research and Development.

  • #11
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,361
    Thanks
    61
    Thanked 530 Times in 517 Posts
    Quote Originally Posted by DrDOS View Post
    That was added in 5.0, they finally made it smart. A whole new ball game, I can revise some of my old code Thank you for that.
    Yet you've been using 5.2.17 on your photo wizard site:



    and your site was created pretty recently (just over a year ago):



    Surely you know what functions you can use on your own server and how to use them? IMO you simply fudged your advice and can't admit it, gone to the php.net manual, noticed the changelog and used that as an excuse. This helps no-one.

    Quote Originally Posted by DrDOS View Post
    their flags are way too long winded, IGNORE_NEW_LINES should be INL or _INL. It's like they think of good things but don't always think of all the ramifications of it.
    On the contrary I think the flags and PHPs way of thinking is great!

    Think about it, if you're writing code and you get to the flags with something like INL you could have a few other abbreviations which would become confusing. IGNORE_NEW_LINES is exactly what it says and is a lot easier to remember.

    Quote Originally Posted by DrDOS View Post
    Me too. Combined with people often not stating the problem very well.
    In defence of lnsflive, they may be a new member but I personally felt they described this particular problem very well:

    Quote Originally Posted by lnsflive View Post
    I have 100 input type text boxes and would like to display the value to each box from a txt file.
    <snip>
    So I need to add value to the text boxes from contacts.txt which has peoples names in line by line.
    I'm sorry DrDos but I personally think you should think about things a bit more before firing off the replies. You've gone too far suggesting that a new member didn't describe the problem very well. Do you think that is fair when you've made the blunder?

    If you don't understand someones problem there is no obligation to reply to them. I don't reply to every topic, just the ones where I can see that I can genuinely help. Likewise if I do reply and get it wrong I can admit it - you'll see quite a bit of banter between me and Fou-Lu in that respect.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #12
    Senior Coder
    Join Date
    Sep 2010
    Posts
    2,217
    Thanks
    15
    Thanked 253 Times in 253 Posts
    Quote Originally Posted by tangoforce View Post
    Yet you've been using 5.2.17 on your photo wizard site:


    and your site was created pretty recently (just over a year ago):


    Surely you know what functions you can use on your own server and how to use them? IMO you simply fudged your advice and can't admit it, gone to the php.net manual, noticed the changelog and used that as an excuse. This helps no-one.



    On the contrary I think the flags and PHPs way of thinking is great!

    Think about it, if you're writing code and you get to the flags with something like INL you could have a few other abbreviations which would become confusing. IGNORE_NEW_LINES is exactly what it says and is a lot easier to remember.



    In defence of lnsflive, they may be a new member but I personally felt they described this particular problem very well:



    I'm sorry DrDos but I personally think you should think about things a bit more before firing off the replies. You've gone too far suggesting that a new member didn't describe the problem very well. Do you think that is fair when you've made the blunder?

    If you don't understand someones problem there is no obligation to reply to them. I don't reply to every topic, just the ones where I can see that I can genuinely help. Likewise if I do reply and get it wrong I can admit it - you'll see quite a bit of banter between me and Fou-Lu in that respect.
    Things are not as simple and clear cut as you believe. Sure, my site opened about a year ago, but I started coding it on my localhost machine, a year or two before that. As another addict of the 'I can haz Cheezburger site, yeah, you've seen some of my lols, I noticed how limited their builder was, and having Imagemagick on my machine, and knowing how to use it,
    I thought I could do much better. So I installed Apache and php, and a few more items, and started writing.

    It was at this time I got acquainted with the lame version of file() , and worked around it. Didn't use it. I also checked to see who might be doing the same thing, I really only found a site called picfont, which was also very limited and non-intuitive in the bargain. So I didn't exactly have much competition.

    And I did not try to impugn insfive with my statement, it was just a general remark and you know it's true just as much as I do.
    As for INL causing confusion, it would not for me, or for the application, but I'm only speaking for myself.

    When I started writing it php was version 4. something or other, it is now 5.2.17 on my site host, it is 5.3.25 on my localhost.
    Welcome to http://www.myphotowizard.net

    where you can edit images, make a photo calendar, add text to images, and do much more.


    When you know what you're doing it's called Engineering, when you don't know, it's called Research and Development. And you can always charge more for Research and Development.

  • #13
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,361
    Thanks
    61
    Thanked 530 Times in 517 Posts
    If you've been coding since v4 then you should really have the hang of this php lark including checking the manual regularly so that your advice is up to date
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #14
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    O.o
    In all fairness here:
    IGNORE_NEW_LINES is still IMO wrong. Problem here is that PHP constants are global constants, and IGNORE_NEW_LINES (and many many many other constants in PHP) have no immediate context from name. I'll concede to making it INL, but only if its prefixed with more information such as FILE_INL. Course. . . that can easily be done with define('FILE_INL', IGNORE_NEW_LINES); . They started getting the idea recently though with constants used for filter_var family being a great example. Still a ways to go though as the input is simply INPUT_X, when it'd be better if it were FILTER_INPUT_X since there seems no other use for the INPUT_X groups.

    Fortunately, as of 5.3 this is a moot point. Namespaces + constants or classes with constants eliminate this problem entirely since every library you create can be drawered from everything else. So at least userspace code is readable.

    File flags in PHP were introduced in 5.0 (well, other than the o'l 0/1 used for the include path). But in fairness, 5.0 was officially released almost a decade ago and 4.x was discontinued as of December 31, 2007, so I'm not sure if I can give points for that
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #15
    Senior Coder
    Join Date
    Sep 2010
    Posts
    2,217
    Thanks
    15
    Thanked 253 Times in 253 Posts
    Quote Originally Posted by Fou-Lu View Post
    O.o
    In all fairness here:
    IGNORE_NEW_LINES is still IMO wrong. Problem here is that PHP constants are global constants, and IGNORE_NEW_LINES (and many many many other constants in PHP) have no immediate context from name. I'll concede to making it INL, but only if its prefixed with more information such as FILE_INL. Course. . . that can easily be done with define('FILE_INL', IGNORE_NEW_LINES); . They started getting the idea recently though with constants used for filter_var family being a great example. Still a ways to go though as the input is simply INPUT_X, when it'd be better if it were FILTER_INPUT_X since there seems no other use for the INPUT_X groups.

    Fortunately, as of 5.3 this is a moot point. Namespaces + constants or classes with constants eliminate this problem entirely since every library you create can be drawered from everything else. So at least userspace code is readable.

    File flags in PHP were introduced in 5.0 (well, other than the o'l 0/1 used for the include path). But in fairness, 5.0 was officially released almost a decade ago and 4.x was discontinued as of December 31, 2007, so I'm not sure if I can give points for that
    This is funny, I was just working with str_split to convert an entire text file to an indiced array, and it has the same stupid problem as file() used to with adding new lines. So this is what I ended up with, and it works!

    $key = str_split($keys, FILE_IGNORE_NEW_LINES);

    Cracked me up.

    Another bone I have to pick with them is 'define'. It should just be 'define x = 10;', and not some complicated function.
    Last edited by DrDOS; 07-23-2013 at 11:36 PM.
    Welcome to http://www.myphotowizard.net

    where you can edit images, make a photo calendar, add text to images, and do much more.


    When you know what you're doing it's called Engineering, when you don't know, it's called Research and Development. And you can always charge more for Research and Development.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •