2

i'm working on a PHP - jQuery simple script which is sending an email on every PHP.

Here is my code:

<HTML>
<head>
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
</head>
<?php

require "./config.php";
ConnectWithMySQLDatabase();
$time = date('h:i:s');

$result = mysql_query("SELECT * FROM `emails`");
    while($row = mysql_fetch_array($result))
    {
    $email = addslashes($row['email']);
    ?>
    <script type="text/javascript">
        setTimeout(function() {
            $.post( "./Sender.php", { name: "<?PHP echo $email;?>", time: "<?PHP echo $time;?>" })
              .done(function( data ) {
                $( "#result" ).html( "Email sended to: " + data + "<br>");
              });
          }, 5000);
    </script>

    <?PHP 
    }
    ?>

<div id="result"></div>

The problem with my code is that it is wating 5 seconds and then it's rushing the sending of the emails in like instant.

  1. How can i make this script send 1 email on every 5 seconds untill all emails are send?
  2. How can i make this script show 1 result on 1 line and then the next result the line bellow and so on ? Now the script is printing: Email sended to: on just one line and chaning the emails but not making more lines..

Thanks in advance!

Venelin
  • 2,905
  • 7
  • 53
  • 117
  • `setInterval(func,5000)` – loveNoHate Dec 13 '14 at 15:28
  • i should change `setTimeout` to `setInterval` ? – Venelin Dec 13 '14 at 15:29
  • Look at your source code, you will have a load of `setTimeout` functions that all trigger 5 seconds after the page loads. You should probably do this on the server-side using for example a queue and a cron job / scheduled job. – jeroen Dec 13 '14 at 15:34
  • possible duplicate of [setTimeout or setInterval?](http://stackoverflow.com/questions/729921/settimeout-or-setinterval) – Mmmh mmh Dec 13 '14 at 15:46

1 Answers1

4

Answering your question:

Your PHP code generates a javascript script with a lot of calls to setTimeout function, but all of them, receiving 5000 as argument, so, it's natural that all emails will be sent at once.

I think you can fix it by adding 5000 to each setTimeout, like this:

$result = mysql_query("SELECT * FROM `emails`");
    $wait = 0;
    while($row = mysql_fetch_array($result))
    {
    $email = addslashes($row['email']);
    $wait += 5000;
    ?>
    <script type="text/javascript">
    setTimeout(function() {
        $.post( "./Sender.php", { name: "<?PHP echo $email;?>", time: "<?PHP echo $time;?>" })
          .done(function( data ) {
            $( "#result" ).html( "Email sended to: " + data + "<br>");
          });
      }, <?php echo $wait; ?>); //printing 5000, 10000, 15000, etc...
</script>

<?PHP 
}
?>

To your second question, you just have to use the jQuey.append function:

Instead of $("#result").html("Email sended to: " + data + "<br>");

use $("#result").append("Email sended to: " + data + "<br>");


Observation:

I know this is not the point here, but I think it's very important to note: your code seems extremely insecure, because a client side script is able to make your server send emails, and it seems that there is no authentication method being employed, because the ajax request just send the email and some "time" ...

Marcos
  • 47
  • 6