Including a value from database using php, in a javascript popup

Discussion in 'PHP' started by janna, Jul 16, 2007.

  1. #1
    Im facing this problem of calculating the date that will make the popup come out 2 months before the expiry date, in simpler words, its like a notification. every time I log in, if there is a contract that will expire in two months time, in that particular day, then the popup will automatically come out to notify me, including the company's details. if there is no contract that will expire, then it will do nothing, no popup will come out. Its like two months in prior reminder alert. And the expiry date value has to be obtained from the database. This is what I've done so far.

    
    <?php
    
    include 'con2db.php';
    
    $expiry_date = $_GET['expiry_date'];
    
    $query = "SELECT * FROM cust_domain WHERE TO_DAYS(expiry_date) - TO_DAYS(NOW()) = 60";
    $result = mysql_query($query) or die ('Error in query: $query. ' . mysql_error());
    $row=mysql_fetch_row($result);
    if ($row)
    {
    ?>
    
    <script type="text/javascript">
    alert('Domain will expire in two months');
    document.location.href='reportDomain.php';
    </script>
    
    <?php 
    }
    else
    {
    echo "<script>document.location.href='reportDomain.php'</script>";
    }  
    ?>
    
    PHP:
    It works ok, but the popup keep on coming out when I include it in other file. I don't know how to stop it. Another problem is, I needed to include the value from database in the popup. Can anybody help me?
     
    janna, Jul 16, 2007 IP
  2. alemcherry

    alemcherry Guest

    Best Answers:
    0
    #2
    Ofcourse the message will come if you include it in any page. If you dont want to show it, you shouldnt include it! If you want to show the message only once, you may have to set a cookie and check it.

    I guess it will be better to use "confirm" instead of "alert" and ask if he want to renew now and if yes, redirect to the page. You may also want to check if the date is less than or equal to 60, rather than equal to 60. The cookie I mentioned above must be used in that case, so that the customer doesnt get to see the message every day. You can make the cookie expirt time a few days, so that the customer gets a reminder after a few days.

    Adding a value to the javascript is as simple as adding it to an HTML. Just treat the javascript as a piece of HTML and add the value the ususal way. Something like, the domain will expire in <?= $days ?> days. Simple!
     
    alemcherry, Jul 16, 2007 IP
  3. janna

    janna Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    I know that of course. I want it to come out, that's why I included it but it kept on popping up after i clicked 'ok', non-stop. I don't know how to make it pops up just once.that's why I asked this question in this forum. Im not an expert and I have very little knowledge about php. Anyways thanks, now I know its cookie that i have to study on.


    Actually, this is an admin system Im doing, so the alert its like a notification to remind the admin that the clients' contract is going to expire in two months, so that the admin can contact the client to suggest for renewing the contract. So yeah, I changed the query to
    $query = "SELECT * FROM cust_webhosting WHERE TO_DAYS(expiry_date) - TO_DAYS(NOW()) <= 60";
    PHP:
    This is what you meant rite?

    So let's say I want to put a value from database in the javascript pop up. so I just have to do like this?

    <script type="text/javascript">
    alert('Web Hosting for <?= $comp_name ?>will expire in two months');
    document.location.href='reportWeb.php';
    </script>
    Code (markup):
    Anyways, can you give an example for that cookie part. Thanks for replying.
     
    janna, Jul 16, 2007 IP