Jump to content

What Am I Doing Wrong?


dzhax

Recommended Posts

I have these form objects:

<td>	 <b>Attack </b>Level: </td><td>	 <input type="text" name="attack"  value="<?php echo''. $u['Attacklvl'] .'';?>" onchange="findvalues('Attack','EXP')">	 <input type="hidden" name="id" value="echo''. $_GET['id'] .'';">	 <input type="hidden" name="action" value="editusers"></td><td>	 <b>Attack </b>Exp: </td><td>	 <input type="text" name="attackexp" value="<?php echo''. $u['Attackxp'] .'';?>">	 <input type="hidden" name="id" value="<?php echo ''. $_GET['id'] .''; ?>">	 <input type="hidden" name="action" value="editusers"></td>

I am trying to make it when you change the Attack Lvl it automatically changes the Attack Exp.These are the script(s) that I am currently using:

<script>function findvalues(skill,type){if (skill == 'Attack'){	if (type == 'EXP'){		currentLvl = document.getElementsByName('attack');		correctexp = findexp(currentLvl);		document.getElementsByName('attackexp').src = correctexp;	}}function findexp(lvlnow){	switch(lvlnow){	case '1':		  document.write("0"); 	  break;	  case '2':		  document.write("83"); 	  break;	}</script>

By reading the script if you were to type 1 into the attack Lvl box and move to another box it should change the value of Attack EXP to 0. If you change Attack Lvl to 2 then EXP should change to 83.Any help is appreciated. I am really new to javascript and it might be my syntax. Not sure.

Link to comment
Share on other sites

document.getElementsByName does not return the value of the attack field, it returns an array of elements that have the same name. Use document.getElementById to get the one element you're looking for, and you'll be able to get the value from the value property of the element. A text input element does not have a src property. You can use the value property to read and write the value in the field.For findexp, using document.write is just going to write the text to the page. You want to use a return statement instead, not just write it to the page.

Link to comment
Share on other sites

going off what you have said I changed some things. It now reads:

<script>function findvalues(skill,type){if (skill == 'Attack'){	if (type == 'EXP'){		currentLvl = document.getElementsById('attack');		correctexp = findexp(currentLvl);	}return correctexp;}function findexp(lvlnow){	switch(lvlnow){	case '1':		  return "0";	  break;	  case '2':		  return "83";	  break;	}</script>

Still does not work. Am I not supposed to use the onchange="findvalues('Attack','EXP')" ? Or is my code still wrong.Just looking at it I see nothing that will tell it to put it in the attackexp field. How do I do that?EDIT:I added an alert("msg here"); at the beginning of the function and nothing happens. so i think something is wrong and the onchange event is not being triggered.

Link to comment
Share on other sites

In your script, currentLvl is not an integer as findexp() assumes; it is a HTMLElement from which the value needs to be extracted.You can write back to the attackexp field by writing to that field's value property.

Link to comment
Share on other sites

I was reading about javascript and it said java script variables can be string one second and integer another... That is why I figured it would work. When you say writing to the fields value property? How.

Link to comment
Share on other sites

Hi, it seems that the onkeyup would work better for this, give it a try with something like this:<input type="text" name="attack" value="<?php echo''. $u['Attacklvl'] .'';?>" onkeyup="findvalues('Attack','EXP')">the downside is it will be evaluating every key stroke, let's say the user inputs "8", the function is executed, then "85" the function is execute again.maybe a onblur would work also, but i guess its your call.

Link to comment
Share on other sites

I was reading about javascript and it said java script variables can be string one second and integer another
You still have to change the types, it doesn't do that automatically. You're not doing any math operations with the number though so it shouldn't matter if it's a string.
When you say writing to the fields value property?
That's not a question.currentLvl.value = correctexp;Also, it's getElementById, singular. You should be using a Javascript debugger to check for error messages, it doesn't do you much good if you're trying to get a piece of code to work without looking at the errors it reports.
Link to comment
Share on other sites

You still have to change the types, it doesn't do that automatically. You're not doing any math operations with the number though so it shouldn't matter if it's a string.That's not a question.currentLvl.value = correctexp;Also, it's getElementById, singular. You should be using a Javascript debugger to check for error messages, it doesn't do you much good if you're trying to get a piece of code to work without looking at the errors it reports.
Yea forgot a comma... Complex sentences ftw! :)Oh didnt realize the s ill fix that. im gonna guess currentLvl.value would be changed to attackexp.value since that is the id of the textbox im trying to "print" to.If im wrong, then I still see no way that it says it is goin to print to that textbox.Im looking at the code and I dont see any of the variables being used as numbers only string... Who ever mentioned that I was switching between variable types, where?@alexnofue - Thanks for the suggestion. Ill give that a try. I like the change by each keyup. That makes it change a lot so I can tell it works.
Link to comment
Share on other sites

If you do this:currentLvl = document.getElementById('attack');Then the currentLvl variable holds a reference to the attack element. The ID should go in quotes there, if the ID is "attackexp" then change the value in quotes there to get the correct ID. But the element itself is stored in the currentLvl variable, or whatever variable name you use. The current value of the textbox would be currentLvl.value, if you want to change the value you can write to that property.

Link to comment
Share on other sites

that would have to be opposite. At the end of everything happening I need to print correctexp to the textbox with id of 'attackexp'This is really frustrating. I dont even think the function is being called. Ive tried onchange, onkeyup, onafterupdate... Nothing. I added an alert to see if the function ever gets triggered and nothing happens. am I calling it wrong. Ill repost what I am using.

<td>	 <b>		  Attack 	 </b>	 Level: </td><td>	 <input id="attack" type="text" name="attack"  value="<?php echo''. $u['Attacklvl'] .'';?>" onafterupdate="findvalues('Attack','EXP')">	 <input type="hidden" name="id" value="echo''. $_GET['id'] .'';">	 <input type="hidden" name="action" value="editusers"></td><td>	 <b>		  Attack 	 </b>	 Exp: </td><td>	 <input id="attackexp" type="text" name="attackexp" value="<?php echo''. $u['Attackxp'] .'';?>">	 <input type="hidden" name="id" value="<?php echo ''. $_GET['id'] .''; ?>">	 <input type="hidden" name="action" value="editusers"></td>

Now the current java script:

<script>function findvalues(skill,type){alert('Function Called!');if (skill == 'Attack'){	if (type == 'EXP'){		currentLvl = document.getElementById('attack');		correctexp = findexp(currentLvl);	}	document.getElementById('attackexp').insertAdjacentText(beforeEnd,correctexp);}function findexp(lvlnow){	switch(lvlnow){	case '1':		  return "0";	  break;	  case '2':		  return "83";	  break;	}</script>

The javascript is no longer an include. Figured that might be it, but no dosent change anything. Its in the head tags towards the top of the page.

Link to comment
Share on other sites

Look for Javascript error messages. If you aren't using Firebug, you should be.

At the end of everything happening I need to print correctexp to the textbox with id of 'attackexp'
document.getElementById('attackexp').value = xxx
Link to comment
Share on other sites

Sweet got it to work. Missing a closing } at the end of the last function. The only thing is it is returning undefined not the number. But default works... EDIT: Now it only calls default...FIXED IT!!!!! Ill post the fixed code below:

<script>function findvalues(){	currentLvl = document.getElementById('attack');	document.getElementById('attackexp').value = findexp(currentLvl);return findexp(currentLvl);}function findexp(lvlnow){	if (lvlnow.value == '1'){		  return '0';	} else {		 if (lvlnow.value == '2'){			  return '83';		 } else {			  return 'not yet supported';		 }	 }}</script>

<td>	 <b>		  Attack	 </b>	 Level:</td><td>	 <input id="attack" type="text" name="attack"  value="<?php echo''. $u['Attacklvl'] .'';?>" onkeyup="findvalues()">	 <input type="hidden" name="id" value="echo''. $_GET['id'] .'';">	 <input type="hidden" name="action" value="editusers"></td><td>	 <b>		  Attack	 </b>	 Exp:</td><td>	 <input id="attackexp" type="text" name="attackexp" value="<?php echo''. $u['Attackxp'] .'';?>">	 <input type="hidden" name="id" value="<?php echo ''. $_GET['id'] .''; ?>">	 <input type="hidden" name="action" value="editusers"></td>

Link to comment
Share on other sites

I couldnt get the findexp switch structure to go so I had to use if statements... It was almost like switch(lvlnow.value) is invalid because that dont work always reverts to default.

Link to comment
Share on other sites

code this is what i was using:

function findexp(lvlnow){	switch(lvlnow.value){		case 1:			return 0;		break;		case 2:			return 83;		break;		case 3:			return 174;		break;		case 4:			return 276;		break;		default:			return 'undefined';		break;	}}

I also tried:

function findexp(lvlnow){	switch(lvlnow.value){		case '1':			return '0';		break;		case '2':			return '83';		break;		case '3':			return '174';		break;		case '4':			return '276';		break;		default:			return 'undefined';		break;	}}

Link to comment
Share on other sites

It doesn't really matter if the numbers you return are in quotes or not, but it would be best to explicitly convert to a number.

function findexp(lvlnow){	val = parseInt(lvlnow.value, 10);	if (isNaN(val))	  return 'undefined';	switch(val){		case 1:			return 0;		break;		case 2:			return 83;		break;		case 3:			return 174;		break;		case 4:			return 276;		break;		default:			return 'undefined';		break;	}}

Link to comment
Share on other sites

There are other ways to do that too. You can also use an array:

function findexp(lvlnow){	val = parseInt(lvlnow.value, 10);	if (isNaN(val))	  return 'undefined';	var exp = [0, 0, 83, 174, 276];	if ((typeof exp[val]) != 'undefined')	  return exp[val];	return 'undefined';}

Link to comment
Share on other sites

justsomeguy, you rock!Thank you for hanging in there. Now that I know I can get it to work, im databasing the values.I got the finding exp value of the level already in now I am working on making it determine what lvl according on what is typed in the exp field.Got the hard portion of it done but making it load from database is proving to be quite a challenge.Made a thread about it in php section... as im typin this im thinkin i should of put it in javascript. ugh... might be reposting.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...