0

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;
?>
1
  • 1
    show some code, no body like guessing an answer Commented May 22, 2011 at 22:10

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

im not trying to show how many times the user completed a action i am trying to have the user see how many times the loop has run in real time, I updated my post to clarify this
0

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

2 Comments

How would i make the ajax function call the process.php count umber of times?
ok, so i have been toying with this code and to get use out of it i need a loop around the ajax stuff, but when i add one whenever i press submit the whole browser gets incredibly laggy, any way to alleviate this?
0

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.

13 Comments

so where would i put the 2nd piece of coding(the timer)
Thinking more about this, you should be doing your loop in a separate file, because php will buffer your output until the loop is done. So move your loop code to a second php file, and do a similar ajax call from your front end. Both functions should be in your jQuery $(document).ready() function.
where would the $(document).ready() function be. I got all the code down and uploaded and it kinda sorta doesnt work right now. It will display 0 but when i submit the file it doesnt update the number and when it is done it goes to a white screen, when i press the back arrow or go back to the page it shows the correct number though. And how should i work the submit button
the document ready function goes on your main page within a script tag. jQuery won't execute the code inside until the document has been rendered by the browser. You have the proper plugins, right? jQuery & jQuery timers?
proper plugins <script type="text/javascript" src="code.jquery.com/jquery-latest.js"></script> <script src="jquery.timers.js" type="text/javascript"></script> with plugins.jquery.com/files/jquery.timers-1.2.js.txt being the timer code and is <form action="exec_loop.php" method="post"> the right code for sending the submit button or should i do something different
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.