PHP 提交请求不起作用

我创建了一个注册用户系统并分为两个页面。(registercontrol.php 和 register.php)


在register.php中,通常“提交”按钮应该将数据发送到registercontrol.php,但是当我单击“提交”按钮时没有任何反应。它只是刷新页面,什么也不做,成员表中没有数据。我错过了什么吗?


注册.php


<?php 


require('../includes/config.php');


//if logged in redirect to members page

if ($user->is_logged_in() ){ 

    header('Location: ../dashboard/index.php'); 

    exit(); 

}


//define page title

$title = 'Demo';


//include header template

require('../layout/header.php');

?>


<input type="text" name="fullname" id="fullname" class="form-control form-control-user" placeholder="Your Name" value="<?php if(isset($error)){ echo htmlspecialchars($_POST['fullname'], ENT_QUOTES); } ?>" tabindex="1" required>


<?php

    //check for any errors

    if (isset($infofn)){

       foreach ($infofn as $infofn){

            echo '<p class="p-3 text-info">'.$infofn.'</p>';

        }

    }                                   

?>


<input id="submit" type="submit" name="submit" value="Create Account" class="btn btn-primary btn-user btn-block" tabindex="6">

注册控制.php


<?php


require('../includes/config.php');


if(isset($_POST['fullname'])){

    //fullname validation

    $fullname = $_POST['fullname'];


    if (! $user->isValidFullname($fullname)){

        $infofn[] = 'Your name must be alphabetical characters';

    }   

}


//if form has been submitted process it

if(isset($_POST['submit'])){


//if no errors have been created carry on

    if (!isset($infofn)){       


        try {


            //insert into database with a prepared statement

            $stmt = $db->prepare('INSERT INTO members (fullname) VALUES (:fullname)');

            $stmt->execute(array(

                ':fullname' => $fullname

            ));

            $id = $db->lastInsertId('memberID');

            


            //redirect to index page

            header('Location: register.php?action=joined');

            exit;


        //else catch the exception and show the error.

        } catch(PDOException $e) {

            $error[] = $e->getMessage();

        }

    }

}


千巷猫影
浏览 176回答 1
1回答

扬帆大鱼

您缺少一个表单元素,其action和method属性设置为您的其他脚本和 to post。在这种情况下action应该是registercontrol.php。如果不设置操作,表单将提交给自身,在本例中为register.php.像这样的事情应该可以解决问题(仅添加第一行和最后一行,其余的是您的代码):<form action="registercontrol.php" method="post">&nbsp; &nbsp; <input type="text" name="fullname" id="fullname" class="form-control form-control-user" placeholder="Your Name" value="<?php if(isset($error)){ echo htmlspecialchars($_POST['fullname'], ENT_QUOTES); } ?>" tabindex="1" required>&nbsp; &nbsp; <?php&nbsp; &nbsp; &nbsp; &nbsp; //check for any errors&nbsp; &nbsp; &nbsp; &nbsp; if (isset($infofn)){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach ($infofn as $infofn){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo '<p class="p-3 text-info">'.$infofn.'</p>';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; ?>&nbsp; &nbsp;<input id="submit" type="submit" name="submit" value="Create Account" class="btn btn-primary btn-user btn-block" tabindex="6"></form>
打开App,查看更多内容
随时随地看视频慕课网APP