在 php while 循环中运行 jquery

发布于 2024-11-09 01:15:55 字数 2408 浏览 0 评论 0原文

好的,就在这里。

我有一个index.php 页面。它的作用是生成 html 页面或运行 while 循环。

它只会在您填写一些信息并按 html 页面上的提交按钮后运行循环。现在,在底部的 html 页面上,我显示“__ 操作已完成”,其中空白是一个变量,每次运行循环时都会添加 1。

现在我想要发生的是每次运行循环时更新该数字。我也被告知我们 ajax/jquery 来做到这一点,但我一直无法弄清楚。

那么我可以在 while 循环中放入什么来更新变量呢?

<?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;
?>

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;
?>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

Bonjour°[大白 2024-11-16 01:15:55

您可以做的是使用会话变量来记录用户完成操作的次数。

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

然后在 HTML 中输出该变量。

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.

穿越时光隧道 2024-11-16 01:15:55

我已经修改了你的代码,以便它可以使用 jquery ajax 提交表单
响应是一个json字符串,我们用jquery解析它以获得一个javascript对象
这是您的表单代码:

<!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>

以及 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.'"}';
?> 

我们将数字存储在会话中并在每个操作(调用 process.php)中递增它
并用response.number更新段落id =“result”

希望有帮助

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

差↓一点笑了 2024-11-16 01:15:55

你想做的事是不可能的(嗯,不是你想的那样)。

如果我必须将其作为绝对要求(即使它的设计很糟糕),我会这样做:

无论您的号码位于原始输出文件中的哪个位置,都将其包装在 div 或 span 中,并给它一个唯一的 id。

然后我将使用会话变量作为循环计数器。

最后,我将使用带有计时器插件的 jQuery 以 1 或 2 秒的间隔触发。在计时器内,您应该在后台调用一个 .php 文件,该文件仅返回会话变量的值。

这里有一些代码要演示:(

根据下面的注释进行编辑以澄清)

这是一个工作示例:

<?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>

然后在 get_counter.php 文件中,只需执行如下操作:

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

现在对于循环文件

<?php
    // exec_loop.php

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

保存这三个文件,然后您就可以了会得到你想要的结果。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文