Jump to content

[SOLVED] If statements issue


Eggzorcist

Recommended Posts

I'm sorry it's very basic it's just its for a school project and I can't seem to get it to work.

 

here my code

 

function showUser(str)
{


if (str==5){

string = "Name: Harold<br>Location: Montreal, Canada"

document.getElementById('txtHint').innerHTML = string;

}



else {

alert("Wrong ID, Please try again");
}

}

 

I can't seem to find the issue.

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/162322-solved-if-statements-issue/
Share on other sites

I'm sorry it's very basic it's just its for a school project and I can't seem to get it to work.

 

here my code

 

function showUser(str)
{


if (str==5){

string = "Name: Harold<br>Location: Montreal, Canada"

document.getElementById('txtHint').innerHTML = string;

}



else {

alert("Wrong ID, Please try again");
}

}

 

I can't seem to find the issue.

 

Thanks

 

Let me guess... your 'str' value comes from some form field or query string, right?  If so, it's being treated as a string and not a number.  To fix it, run it through the parseInt() function:

 

function showUser(str)
{
   str = parseInt(str);

   if(str == 5)
   {
      string = "Name: Harold<br>Location: Montreal, Canada";
      document.getElementById('txtHint').innerHTML = string;
   }
   else
   {
      alert("Wrong ID, Please try again");
   }
}

I've added that however, the command still gets passed to the else command.

 

I know this isn't the problem but here is how the information is showing.

 

 

 <form>
Package ID:<input name="id" id='id' type="text" size="10" maxlength="10" /> 
<br />
<input type='button' onclick='showUser(this.value)' value="Track"/>
</form>

Archived

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

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.