[英]How would I send user input data to PHP & send data from one PHP page to another PHP page
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: 我计划将HTML布局为:
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: 此HTML代码是由一些PHP代码生成的:
<?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): 现在生成了,我算出了$ a和$ b(随机数)的总和:
$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. 对于HTML按钮,我想要它,因此当用户单击它时,它将检查所有输入框,看看他们输入的内容是否等于$ t忘记所有验证,我现在要开始工作的只是基本知识。
I've got further with this using ajax to create a on-click event: 我使用ajax创建了一个单击事件,进一步介绍了这一点:
$(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. 我得出结论,我需要使用$ _SESSION或类似的东西来保存$ t之前的值,但是我似乎无法弄清楚它的工作方式,我不断收到长重复数组的错误,并且我几乎不知道该如何提取所需的信息。
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. 我遇到的另一个问题是在单击时检索用户输入,我以前使用过POST和GET,但是当我单击“提交”按钮时,它将以一种方式来使页面重新加载并更改所有随机数。
I preferably wanted to solve this in PHP. 我最好是想用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. 先感谢您。
There are 2 possible solutions for your problem. 有两种可能的解决方案来解决您的问题。
The second solution ( the one that you are using here ) is to use AJAX. 第二种解决方案(您在此处使用的解决方案)是使用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. 希望对您有所帮助:)如果您需要更多解释,请发表评论。
I would put $a and $b in input fields too. 我也会在输入字段中放入$ a和$ b。 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. 在这种情况下,您的php脚本会从用户那里获得随机$ a和$ b以及结果。 Your php script can now validate the result. 您的php脚本现在可以验证结果了。 Tell me if you need some code. 告诉我您是否需要一些代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.