doubo1871 2017-01-25 13:49
浏览 75
已采纳

如何将用户输入数据发送到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条回答 默认 最新

  • dounei9043 2017-01-25 14:08
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 来真人,不要ai!matlab有关常微分方程的问题求解决,
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算