SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    SitePoint Addict Philip Toews's Avatar
    Join Date
    Dec 2001
    Location
    Kuala Belait, Brunei
    Posts
    367
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question javascript reload page with PHP help a desperate newbie...

    This is a script for sending out newsletters from a PHP script. Everything works fine except it doesn't reload the page to complete the mailing. It is supposed to send newsletters out in blocks of subscribers ($perpage). I have tested it and it stops after the number in $perpage and doesn't finish the rest of the list. It is supposed to reload the page and grab the next block of subscribers, send the newsletter to them and so on.

    The following script fails to reload the page after the number in $perpage. What am I doing wrong?

    TIA from a desperate newbie

    PHP Code:

    <?php

    ini_set
    ('max_execution_time''300'); // sets timeout to 5 minutes



    $perpage 3// number of users to e-mail every time
    if (!is_int($startnum)) {
        
    $startnum 0;
    }

    echo 
    'Emailing ' $perpage ' users, starting at record ' $startnum+.'...';

    $subscribers mysql_query("SELECT email, newsletter_format FROM newsletter WHERE validation = 1 LIMIT $startnum,$perpage");
    if (!
    $subscribers) {
      echo(
    "<p>Error retrieving subscriber list from database!<br />".
           
    "Error: " mysql_error() . "</p>");
      exit();
    }
    while (
    $subscriber mysql_fetch_array($subscribers)) {
        
    $to htmlspecialchars($subscriber['email']);
        
    $newsletter_format htmlspecialchars($subscriber['newsletter_format']);
        if (
    $newsletter_format == 'html') {
            
    mail($to$subject$htmlmessage$htmlheader);
            
    $htmlsent++;
        } elseif (
    $newsletter_format == 'text') {
            
    mail($to$subject$textmessage$textheader);
            
    $textsent++;
        }
    }

    echo 
    'Done! Taking you to next page, please hold...';
    echo 
    "\n<script language=\"javascript\">\n";
    echo 
    "<!--\n";
    echo 
    "window.location=\"" $PHP_SELF "?startnum=" $startnum+$perpage "\";\n";
    echo 
    "// -->\n";
    echo 
    "</script>\n";
    //echo ("<h1>Newsletter # $issue Sent</h1>");
    ?>
    <p><a href="admin.php"><img src='images/main-console.gif' width='114' height='51' border='0' alt='Go back to the main console'></a><a href="newnewsletter.php"><img src='images/new-newsletter.gif' width='114' height='51' border='0' alt='Create a new newsletter'></a><a href="newsletters.php"><img src='images/show-newsletters.gif' width='114' height='51' border='0' alt='Go back to the newsletter management console'></a><br><br></p>
    <?php

    $sent_to 
    = ($htmlsent $textsent);
    $sql "UPDATE newsletter_management SET sent_to='$sent_to', send_date = curdate(), sent='yes' WHERE ID=$id";
    $newslettersent = @mysql_query($sql);
    if (!
    $newslettersent) {
      echo(
    "<p>Error updating newsletter_management table!<br />".
           
    "Error: " mysql_error() . "</p>");
      exit();
    }
    echo (
    "<h3>Newsletter # $issue sent to $htmlsent html subscribers and $textsent text subscribers</h3>");
    echo (
    "<h3>A total of $sent_to subscribers</h3>");


    ?>
    Philip Toews Professional esl Educator and ASP.NET wannabe

    http://www.philiptoews.com
    philip@philiptoews.com

  2. #2
    What? Maelstrom's Avatar
    Join Date
    Oct 2001
    Location
    Whistler BC originally from Guelph Ontario
    Posts
    2,175
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't want to cut and paster but you have a

    echo "window.location=\"" . $PHP_SELF . "?startnum=" . $startnum+$perpage . "\";\n";

    Try

    echo "top.location.href=\"" . $PHP_SELF . "?startnum=" . $startnum+$perpage . "\";\n";

    That should work fine now.
    Maelstrom Personal - Apparition Visions
    Development - PhP || Mysql || Zend || Devshed
    Unix - FreeBSD || FreeBsdForums || Man Pages
    They made me a sitepoint Mentor - Feel free to PM me or Email me and I will see if I can help.

  3. #3
    SitePoint Addict Philip Toews's Avatar
    Join Date
    Dec 2001
    Location
    Kuala Belait, Brunei
    Posts
    367
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    nope

    I implemented your suggestion. The result is exactly the same. I don't know if it is helpful, but the page the source code of the page the script lands on is:

    PHP Code:
    <!-- sendnewsletter.php -->
    <!
    DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

    <
    html>
    <
    head>
        <
    title>SEND NEWSLETTER</title>
    <
    link rel="stylesheet" href="styles.css" type="text/css">
    </
    head>
    <
    body>



    1...</p><h2>DoneTaking you to next pageplease hold...</h2>
    <
    script language="javascript">
    <!--
    2";
    // -->
    </script>
    <h1>Newsletter # 4 Sent</h1><p><a href="
    admin.php"><img src='images/main-console.gif' width='114' height='51' border='0' alt='Go back to the main console'></a><a href="newnewsletter.php"><img src='images/new-newsletter.gif' width='114' height='51' border='0' alt='Create a new newsletter'></a><a href="newsletters.php"><img src='images/show-newsletters.gif' width='114' height='51' border='0' alt='Go back to the newsletter management console'></a><br><br></p>
    <h3>Newsletter # 4 sent to 2 html subscribers and 0 text subscribers</h3><h3>A total of 2 subscribers</h3>

    </body>
    </html> 
    I notice that there is an absence of the javascript code that is supposed to reload the page. Is that the problem? Also, why does it print "...1" at the beginning of the page?

    The page definately does not reload. I put some test variables in the URL to be reloaded and they are completely absent when the script finishes.

    I am testing this script on my winXP pro box, locally. Could that possibly have anything to do with the reload failure?
    Last edited by Philip Toews; Mar 2, 2002 at 03:20.
    Philip Toews Professional esl Educator and ASP.NET wannabe

    http://www.philiptoews.com
    philip@philiptoews.com

  4. #4
    What? Maelstrom's Avatar
    Join Date
    Oct 2001
    Location
    Whistler BC originally from Guelph Ontario
    Posts
    2,175
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: nope

    No what you run it on won't matter. It only has to do with the browser. Hmmm strange dilema...

    Try this. Instead of echoing everything cut the php out and run HTML only like this
    PHP Code:
    ?>
    <p>'Done! Taking you to next page, please hold...'</p>
    <script language="javascript">
    <!--
    window.location='"+<?= $PHP_SELF?>+ "?startnum="+ <?= $startnum+$perpage?>+"'";
    // -->
    </script>
    <?php
    Also I did notice this line that could be messing it up as well.

    Code:
    echo 'Done! Taking you to next page, please hold...';
    Try using double quote instead.

    Code:
    echo "one! Taking you to next page, please hold...";
    Anyways thats all for now hopefully that helps.
    Maelstrom Personal - Apparition Visions
    Development - PhP || Mysql || Zend || Devshed
    Unix - FreeBSD || FreeBsdForums || Man Pages
    They made me a sitepoint Mentor - Feel free to PM me or Email me and I will see if I can help.

  5. #5
    SitePoint Addict Philip Toews's Avatar
    Join Date
    Dec 2001
    Location
    Kuala Belait, Brunei
    Posts
    367
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Thanks Maelstrom

    thanks! I'll try it tommorow when I'm less beat...put in 20 hrs coding yesterday and my head's fried. Sometimes it is better to rest and then come at a problem fresh.

    BTW. How's the weather in Whistler?...my family's from Vancouver..but I'm in tropical Thailand. The weather here is always great but I miss my folks...


    Thanks for your time. Can I PM you about this thing?

    p
    Philip Toews Professional esl Educator and ASP.NET wannabe

    http://www.philiptoews.com
    philip@philiptoews.com

  6. #6
    What? Maelstrom's Avatar
    Join Date
    Oct 2001
    Location
    Whistler BC originally from Guelph Ontario
    Posts
    2,175
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: Thanks Maelstrom

    Originally posted by Philip Toews
    thanks! I'll try it tommorow when I'm less beat...put in 20 hrs coding yesterday and my head's fried. Sometimes it is better to rest and then come at a problem fresh.

    BTW. How's the weather in Whistler?...my family's from Vancouver..but I'm in tropical Thailand. The weather here is always great but I miss my folks...


    Thanks for your time. Can I PM you about this thing?

    p
    Well I hope that helped. Cause I ripped you code and tried it and it worked fine.

    Weather is awesome. Little snow but still tonnes of awsome skiiing and snowboarding. The best is no clouds for a week so very beautiful scenery.
    Maelstrom Personal - Apparition Visions
    Development - PhP || Mysql || Zend || Devshed
    Unix - FreeBSD || FreeBsdForums || Man Pages
    They made me a sitepoint Mentor - Feel free to PM me or Email me and I will see if I can help.

  7. #7
    SitePoint Addict Philip Toews's Avatar
    Join Date
    Dec 2001
    Location
    Kuala Belait, Brunei
    Posts
    367
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    ummm nope not yet

    Hi again Maelstrom,

    I implemented your suggestions and the result is exactly the same. You said you ripped it and it worked. Can you post the code that you ripped? Would you mind taking a look at this again?

    Here's the updated version of the code so far. This version produces the exact same result as before...the script sends 2 mails and then stops



    PHP Code:
    <?php
    ini_set
    ('max_execution_time''300'); // sets timeout to 5 minutes



    $perpage 2// number of users to e-mail every time
    if (!is_int($startnum)) {
        
    $startnum 0;
    }

    echo 
    '<p>Emailing ' $perpage ' users, starting at record ' $startnum+.'...</p>';

    $subscribers mysql_query("SELECT email, newsletter_format FROM newsletter WHERE validation = 1 LIMIT $startnum,$perpage");
    if (!
    $subscribers) {
      echo(
    "<p>Error retrieving subscriber list from database!<br />".
           
    "Error: " mysql_error() . "</p>");
      exit();
    }
    while (
    $subscriber mysql_fetch_array($subscribers)) {
        
    $to htmlspecialchars($subscriber['email']);
        
    $newsletter_format htmlspecialchars($subscriber['newsletter_format']);
        if (
    $newsletter_format == 'html') {
            
    mail($to$subject$htmlmessage$htmlheader);
            
    $htmlsent++;
        } elseif (
    $newsletter_format == 'text') {
            
    mail($to$subject$textmessage$textheader);
            
    $textsent++;
        }
    }

    echo 
    "Done! Taking you to next page, please hold...";

    ?>

    <script language="javascript">
    <!--
    window.location='"+<?= $PHP_SELF?>+ "?startnum="+ <?= $startnum+$perpage?>+"'";
    // -->
    </script>
    <?php
    echo ("<h1>Newsletter # $issue Sent</h1>");
    ?>
    <p><a href="admin.php"><img src='images/main-console.gif' width='114' height='51' border='0' alt='Go back to the main console'></a><a href="newnewsletter.php"><img src='images/new-newsletter.gif' width='114' height='51' border='0' alt='Create a new newsletter'></a><a href="newsletters.php"><img src='images/show-newsletters.gif' width='114' height='51' border='0' alt='Go back to the newsletter management console'></a><br><br></p>
    <?php

    $sent_to 
    = ($htmlsent $textsent);
    $sql "UPDATE newsletter_management SET sent_to='$sent_to', send_date = curdate(), sent='yes' WHERE ID=$id";
    $newslettersent = @mysql_query($sql);
    if (!
    $newslettersent) {
      echo(
    "<p>Error updating newsletter_management table!<br />".
           
    "Error: " mysql_error() . "</p>");
      exit();
    }
    echo (
    "<h3>Newsletter # $issue sent to $htmlsent html subscribers and $textsent text subscribers</h3>");
    echo (
    "<h3>A total of $sent_to subscribers</h3>");


    ?>
    Philip Toews Professional esl Educator and ASP.NET wannabe

    http://www.philiptoews.com
    philip@philiptoews.com

  8. #8
    What? Maelstrom's Avatar
    Join Date
    Oct 2001
    Location
    Whistler BC originally from Guelph Ontario
    Posts
    2,175
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: ummm nope not yet

    Ok never mind. The javascript works fine. Its something within your php that is messed. I though I had the solution and then reread the situation. I will take a deeper look at this and hopefully get back to you soon.
    Last edited by Maelstrom; Mar 4, 2002 at 21:16.
    Maelstrom Personal - Apparition Visions
    Development - PhP || Mysql || Zend || Devshed
    Unix - FreeBSD || FreeBsdForums || Man Pages
    They made me a sitepoint Mentor - Feel free to PM me or Email me and I will see if I can help.

  9. #9
    SitePoint Addict Philip Toews's Avatar
    Join Date
    Dec 2001
    Location
    Kuala Belait, Brunei
    Posts
    367
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    do you want to see the rest of the code?

    The rest of the code has the html and text headers and messages. Could the problem be in there. I'll post the code if you wish...but be forewarned, it is long!

    p
    Philip Toews Professional esl Educator and ASP.NET wannabe

    http://www.philiptoews.com
    philip@philiptoews.com

  10. #10
    What? Maelstrom's Avatar
    Join Date
    Oct 2001
    Location
    Whistler BC originally from Guelph Ontario
    Posts
    2,175
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: do you want to see the rest of the code?

    Originally posted by Philip Toews
    The rest of the code has the html and text headers and messages. Could the problem be in there. I'll post the code if you wish...but be forewarned, it is long!

    p
    No I believe Weirdy has it right. It is somewhere within that loop. He might even have it by now. .. The javascript is all good now.
    Maelstrom Personal - Apparition Visions
    Development - PhP || Mysql || Zend || Devshed
    Unix - FreeBSD || FreeBsdForums || Man Pages
    They made me a sitepoint Mentor - Feel free to PM me or Email me and I will see if I can help.

  11. #11
    SitePoint Addict Philip Toews's Avatar
    Join Date
    Dec 2001
    Location
    Kuala Belait, Brunei
    Posts
    367
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    newbie embarrassment

    Oops,

    I had to take the comment lines out. Now the page reloads, but I'm struggling passing the variables. Does javascript pass variables as part of a query string?

    p
    Philip Toews Professional esl Educator and ASP.NET wannabe

    http://www.philiptoews.com
    philip@philiptoews.com


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •