如何将消息表格PHP页面输入HTML页面



我想知道如何将消息从我的PHP页面获取到我的HTML消息。当用户成功创建用户时,我想从PHP到HTML获取"成功"消息。

html:

<form class="" action="insertUser.php" method="post">
    <input id="formInput" name="firstname" value="" placeholder="Firstname"> <br>
    <input id="btn" type="submit" name="" value="Create">
    <h2><?php echo $result ?></h2>
</form>

php:

$firstname = mysqli_real_escape_string($conn, $_REQUEST['firstname']);
$stmt = "INSERT INTO user (firstname) VALUES('$firstname')";
if(empty($firstname) ){
    echo $result = "added";
    header('Location: index.php', true, 303);
    exit;
} else {
    if (mysqli_query($conn, $stmt)) {
        echo "erro";
        header('Location: index.php', true, 303);
        exit;
    }else {
        echo "Error: " .mysqli_error($conn);
    }
}

我不包括连接文件。该功能有效,这是唯一不起作用的回声部分。我收到错误消息"致命错误"

您可以通过将消息作为get var附加到标题URL上来做到这一点,这有点不合同地

$stmt = "INSERT INTO user (firstname) VALUES('$firstname')";
  if (mysqli_query($conn, $stmt)) {
    $msg = "success";
    header('Location: index.php?msg='.$msg);
        exit;
  }else {
        $error = "erro";
        header('Location: index.php?msg='.$error);
  }

在您的html中,您会使用以下方式调用get var:

<h2><?php echo $_GET['msg'] ?></h2>

,或者您可以将错误消息存储在会话中:

     //include at top of your script
     session_start();

$stmt = "INSERT INTO user (firstname) VALUES('$firstname')";
  if (mysqli_query($conn, $stmt)) {
    $msg = "success";
    $_SESSION['msg'] = "Success message"
    header('Location: index.php');
  }else {
        $_SESSION['msg'] = "Error message"
        header('Location: index.php');
  }

以及错误消息的收件人PHP文件,还包括session_start()和简单的回波$_SESSION['error'] So

在您的html中,您会使用以下方式调用get var:

<h2><?php echo $_SESSION['msg'] ?></h2>

您将用户重定向到index.php。

您有两个将消息传递给index.php

的选项
  1. 您可以使用查询参数传递消息。在这种情况下,更改
header('Location: index.php', true, 303);

to

header('Location: index.php?response=success', true, 303);

回声$结果

to

echo $ _get ['响应']

注意:此情况是XSS俯卧,请在回声之前使用Appt Sanitized。

  1. 使用会话设置消息。

SQL可能容易受到SQL injection的影响,因此在处理类似的用户提供的数据时,您应该使用prepared statements。已经有人建议使用会话变量是一种方法 - 这就是我可能做到的。

如果数据库查询存在某种错误,则警惕显示过多的信息 - 适用于开发但不在生产中。

<?php
    session_start();
    $_SESSION['useradded']='Unable to add user';
    /* include db connection */
    try{
        $firstname=filter_input( INPUT_POST, 'firstname', FILTER_SANITIZE_STRING );
        if( $firstname ){
            $sql='insert into `user` ( `firstname` ) values( ? )';
            $stmt=$conn->prepare( $sql );
            if( !$stmt )throw new Exception('Failed to prepare sql');
            $stmt->bind_param( 's', $firstname );
            $result=$stmt->execute();
            if( $result && $stmt->affected_rows==1 ){
                $_SESSION['useradded']='User added';
            } else {
                throw new Exception('error adding user');
            }
            $stmt->free_result();
            $stmt->close();
            $conn->close();
            exit( header('Location: index.php') );
        } else {
            throw new Exception('firstname is required');
        }
    }catch( Exception $e ){
        $conn->close();
        $_SESSION['error']=$e->getMessage();
        exit( header('Location: index.php') );
    }
?>

<form action='insertUser.php' method='post'>
    <input id='formInput' name='firstname' placeholder='Firstname'> <br>
    <input type='submit' value='Create'>
    <?php
        if( $_SERVER['REQUEST_METHOD']=='POST' ){
            if( !empty( $_SESSION['error'] ) ) {
                printf('<h2 class="error">%s</h2>', $_SESSION['error'] );
                unset( $_SESSION['error'] );
            } else {
                printf('<h2 class="success">%s</h2>', !empty( $_SESSION['useradded'] ) ? $_SESSION['useradded'] : '' );
                unset( $_SESSION['useradded'] );
            }
        }
    ?>
</form>

最新更新