当我使用链接到其他文件的标题时,如何更新表单的用户输入? [英] How to update user input of a form when I am using header that links to other file?

查看:45
本文介绍了当我使用链接到其他文件的标题时,如何更新表单的用户输入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用php和mysql编写表单。主要目标是使表格(1)检测缺失的字段。 (2)成功提交后更新用户输入和(3)最重要的是避免重新加载/刷新时重新提交。我可以管理第一个和第三个,但对第二个没有任何想法。这是我的代码(能够实现第一和第三)



<!DOCTYPE html> 
< html>
< head>< / head>
< body>
<?php
$ name =;
$ class =;
$ school =;

if(isset($ _ POST [submit])){
$ name = $ _POST [name];
$ class = $ _POST [class];
$ school = $ _POST [school];
$ output = false;

if(空($ _ POST [name])|| empty($ _ POST [class])|| empty($ _ POST [school])){
echo'field不能为空';
$ output_form = true;
}
if(!empty($ _ POST [name])&&!empty($ _ POST [class])&&!empty($ _ POST [school] ])){
$ hostname =localhost;
$ admin =root;
$ password =;
$ database =testdatabase;

$ dbc = mysqli_connect($ hostname,$ admin,$ password,$ database)或die(数据库连接错误);

$ insert_query =INSERT INTO`squyy`(`name`,`class`,`school`)VALUES('$ name','$ class','$ school');
$ insert_result = mysqli_query($ dbc,$ insert_query)或die(error);
if($ insert_result == 1)
echodata inserted;
else
echoinsert query failed;

mysqli_close($ dbc);
header('Location:form2.php');
}
}
其他
$ output = true;

if($ output){
?>
< form action =<?php echo $ _SERVER ['PHP_SELF'];?>方法= POST >
名称:< input type =textname =namevalue =<?php echo $ name?>/>< br />
类:< input type =textname =classvalue =<?php echo $ class?>/>< br />
学校:< input type =textname =schoolvalue =<?php echo $ school?>/>< br />
< input type =submitvalue =submitname =submit/>
< / form>
<?php
}
?>
< / body>
< / html>





我的第二个文件form2.php(表单提交后的成功页面)



< body> 
名称:/ *用户输入* /< br />
类:/ *用户输入* /< br />
学校:/ *用户输入* /< br />





因为我无法访问变量$ name,$ class,$ school of form.php我在更新用户输入数据时遇到问题。那么无论如何都要在文件中访问变量,或者不可能以这种方式进行。



我尝试了什么:



我尝试更新而不使用标题,它工作正常。但我需要这样做。

解决方案

name =;


class =;

school =;

if(isset(


I am writing a form using php and mysql. The main goal is to make the form (1) detect missing field. (2) update user input after successful submit and (3) most importantly to avoid re-submission on reload/refresh. I am able to manage the first and the third one but doesn't have any idea on the second one. Here's my code (able to achieve first and third)

   <!DOCTYPE html>
<html>
    <head></head>
    <body>
        <?php
        $name = "";
        $class = "";
        $school = "";

        if(isset($_POST["submit"])){
            $name = $_POST["name"];
            $class = $_POST["class"];
            $school = $_POST["school"];
            $output = false;

            if(empty($_POST["name"]) || empty($_POST["class"]) || empty($_POST["school"])){
                    echo 'field cannot be empty';
                    $output_form = true;
            }
            if(!empty($_POST["name"]) && !empty($_POST["class"]) && !empty($_POST["school"])){
                    $hostname = "localhost";
                    $admin = "root";
                    $password = "";
                    $database = "testdatabase";

                    $dbc = mysqli_connect($hostname, $admin, $password, $database) or die("database connection error");

                    $insert_query = "INSERT INTO `sorty` (`name`, `class`, `school`) VALUES ('$name', '$class', '$school')";
                    $insert_result = mysqli_query($dbc, $insert_query) or die("error");
                    if($insert_result == 1)
                        echo "data inserted";
                    else
                        echo "insert query failed";

                    mysqli_close($dbc);
                    header('Location: form2.php');
            }
        }
        else
            $output = true;

        if($output){
        ?>
            <form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post">
                Name: <input type="text" name="name" value="<?php echo $name?>"/><br/>
                Class: <input type="text" name="class" value="<?php echo $class?>"/><br/>
                School: <input type="text" name="school" value="<?php echo $school?>"/><br/>
                <input type="submit" value="submit" name="submit"/>
            </form>
        <?php
        }
        ?>
    </body>
</html>



My second file form2.php(succesful page after form submission)

<body>
Name: /*user input here*/<br/>
Class: /*user input here*/<br/>
School: /*user input here*/<br/>



As I can't access the variable $name, $class, $school of form.php I am having problem updating the user input data. So is there anyway to access the variable across file or is it not possible to do in this way.

What I have tried:

I have try updating without using header and it works fine. But I need to do in this way.

解决方案

name = "";


class = "";


school = ""; if(isset(


这篇关于当我使用链接到其他文件的标题时,如何更新表单的用户输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆