开发者

Running jquery in a php while loop

Ok so here it is.

I have a index.php page. What it does is either make the html page or run a while loop.

It will only run the loop after you fill in some info and press a submit button on the html page. Now on the html page at the bottom i have it say "__ actions have been completed" with the blank being a variable that has 1 added to it each time the loop is run.

Now what i want to happen is that number to update everytime the loop is run. I have also been told to us ajax/jquery to do this but i have been unable to figure it out.

So what can i put in the while loop to have the variable update?

<?php
$number = $_POST['number'];
if(isset($number)){}
else{
    $number = 0;
}
if(isset($_POST['Submit'])){
    $MN = $_POST['MN'];
    $count = $_POST['count'];
    $provider = $_POST['provider'];
    for ($i = 0; $i < $count; $i++) {
        $m = rand(10e16, 10e20);
        $n = base_convert(¤m, 10, 36);
        $subject = $m;
        $body =  $n;
        $number = $number + 1;
    }
}
echo <<<END
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html> 
  <body>
    <form action="$PHP_SELF" method="post">
    <center> <p>
    <label><b><big><big><big><big>开发者_如何学运维<big><big><big>Page</big></big></big></big></big></big></big></b></label>
    </p>
    <p>
    <p>
    <label><strong><u>MN</u></label>
    </p>
    <input name="MN" type="text" value=""/>
    </p>
    <p>
    <p>
    <label><strong><u>Number to Send</u></label>
    </p>
   <input name="count" type="text" value = "1"/>
     <input name = "number" type = "hidden" value = "$number"/>
    </p>
    <p>
    <p>
    <label><strong><u>Provider</u></label>
    </p>
    <select name="provider">
        <option value="">Choose One...</option>
    </select>
    </p>
    <p>
    <input name = "Submit" type = "submit" value = "Send"></a>
    </p>
    <p>You have done {$number} actions</p>
    </center>
    </body></html>

    </style>
    </head>
    <style type="text/css">
    <!--
    body {
        font-family: Arial, Helvetica, sans-serif;
        font-size: 12px;
        font-style: normal;
        line-height: normal;
        color: #FF0000;
        background-color: #000000;
    }
    .style7 {color: #FF0000}
END;
?>


What you could do, is use a session variable to record the number of times the user has completed the action.

if (!$_SESSION["times"]) $_SESSION["times"] = 0;
else $_SESSION["times"]++

Then in the HTML, output that variable.


i have modified your code so it can use jquery ajax to submit the form the response is a json string , we parse it with jquery to get an javascript object this your form code :

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html> 
    <head>
     <style type="text/css">
        body {
            font-family: Arial, Helvetica, sans-serif;
            font-size: 12px;
            font-style: normal;
            line-height: normal;
            color: #FF0000;
            background-color: #000000;
        }
       .style7 {color: #FF0000}
    </style>
    <script type="text/javascript" src="js/jquery-1.5.1.min.js"></script>
    <script type="text/javascript">
        $(function(){
            $("#Submit").click(function(){
                $.ajax({
                    url:"process.php",
                    type:"get",
                    data:$("form").serialize(),
                    success:function(response){
                        var obj = jQuery.parseJSON( response );
                        var success = obj.success;
                        var actionsNumber = obj.number;
                        $("#result").html('You have done  '+actionsNumber+'  actions');                     
                    }
                })
            })
        })
    </script>
    </head>
  <body>
    <form action="" method="post">
    <center> <p>
    <label><b><big><big><big><big><big><big><big>Page</big></big></big></big></big></big></big></b></label>
    </p>
    <p>
    <p>
    <label><strong><u>MN</u></label>
    </p>
    <input name="MN" type="text" value=""/>
    </p>
    <p>
    <p>
    <label><strong><u>Number to Send</u></label>
    </p>
   <input name="count" type="text" value = "1"/>
     <input name = "number" type = "hidden" value = "$number"/>
    </p>
    <p>
    <p>
    <label><strong><u>Provider</u></label>
    </p>
    <select name="provider">
        <option value="">Choose One...</option>
    </select>
    </p>
    <p>
    <input id="Submit" type = "button" value = "Send">
    </p>
    <p id="result"></p>
    </center>
    </body></html>

and the code of process.php :

<?php session_start();
// process your form data as you do 
//:::::::::
//
if(!isset($_SESSION['number'])){
      $_SESSION['number'] = 0;
    }
$number =  $_SESSION['number']++;

// output json response 
echo'{"success":"true","number":"'.$number.'"}';
?> 

we store the number in the session and increment it every action ( call of process.php) and update the paragraph id="result" with the response.number

hope that help


What you want to do isn't possible (well, not in the way you think).

If I had to do this as an absolute requirement (even though it stinks of poor design) I would do it like so:

Wherever your number is in the original output file, wrap it in a div or span and give it a unique id.

I would then use a session variable for your loop counter.

Finally, I would use jQuery with the timers plugin to fire off at 1 or 2 second intervals. Within the timer, you should call a .php file in the background that simply returns the value of the session variable.

Here's a bit of code to demonstrate:

(Edited to clarify based on comments below)

Here is a working example:

<?php
    // main_page.php

    session_start();
    $_SESSION['loop_count'] = 0;
?>
<html>
    <head>
        <title>Background Updating Example</title>
        <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
        <script type="text/javascript" src="PATH_TO_YOUR_SCRIPTS/timers.jquery.js"></script>
        <script type="text/javascript">
            $(document).ready(function() {
                $.ajax({
                    url: 'exec_loop.php',
                    success: function(data) {
                        $('#status_message').html("Loop started...");
                    }
                });

                 $(document).everyTime(1000, function() {
                     $.ajax({
                         url: 'get_counter.php',
                         success: function(data) {
                             $('#counter_text').html(data);
                         }
                     });
                 });

            });
        </script>
    </head>
    <body>
        The loop has executed <span id='counter_text'>0</span> times.
    </body>
</html>

Then in the get_counter.php file, just do something like this:

<?php
    // get_counter.php
    session_start();
    echo $_SESSION['loop_count'];
?>

Now for the loop file

<?php
    // exec_loop.php

    session_start();
    for ($i = 0; $i < 50000000; $i++) {
        $_SESSION['loop_count']++;
    }
?>

Save these three files, and you'll get the result you desire.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜