Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

should be simple but im having trouble

Status
Not open for further replies.

dariuc

Programmer
Sep 14, 2009
6
US
I've been staring at this same problem for over a week now. I've worked around it as best as i can but i think it's time to ask someone else for input.

I'm trying to pass a value thru an ajax parameter.. that's all. it SHOULD be easy in theory.

what i want to do is create a jscript variable then pass that variable as the value for a parameter.


php then converts that value to something it can use to finish the rest of my code.

As i said it's hindering my webpage progress and i would like to get it fixxed soon so any help would be appreciated.

Code:
function getSelection()
{
var selection=document.getElementsById("SelectedItem");

var xmlhttp;//create a var for the obj
if (window.XMLHttpRequest)//if requesting an obj...
{
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else if (window.ActiveXObject)
{
// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
else
{
alert("Your browser does not support XMLHTTP!");
}
xmlhttp.onreadystatechange=function()
{
if(xmlhttp.readyState==4)
{
var text = xmlhttp.responseText;
addNode(text);
}
}
xmlhttp.open("GET","battle_nin.php?selection",true);
xmlhttp.send(null);


}
//the php snippet
$currentjutsu=$_GET[selection];
 
To start with, 'getElementsById' is invalid. Given IDs are unique, you should drop the 's' to make it a singular reference: 'getElementById'.

Did you not see a JS error telling you that 'getElementsById' was undefined? Did you even check the error console?

Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page [blue]@[/blue] Code Couch:
Code Couch Tech Snippets & Info:
 
no errors popped up simply because i am using ajax. the page is a combination of php making it necessary to use ajax.

i've been working on it since then. i noticed the error you mentioned and fixed it. however because the element i'm drawing from is a select list that is populated by database data im not sure how to write it.

i also noticed that the parameter i tried to send was wrong. i since corrected it but it's also not working
 
So when you use AJAX, no errors are shown. Curious.

If you put battle_nin.php?selection in your browser it will probably fail because the hostname is not found. Put the complete URL as a starting point

Cheers,
Dian
 
Dan is correct about the getElementById syntax.

Also, this line:
xmlhttp.open("GET","battle_nin.php?selection",true);

If the parameter you were trying to send was a cartoon character named "MagillaGorilla" based on the selection made, that would end up creating an url similar to battle_nin.php?MagillaGorilla. I'm an asp guy, but I'm pretty sure that would equate to nothing in php as well.

Try
Code:
xmlhttp.open('GET','battle_nin.php?character=' + selection +'&ms='+ new Date().getTime(), true);
then handle the querystring parameter, "character" in your php code.

Dian also offers good advice. Get your php code working by itself, then create the javascript Ajax call to send the right url.

Good luck!

.mark.
 
i appreciate the replies. i did make sure all the code works before i posted here.

ALL of the code works except for the part that passes the information to the PHP file, which is why i posted. i even made sure by giving the page a value within my database.

i believe aside from those problems that you mentioned... that values containing an apostrophe are to blame too
 
Code:
function jutsu()
{
var selection=document.getElementById("SelectedJutsu").options[document.getElementById("SelectedJutsu").selectedIndex].value;
var xmlhttp;//create a  var for the obj
if (window.XMLHttpRequest)//if requesting an obj...
  {
  // code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else if (window.ActiveXObject)
  {
  // code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
else
  {
  alert("Your browser does not support XMLHTTP!");
  }
xmlhttp.onreadystatechange=function()
{
if(xmlhttp.readyState==4)
  {
      var text = xmlhttp.responseText;
      addNode(text);

  }
}
xmlhttp.open("GET","battle_nin.php?sel="+ selection,true);
xmlhttp.send(null);


}
//php snippet
$currentjutsu=$_GET[sel];
 
sorry i meant to edit the previous post before posting it.

that is the code as it stands now. i did more spot troubleshooting and traced it back to the line that sends the url. the url is correct because it's on a localhost.
 
Code:
xmlhttp.open("GET","[URL unfurl="true"]http://localhost/battle_nin.php?sel="+[/URL] selection,true);


Cheers,
Dian
 
i narrowed the problem down to two things.. out of six.

i have code to create a select list.

this select list is populated by db query

i have two pieces of code i need looked at to fix the problem

Code:
echo "<select name='LearnedJutsus' id='YourJutsu' onchange='return jutsu()' >";

	foreach($learnedJutsuInfo as $v)
	{

		echo "<option  value='$v'>$v</option>";


	}
	echo "</select></form>";


and
most importantly this one!

Code:
var selection=document.getElementById("x").options[document.getElementById("x").selectedIndex].value;

i did not write the above code it was donated to me but it seems to be not working correctly.

what i need is an explanation on how it works (which would be best since im foggy with jscript)or a fix for it

i basically need it to grab the selected option's information for later use in the program. this is a pivotal part of the system

any help appreciated.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top