0

Im getting the tittle error when im calling this function.

<script type="text/javascript">     
    function upvote(user, id){
        fazer = <?php echo $doornot ?>;
        if( fazer == 'true'){
            window.location = "http://www.dawnsource.com/forums/register.php";
        }else {
            user = <?php echo $user; ?>;
            id = <?php echo $_GET[id]; ?>;
             $.ajax({
             url: 'scripts/upvote.php',
             type: 'post',
             data: 'user='+user+'&id='+id,
            success: function() 
             {
              alert('success, upvote completed ');
          }, error: function()
          {
              alert('something went wrong, rating failed');
          }
        });
        }

    }
</script>

The error is going on:

user = <?php echo $user; ?>;

But its getting me the right value in the echo. Why is this happening?

3
  • try user = "<?php echo $user; ?>";. note that quotes around php tags Commented Jun 23, 2013 at 21:52
  • refer to @MarcB's answer, some of your javascript variables need quotes around, otherwise you will get a syntax error Commented Jun 23, 2013 at 21:58
  • Dunno if its simple, but can I change the sucess function to reload the div thats calling the javascript? Commented Jun 23, 2013 at 22:49

2 Answers 2

1

You're missing quotes around your php data, so you're generating invalid javascript. Remember... whatever PHP is outputting has to be correct in a javascript context.

<?php
$foo = 'bar';
?>

<script>
baz = <?php echo $foo ?>;
</script>

is going to generate

baz = bar;

and bar will be interpreted as an undefined variable name.

The proper solution is to simply pass everything through as json from PHP:

 bar = <?php echo json_encode($foo); ?>;

which will ALWAYS produce valid javascript, no matter what's in $foo. and produces

baz = "bar";
Sign up to request clarification or add additional context in comments.

Comments

0

true should be boolean if $doornot is. Additionally, in both PHP and JavaScript String quotes are peeled off when they return a value. Your JavaScript sees another variable, not a String. Try:

<script type="text/javascript">     
  function upvote(){
    var fazer = <?php echo $doornot ?>;
    if(fazer == true){
        window.location = 'http://www.dawnsource.com/forums/register.php';
    }
    else {
      var user = '<?php echo $user; ?>';
      var id = '<?php echo $_GET[id]; ?>';
      $.ajax({
        url: 'scripts/upvote.php',
        type: 'post',
        data: 'user='+user+'&id='+id,
        success: function(){
          alert('success, upvote completed');
        },
        error: function(){
          alert('something went wrong, rating failed');
        }
      });
    }
  }
</script>

Side note:

I do not see a use for your user or id arguments in your upvote function, since you are getting those values from PHP, and only under a condition. Also, you should use the keyword var in JavaScript, unless you want those variables to be global.

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.