ptamzz
ptamzz

Reputation: 9355

Debugging javascript code

My script part in my php page is like this

<script type="text/javascript" >
    alert('Out');
    if (<?php echo $delFlag; ?> == 1) {
        alert('ting');
    } 
</script>

While the code below is what I get from the Chrome Developers tool (Resource>XHR) (which I'm using for debugging)

<!-- For javascripts -->
<script type="text/javascript" >
alert('Out');
    if (1 == 1) {
        alert('ting');
    }
</script>

The alerts are not popping up.

What is wrong with my codes??

Update

I've my codes in a file add-line.php

<script type="text/javascript" >
//  $(function() {

        if (<?php echo $timer; ?> == 1) {
            //alert('Line');

            var timenow = <?php echo time(); ?>;
            var dataPass = 'lineId=' + <?php echo $lineId; ?> + '&storyId=' + <?php echo $storyId; ?> + '&timenow=' + timenow;
            $.ajax({
                type: "POST",
                url: "proc/updateDb.php",
                data: dataPass,
                cache: false,
                success: function(){
                    // Show error if error is there

                }
            }); 
        } 
//  });
</script>

Now in the updateDb.php I m processing some CRUD thing using the POST values and then determine $delFlag value. Then at the end of this file, I've my scripts as (same as the first one above)

<script type="text/javascript" >
        alert('Out');
        if (<?php echo $delFlag; ?> == 1) {
            alert('ting');
        } 
    </script>

But this script is not being executed at all it seems.

Upvotes: 0

Views: 139

Answers (3)

Reigel Gallarde
Reigel Gallarde

Reputation: 65254

As I notice you are using ajax, you that script would run if you add it to the DOM.

for example, if you use $.ajax()

$.ajax({
   url: 'some/url/',
   type: 'html',
   method: 'get',
   success: function(data){
      $('head').append(data);
      // data here would be the response from the server...
   }
});

Upvotes: 1

Pyae Phyoe Shein
Pyae Phyoe Shein

Reputation: 13787

Try like that. Error message will prompt and try to fix it.

<script type="text/javascript">
    alert('Out');
    try
      {
        if (<?php echo $delFlag; ?> == 1) {
            alert('ting');
        } 
      }
    catch(err)
      {
      alert(err.description);
      }
</script>

Upvotes: 0

Yoram de Langen
Yoram de Langen

Reputation: 5499

You tried this:

<!-- For javascripts -->
<script type="text/javascript" >
    var number = parseInt("<?php echo $delFlag; ?>");
    alert('Out');
    if (number == 1) {
        alert('ting');
    }
</script>

You can do it without the quotes if you want.. but if you using some editor they whill markit as a error.

Upvotes: 1

Related Questions