-1
<?php $genderdb = "Male"?>
<script type="text/javascript">
        var genderdb = "<?php echo $genderdb; ?>";
        if ((genderdb == "Female") || ($genderdb == "Shemale")) {
            var himher = "her";
        } else {
            var himher = "him";
        }
        alert (himher);
</script>

If I change php $genderdb = "Female", then it can alert the value successful. But if $genderdb = "Male", the page won't have alert. Why? Where is the error?

6
  • 4
    Is that $ supposed to be there in the JavaScript if? Commented Jun 17, 2011 at 23:20
  • 1
    @CD Sanchez: That's an answer. Post it, and I'll vote it up. Good eye. @zac1987: JavaScript expressions are short-circuited. When genderdb == "Female", the if condition is true and so the second half doesn't have to be evaluated at all. If genderdb != "Female", the second half of that condition has to be evaluated, resulting in an undefined symbol error. You should be seeing it in the JavaScript console of whatever browser you're using. Commented Jun 17, 2011 at 23:21
  • For reference, you can use <?=$genderdb?> instead of <?php echo $genderdb; ?> Commented Jun 17, 2011 at 23:22
  • @Jamie: (a) only if you have short_tags enabled; (b) please do not. Commented Jun 17, 2011 at 23:22
  • @Jamie: Only if the shorttags option is turned on, which it isn't necessarily. Commented Jun 17, 2011 at 23:23

3 Answers 3

4
if ((genderdb == "Female") || ($genderdb == "Shemale")) {

should be

if ((genderdb == "Female") || (genderdb == "Shemale")) {

It works when genderdb is "Female", because then the (broken) second comparison doesn't come into the equation.

In addition, using var inside the conditional blocks is suspect. Javascript actually doesn't have block scope so it works, but it's something to consider.

Sign up to request clarification or add additional context in comments.

3 Comments

Re var: Yes, it's specifically allowed by the spec to repeat a var within an execution context (logic flow has no effect whatsoever on var, it's evaluated upon entry to a context), but it's not a good idea.
what is blocks? Do you mean I must not use var? If so, what should I use?
@zac1987: Since it's not a problematic issue here, and quite complex to explain in a comment, I think I'll actually leave the topic for another day.
2

You have an error in this line:

if ((genderdb == "Female") || ($genderdb == "Shemale")) {

Namely, the second reference to genderdb uses the name of the PHP variable, instead of the JavaScript variable.

Comments

0

Remove the $ from your 'shemale' comparison.

That's causing your script to fail as it's looking for a non-existent variable.

        var genderdb = "<?php echo $genderdb; ?>";
        if ((genderdb == "Female") || (genderdb == "Shemale")) {
            var himher = "her";
        } else {
            var himher = "him";
        }
        alert (himher);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.