Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/434.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如果答案正确,如何将提交按钮重定向到下一页_Javascript_Php_Html - Fatal编程技术网

Javascript 如果答案正确,如何将提交按钮重定向到下一页

Javascript 如果答案正确,如何将提交按钮重定向到下一页,javascript,php,html,Javascript,Php,Html,若答案正确,我想将提交按钮重定向到下一个问题url <html> <head> <title>PHP Test</title> </head> <body> <p> Answer the question. </p> CLUE: The base is rotated <br/></body>

若答案正确,我想将提交按钮重定向到下一个问题url

    <html>
    <head>
    <title>PHP Test</title>
    </head>
    <body>
    <p>
    Answer the question.
    </p>
    CLUE: The base is rotated
    <br/></body>

    <form action="" method="post">
    Answer: <input type="text" name="number" /><br />

    <input name="submit" type="submit">
    </form>

    </html>

PHP测试

回答问题。

提示:底座是旋转的
回答:

在您的html标记前放置与此类似的内容:

<?php
if (isset($_POST['number']) && $_POST['number'] == 12345) {
    header('Location: /next/page/');
}
?>

由于返回到同一页,您可以添加一个隐藏的输入字段

<?php if (isset($_POST[number]){

        if ($_POST['number'] == $answer){
            $_SESSION['answer1'] == $answer;
            header("Location: /next/page");
        }
    }

?>

您可以创建一个包含答案的隐藏字段,并创建一个新脚本,将文本字段中的答案检查到隐藏字段中的答案($answer),并重定向到新页面

假设我们将此脚本称为checkanswer.php->那么表单操作应为

然后,checkanswer.php将如下所示:

<?php
  if(isset($_POST['number'])
    {
     if ($_POST['number'] == $answer)
       { echo "Correct!";
         header("location:nextquestion.php");
       }
         else
         { echo "That is not the right answer, try again!"; }
 ?>

这里有一个非常基本的方法来检查是否给出了正确的答案,并使用
标题
重定向到另一个页面

如果用户在表单字段中键入单词
answer
,则该单词将设置为
TRUE

如果答案不正确,它将显示
对不起,回答错误。

<?php

if(isset($_POST['submit'])) {
$answer = "answer";

if($_POST['number'] == $answer) {

// echo "Correct"; 

header("Location: correct.php"); 

} else { 
die("Sorry, wrong answer.");
}

}

?>

使用会话方法:

correct.php


如果($_POST['number']=“answer”){echo“Correct”///或header(“Location:Correct.php”);}否则{die(“Sorry”)}
访问您想要JavaScript或php解决方案吗?因为问题只有JS标记。@Fred ii-如果答案正确,我想将其重定向到下一页。tymeJV编辑我的问题并删除php标记。它可以工作,但如果我想显示一条消息,如果它不正确,我使用的是else,但当页面出现时,它会显示else消息。
<?php
session_start();

if (!isset($_SESSION['name'])) {
$_SESSION['name'] = "correct";
}

if(isset($_POST['submit'])) {
$answer = "answer";

if($_POST['number'] == $answer) {
header("Location: correct.php"); 
} else { 
die("Sorry, wrong answer");
}

}

else {
die("You cannot access this page from here.");
}
?>
<?php
session_start();

if (!isset($_SESSION['name'])) {
die("Sorry, session not set.");
}

else {
echo "Correct answer.";
}

?>