0

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.

7
  • 3
    The code is correct, the problem must lie somewhere else than in the code. Commented Feb 1, 2011 at 8:22
  • js is already enabled. It must be somewhere else then. Not been able to figure out where for hours.. Commented Feb 1, 2011 at 8:25
  • you can check that it works by yourself: take the code above, put it in a blank "test.html" file and open it with a browser. The alerts must show up. Commented Feb 1, 2011 at 8:26
  • @ptamzz - Resource>XHR ? Do you mean to say you are using ajax? Commented Feb 1, 2011 at 8:30
  • 1
    If you have any error prior to this code is executed, there will be no alert. What does the error console say? Commented Feb 1, 2011 at 9:15

3 Answers 3

1

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...
   }
});
Sign up to request clarification or add additional context in comments.

1 Comment

You are great man.. thanks.. been struggling for the whole day with this. :) Working now.
1

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.

Comments

0

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>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.