-2

I get this weird error. It happens to be my PHP code that is embedded into jquery.

<?php $user=isset($_GET['u']) ? $_GET['u'] : NULL; //This returns a numeric value ?> 

<script>
  $("button[name='send_mail']").on('click',function(){
  var user = '<?php echo $user;?>'; // <----The Problem
  $.ajax({
    type:"POST",
    url:"ajax/scripts/user_mail.php?u="+user,
    success: function(data){
        alert(data);
        },
    });
  });
</script>

I tried both, single and double quotes. This piece code happens to work in some of my files and doesn't work in others.

Ikhlak S.
  • 8,578
  • 10
  • 57
  • 77

3 Answers3

-1
<?php
    $user = (!empty($_GET['u']))?intval($_GET['u']):0;
?>
<script language="JavaScript">
    console.log("User: " + <?php echo $user; ?>);
    // try to debug what is really in the variable
    var user = <?php echo $user; ?>;
    // same as <?= $user; ?>
</script>
Jan
  • 42,290
  • 8
  • 54
  • 79
-1

You have a rogue comma in this part of your code:

success: function(data){
    alert(data);
    }, //<--- here
});

It should be only:

success: function(data){
    alert(data);
    }
});
Shadow The GPT Wizard
  • 66,030
  • 26
  • 140
  • 208
-1

This works:

<?php $user=isset($_GET['u']) ? $_GET['u'] : NULL;?>
<span id="user_id_span" style="display:none;"><?php echo $user; ?></span>

<script> 
  var user = $('#user_id_span').text();
</script>

If your output is more than just a single string or number or single line, this would not work.

Ikhlak S.
  • 8,578
  • 10
  • 57
  • 77