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 8 of 8
  1. #1
    New to the CF scene
    Join Date
    Jun 2010
    Location
    UTAH
    Posts
    5
    Thanks
    3
    Thanked 0 Times in 0 Posts

    document.getelementsbyname() for PHP Array.

    I am trying to pass two inputs one hidden one text to php using document.getelementsbyname().

    The hidden input is in a php array, the text input is a user typed input. When ever I pass it to php it always pulls the first item in the array. So even if I click "View" on the 3rd item in my array it will still show the two inputs from my first item in the array. Here are my html input fields followed by java function followed by submit. How do i get it to send the proper information and not the first item in the array.

    Code:
    <input name="imageNumber" type="hidden" value="<? echo $imageNumber; ?>.png">
    <input type="text" name="imprint" value="" size="40">
    
    <script>
              function passImprint() {
    imageNumber = (document.getElementsByName("imageNumber")[0].value);
    imprint = (document.getElementsByName("imprint")[0].value);	
    urlString = "imprint.php?imprint=" +imprint + "&imageNumber=" +imageNumber;				window.open(urlString,"width=640,height=295");
    									}			
    					</script>
    
    
    <input type="button" value="View Imprint" onClick="passImprint();">

  • #2
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,394
    Thanks
    13
    Thanked 353 Times in 349 Posts
    Quote Originally Posted by ringer View Post
    The hidden input is in a php array,
    I only see a PHP variable, that might be of any type.

    Quote Originally Posted by ringer View Post
    When ever I pass it to php it always pulls the first item in the array.
    where do you pass it to PHP? I neither see a submit button, a submit() method, a <form> element, nor an AJAX call.

    Quote Originally Posted by ringer View Post
    So even if I click "View" on the 3rd item in my array it will still show the two inputs from my first item in the array.
    But that’s what you tell the JavaScript* function to do, alert the first named element’s values.

    Quote Originally Posted by ringer View Post
    Here are my html input fields followed by java function followed by submit.
    that’s just a button, it doesn’t submit anything.

    Quote Originally Posted by ringer View Post
    How do i get it to send the proper information and not the first item in the array.
    Info: document.getElementsByName() doesn’t return an Array, but a NodeList/HTMLCollection. a subtle but important difference.



    * - JavaScript != Java
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • Users who have thanked Dormilich for this post:

    ringer (06-22-2010)

  • #3
    New to the CF scene
    Join Date
    Jun 2010
    Location
    UTAH
    Posts
    5
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Thanks for your reply

    I'm obviously new to programming, sorry if my explanation didn't quite do it. I'm still learning whats obvious and whats not. What I can omit when I explain and details needed. i'll post full code from php array to submit maybe that will help. Two things in reply to your post

    Quote:
    Dormilich: that’s just a button, it doesn’t submit anything.

    Well the submit calls the function passImprint() in that function
    urlString = "imprint.php?imprint=" +imprint + "&imageNumber=" +imageNumber; sends it to imprint.php
    that passes imprint and imageNumber to imprint.php.

    Quote:
    Dormilich: But that’s what you tell the JavaScript* function to do, alert the first named element’s values.

    K so this is the root of my problem. I believe the [0] in my function is telling it to pass the first imageNumber in my array, and i'm trying to figure out how to pass the imageNumber in the array that i'm clicking view on.

    My next post i'll post more code. thanks again for your reply

  • #4
    New to the CF scene
    Join Date
    Jun 2010
    Location
    UTAH
    Posts
    5
    Thanks
    3
    Thanked 0 Times in 0 Posts
    [code]
    <?php
    while(odbc_fetch_array($qry)){
    $id = odbc_result($qry, "ID");
    $tagline = odbc_result($qry, "Campaign");
    $imageNumber = odbc_result($qry, "imageNumber");
    ?>


    <table width="370" cellpadding="2" cellspacing="0" border="0">
    <img src="Images/line.jpg">
    <TR>
    <TD>
    <INPUT TYPE=CHECKBOX NAME="order<?php echo $imageNumber ?>"><? echo "<img width='249' height='115' src='$imageNumber.png'>"; ?><br>
    </TD>
    </TR>
    <TR>
    <TD>
    <? echo $tagline; ?><br>
    </TD>
    </TR>
    </table>

    <br />
    <table width="370" cellpadding="2" cellspacing="0" border="0">
    <TR>
    <TD>
    QTY:
    </TD>
    <TD>
    <input name="qty" type="text" size="5">
    </TD>
    </TR>
    <TR>
    <TD>
    Imprint:

    </TD>

    <TD>
    <input name="imageNumber" type="hidden" value="<? echo $imageNumber; ?>.png">
    <input type="text" name="imprint" value="" size="40">

    </TD>
    </TR>
    <TR>
    <TD></TD>
    <TD>

    <script>
    function passImprint() {
    imageNumber = (document.getElementsByName("imageNumber")[0].value);
    imprint = (document.getElementsByName("imprint")[0].value);
    urlString = "imprint.php?imprint=" +imprint + "&imageNumber=" +imageNumber;
    window.open(urlString,"width=640,height=295");
    }
    </script>

    <input type="button" value="View Imprint" onClick="passImprint();">




    <br /><br />

    </TD>
    </TR>
    <TR>
    <TD>
    Material:
    </TD>
    <TD>
    <input type="radio" value="wsp" name="paperORplastic">Wet Strength Paper<br/>
    <input type="radio" value="pe" name="paperORplastic">Polyethylene (PE)
    </TD>
    </TR>
    <TR>
    <TD></TD>
    <TD>
    (Only use (PE) if Outoor Company Requires it.)
    </TD>
    </TR>
    </table>
    <br />
    <?php
    }
    ?>
    [code]

  • #5
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,394
    Thanks
    13
    Thanked 353 Times in 349 Posts
    ah, that makes more sense now.

    a clarification between button and submit: a button (with the "button" type) does nothing in HTML, it just changes its appearance when being clicked. contrary, a submit button (with the "submit" type) triggers the form to be sent (which in your case you seem to have not defined).

    by attaching an "onclick" attribute to any (!) HTML element, you provide a hook for JavaScript to do something. this is of course also true for standard and submit buttons, but doesn’t change the intention behind these elements.

    currently the best/easiest would be assigning each input field (of that you want the value) an unique ID (violating this will get you in more trouble), which you could pass as input parameter to passImprint(). (there are certainly more elegant solutions possible, but not with the current HTML)
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • Users who have thanked Dormilich for this post:

    ringer (06-22-2010)

  • #6
    New to the CF scene
    Join Date
    Jun 2010
    Location
    UTAH
    Posts
    5
    Thanks
    3
    Thanked 0 Times in 0 Posts
    OK Dormilich thanks for your posts i'm starting to understand why it's not working and even direction for what i need to do, but my java script sucks bad. Below is what I tried to assign the two input fields unique id's and then use those in the function. I took a guess at it and no luck so heres what I came up with. If you could point out what i did wrong that would be great. Thanks again for all your help.

    K so i just posted the changes i made to my above code. for the id's I added "id$imageNumber and imprint$imageNumber. That way each input will have unique id's attached to them as you suggested.
    Code:
    <input name="imageNumber" id="id<?echo $imageNumber ?>" type="hidden" value="<? echo $imageNumber; ?>.png">
    <input type="text" name="imprint" id="imprint<?echo $imageNumber ?>" value="" size="40">
    This is where i really don't get it. I tried adding id in the function, adding passImprint(id), and +id next to my document.getelementsbyname I know i am way off, and I cant even explain why i think this would work but it's all i got. Any direction would be appreciated. And try not to pee yourself while laughing at my feeble attempt LOL. Thanks again.

    Code:
    		
    <script>
    function passImprint(id) {
    imageNumber = (document.getElementsByName("imageNumber"+id).value);
    imprint = (document.getElementsByName("imprint"+id).value);	
    urlString = "imprint.php?imprint=" +imprint + "&imageNumber=" +imageNumber;
    window.open(urlString,"width=640,height=295");
    									}			
    </script>

  • #7
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,394
    Thanks
    13
    Thanked 353 Times in 349 Posts
    how do you define the button?

    if you want to access elements via ID, you need the document.getElementById() function (attention, that returns a single element or null)
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • Users who have thanked Dormilich for this post:

    ringer (06-22-2010)

  • #8
    New to the CF scene
    Join Date
    Jun 2010
    Location
    UTAH
    Posts
    5
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Thanks for your help Dormilich


  •  

    Posting Permissions

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