如何将用户输入数据发送到PHP并将数据从一个PHP页面发送到另一个PHP页面

I am currently creating a basic maths website where it would ask the user to solve a multiply question.

I'm planning to have the HTML layed out like:

1*8 = <input type="number" maxlength="3" name="answer1"><br>
2*9 = <input type="number" maxlength="3" name="answer2"><br>
4*4 = <input type="number" maxlength="3" name="answer3"><br>
<button type="button">Check Answers</button>
<p></p>

This HTML code is generated by some PHP code:

<?php
    $t = 0;

    for ($n = 1; $n <= 10; $n++) {

        $a = rand(1, 10);
        $b = rand(1, 10);

        echo $a . "*" . $b . " = <input type=\"number\" maxlength=\"3\" name=\"answer" . $n . "\"><br>";

        $t = $a * $b;
    }
?>

Now that is generated, I worked out what the total would be of the $a and $b (random numbers):

$t = $a * $b;

For the HTML button I wanted it so when the user clicks it, it would check all the input boxs and see if what they inputted = $t Forgetting all validation, all I want to get working for now is the very basics.

I've got further with this using ajax to create a on-click event:

$(document).ready(function(){
    $("button").click(function(){
        $.ajax({
            type: 'POST',
            url: 'calcc.php',
            success: function(data) {
                $("p").text(data);
            }
       });
   });
});

I've been messing around with it for a while now. I worked out that I would need to use $_SESSION or something similar to save the value of $t from before, but I can't seem to work out how it would all work, I keep getting errors of long repeated arrays and I have little idea how I would extract the information I want.

Another problem I am having is retrieving the users input upon the on-click, I have used POST and GET before but when I click the submit button it will act in a way where the page would reload and change all of the random numbers.

I preferably wanted to solve this in PHP. This has been bothering me for a while I just wanted to see if any of you had any great ideas of how I could make this work.

Thank you in advance.

</div>

2个回答



您的问题有两种可能的解决方案。 </ p>


  • 第一个(也是最简单的)是在同一个脚本中检索$ _POST []参数,并在收到$ _POST []参数时执行代码 不是空的。 </ li>
  • 第二个解决方案(您在这里使用的解决方案)是使用AJAX。
    我查看了您的代码并进行了一些调整。</ p>

    $(document).ready(function(e){
    //为事件添加preventDefault以便不提交表单。
    e.preventDefault();
    $(“ 按钮“)。click(function(){
    //从表单中获取所有数据
    var data = $(form).serialize();
    $ .ajax({
    type:'POST',

    //使用ajax发送数据。
    //您可以通过$ _POST ['表单字段名称']在数据中检索它
    data:data,
    url:'calcc.php',
    n success:function(data){
    $(“p”)。text(data);
    }
    });
    });
    }); </ code> </ p> </ li>
    </ ul>

    我希望这可以帮助你:)
    评论如果你想要更多的解释。</ p>
    </ div>

展开原文

原文

There are 2 possible solutions for your problem.

  • The first one ( and most simple ) is retrieving your $_POST­[] parameters in the same script and executing the code when you receive a $_POST[] parameter that isn't empty.
  • The second solution ( the one that you are using here ) is to use AJAX. I looked at your code and have some adjustements.

    $(document).ready(function( e ){ //Add an preventDefault on your event in order to not do the submit of the form. e.preventDefault(); $("button").click(function(){ //Get all the data from your form var data = $(form).serialize(); $.ajax({ type: 'POST', //send the data with the ajax. // You can retrieve it in your php through $_POST['name of form field'] data: data, url: 'calcc.php', success: function(data) { $("p").text(data); } }); }); });

I hope this helps you :) Comment if you would like some more explanations.



我也会在输入字段中输入$ a和$ b。 它们可以是只读的,甚至是隐藏的。 在这种情况下,您的php脚本会获得随机的$ a和$ b以及用户的结果。 您的PHP脚本现在可以验证结果。
如果您需要一些代码,请告诉我。</ p>
</ div>

展开原文

原文

I would put $a and $b in input fields too. They could be readonly, or even hidden. In this case your php script gets your random $a and $b and the result from the user. Your php script can now validate the result. Tell me if you need some code.

dousheyan0375
dousheyan0375 好吧,我想我明白了你的意思,我会在HTML页面上放上$ a和$ b随机数以及用户输入。 我可以立刻得到它,并在第二个PHP脚本(点击)上计算答案。 基本上是另一种方法而不是$ _SESSION。 然后我唯一的问题就是调用PHP。 谢谢 :)
3 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问