tags:

views:

107

answers:

5

Hi

I have a problem in my code.

can anybody help me ...

<html>
    <body>
    <script type="text/javascript">
        <?php
        $conn = mysql_connect("localhost","root");
        mysql_select_db("tr", $conn); 
        $q = mysql_query("SELECT message FROM messages WHERE to_viewed = '0' ");

        if (mysql_num_rows($q)) {
            require('pm.php');
            ?>
            var answer = confirm("you have new message ");
            <?php
        }
        ?>
        if (answer) {
            window.location = "http:>>>/";
        } else {
        }

    //-->
    </script>
</body>
</html>

Thanks ....

The error that appears in my code is that the confirm is not working if I open my page (no output)

A: 

For one thing, window.location = "http:>>>/"; will cause this message to appear: This URL does not have any code saved to it

zincorp
A: 

You didnt say anything about the problem you were having but if to_viewed is an integer field, youll need to take the quotes out from the query

SELECT message FROM messages WHERE to_viewed = 0
Galen
I think no error in this line.
A: 

If you are getting errors client-side about answer not being defined you should either assign it the value false when there are no new messages (var answer = false;) or get rid of it altogether (since it's not necessary).

<html>

    <body>


        <script type="text/javascript">


<?php

$conn = mysql_connect("localhost","root");
mysql_select_db("tr", $conn); 
$q = mysql_query("SELECT message FROM messages WHERE to_viewed = '0' ");

    if (mysql_num_rows($q))
{
  require('pm.php');

    ?>

    if (confirm("you have new message ")){

        window.location = "http://mywebsite.com/";
    }
    else{

    }

<?php

}
    ?>

//-->
</script>



</body>
</html>
icio
I tried code that you put it but did not work
+1  A: 

Considering you're only showing the confirm if there are rows returned, "answer" should be in this area as well rather than outside of it.

Are you sure rows are being returned? Do a quick echo... echo @mysql_num_rows($q); before the if() to be sure something is even being returned.

<html>
    <body>
    <script type="text/javascript">
        <?php
        $conn = mysql_connect("localhost","root");
        mysql_select_db("tr", $conn); 
        $s = "SELECT message FROM messages WHERE to_viewed = '0' ");
        //you can remove the trigger_error() call on the live server
        //and place an @ before mysql_query to stifle errors from being displayed
        $q = mysql_query($s) or trigger_error(mysql_error());

        //you can take this comment and the next line out, just testing for result
        echo '<p>Rows: '.mysql_num_rows($q).'</p>';

        if(mysql_num_rows($q) > 0) {
            require('pm.php');
            ?>
            var answer = confirm("you have new message");
            if(answer) {
                window.location = "http://domain.com/page"
            } else {
                //whatever action if no action, you don't need the else
            }
        }
    </script>
</body>
</html>
Vernon
Had a misspelling in the SQL statement... I've changed 'to_viewd' to 'to_viewed' like it should be. (Thanks for the vote up somebody!)
Vernon
A: 

It wold be helpful to view the resulting script that you are actually getting errors on. Do a 'View Source' in your browser and check the resulting client code.

hendepher