#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Location
    New York
    Posts
    4
    Rep Power
    0

    how to get a single value out of an array in Java Script/PHP


    Hi. I hope somebody can help me with this. Even though it seems simple I cannot figure it out.

    Below is my php script. It creates a form to allow a 10 file upload. Does anyone know the statement to get the first entry out of the array 'imagefile' using javascript?

    for instance, if the first file is "picture.jpg" how do i extract that filename from the first position in the array?

    I need to do it in javascript and not php.

    Any help would be appreciated.


    $uploadcount=10;
    echo "<TABLE ALIGN=CENTER><TR><TD>";
    echo "<FORM ENCTYPE=\"multipart/form-data\" METHOD=\"post\" ACTION=\"$scriptname\">";
    echo "<INPUT TYPE=hidden NAME=MAX_FILE_SIZE value=5000000>";
    echo "<INPUT TYPE=hidden NAME=upload value=true>";
    for ($i=1; $i<=$uploadcount; $i++) {
    echo " $i. <INPUT NAME=imagefile[] TYPE=file size=30><BR>";
    }
    echo "<INPUT TYPE=submit NAME=save VALUE=\"Upload these files\">";

    echo "</FORM>";
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Jan 2003
    Location
    No es importante
    Posts
    2,065
    Rep Power
    14
    Look at the php functions array_* at
    http://us3.php.net/array
    and the javascript `shift()` fucntion.
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Location
    New York
    Posts
    4
    Rep Power
    0
    Thanks for the tip, but the shift() function seems to be a php function and I must code this with javascript.

    Also, I want to extract the filename from any position in the array.

    for example,
    if i want the first filename --> get from array position one
    if i want the second filename --> get from array position two
    if i want the nineth filename --> get from array position nineth
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Jan 2003
    Location
    No es importante
    Posts
    2,065
    Rep Power
    14
    Shift() is a javascript function.
    http://javascriptkit.com/javatutors/oopjs2.shtml
  8. #5
  9. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Location
    New York
    Posts
    4
    Rep Power
    0
    Oh, ok. I'll play around with that.

    Below is the complete code that I'm working with. Maybe you could tell me what's wrong just by looking at it. The code allows a 10 picture upload. The reason I need to get the names of the files from the array is so that I can get it to preview the image.

    <html>

    <head>
    <title></title>
    </head>
    <body>

    <script language="javascript">
    function previewimg(imagenum){
    document.getElementById(imagenum).src=document.forms.myform.imagefile[imagenum];}
    </script>


    <TABLE ALIGN=CENTER><TR><TD>
    <FORM NAME="myform" ENCTYPE="multipart/form-data" METHOD="post" ACTION="process.php">
    <INPUT TYPE=hidden NAME=MAX_FILE_SIZE value=5000000>
    <INPUT TYPE=hidden NAME=upload value=true>
    <DIV ALIGN=RIGHT>

    <script language="javascript">
    for (i=1; i<=10; i++) {
    document.write('<img id='+i+' width=100 height=100>');
    document.write(i + '. <INPUT NAME=imagefile['+i+'] TYPE=file size=30 onChange="previewimg(i)">');
    document.write('<BR>');
    }
    </script>
    </DIV><DIV ALIGN=CENTER>
    <BR>
    <img id="processing" src="uploading.gif" style="visibility:hidden" width="234" height="41"><br>
    <INPUT TYPE=submit NAME=save VALUE="Send these pictures" onClick="document.getElementById('processing').style.visibility='visible'">
    </DIV>
    </FORM>

    </body>
  10. #6
  11. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Location
    New York
    Posts
    4
    Rep Power
    0
    Thanks for your help. I got it to work!

IMN logo majestic logo threadwatch logo seochat tools logo