Jump to content

Problem In Retrieving A Value With Getelementbyid


osprey
 Share

Recommended Posts

Hi there,i am writing a script to handle a little photo gallery generated by php.My aims is passing to the php script the codex that identify the image in vision in a certain moment. In this way php can generate the next or the previous image according to the user's choice.I store the numeric codex of the image in a "hidden" input field and I send it via GET to php script using this code:myTextField = parseInt(document.getElementById("nascosto").value );The problem is that on the first click the page show "undefined" instead of the value of the codex! I need a second click to reach the goal.Thanx in advance for any help.The following is the html page, I've just omitted the php script that generates the code of the image and I've inserted into the hidden field a random codex.I put an alert into the AJAX function that handles the gallery, there the codex is retrieved with any trouble.<html><body><script type="text/javascript">var myTextField; //it stores the actual image codexfunction showImage(img_param){xmlhttp=GetXmlHttpObject(); if (xmlhttp==null) { alert ("Il tuo browser non è compatibile con AJAX!"); return; }var url="nextImg.php";url=url+"?num="+img_param;url=url+"&sid="+Math.random();xmlhttp.onreadystatechange=stateChanged;xmlhttp.open("GET",url,true);xmlhttp.send(null);myTextField = parseInt(document.getElementById("nascosto").value );alert("You entered: " + myTextField)}function stateChanged(){ if (xmlhttp.readyState==4) { document.getElementById("nuovocodice").innerHTML=x mlhttp.responseText; }}function GetXmlHttpObject(){ if (window.XMLHttpRequest) { // code for IE7+, Firefox, Chrome, Opera, Safari return new XMLHttpRequest(); } if (window.ActiveXObject) { // code for IE6, IE5 return new ActiveXObject("Microsoft.XMLHTTP"); }return null;}</script><input type="hidden" id="nascosto" value="123456"><p id="nuovocodice">New image here</p><table><tr><td>prev</td><td><div onClick="showImage(myTextField)"><a href=#>next</a></div></td></tr></table></body></html>the simple testing script of the nextImg.php page is<?php$num=$_GET['num'];echo $num;?>

Edited by osprey
Link to comment
Share on other sites

If you're setting the value of the input with AJAX (I don't see it in your code, but it wouds like that's what's happening) , it would have to wait until the AJAX request is complete before having a value.

Link to comment
Share on other sites

onclick runs the function showImage(myTextField) problem is! the value 'myTextField' is within the function itself (in bold). img_param is underfined because this value has not been set yet, because it is five lines below it.it will show on the second click, obviously! because the mytextfield has now been set.place:myTextField = parseInt(document.getElementById("nascosto").value ); above:url=url+"?num="+img_param; and change 'img_param' to 'myTextField'url=url+"?num="+myTextField;showImage(myTextField) 'myTextField' is not required, therefor either is 'img_param' in function showImage(img_param)url=url+"?num="+img_param;url=url+"&sid="+Math.random();xmlhttp.onreadystatechange=stateChanged;xmlhttp.open("GET",url,true);xmlhttp.send(null);myTextField = parseInt(document.getElementById("nascosto").value );

Edited by dsonesuk
Link to comment
Share on other sites

Thank you very much!!!Everything is working now!

onclick runs the function showImage(myTextField) problem is! the value 'myTextField' is within the function itself (in bold). img_param is underfined because this value has not been set yet, because it is five lines below it.it will show on the second click, obviously! because the mytextfield has now been set.place:myTextField = parseInt(document.getElementById("nascosto").value ); above:url=url+"?num="+img_param; and change 'img_param' to 'myTextField'url=url+"?num="+myTextField;showImage(myTextField) 'myTextField' is not required, therefor either is 'img_param' in function showImage(img_param)url=url+"?num="+img_param;url=url+"&sid="+Math.random();xmlhttp.onreadystatechange=stateChanged;xmlhttp.open("GET",url,true);xmlhttp.send(null);myTextField = parseInt(document.getElementById("nascosto").value );
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...