...

View Full Version : document.getelementsbyname() for PHP Array.



ringer
06-21-2010, 06:49 PM
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.




<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();">

Dormilich
06-21-2010, 07:03 PM
The hidden input is in a php array,

I only see a PHP variable, that might be of any type.


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.


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.


Here are my html input fields followed by java function followed by submit.

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


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

ringer
06-21-2010, 07:18 PM
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

ringer
06-21-2010, 07:23 PM
[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]

Dormilich
06-21-2010, 11:43 PM
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)

ringer
06-22-2010, 05:15 PM
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.


<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.



<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>

Dormilich
06-22-2010, 08:59 PM
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)

ringer
06-22-2010, 11:47 PM
Thanks for your help Dormilich



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum