doute3621 2014-12-12 16:04
浏览 52
已采纳

如果使用标头将用户重定向到另一个页面,如何使用表单提交错误的用户

When the user submits the form, the form information is posted to a php file and the php file redirects the user straight away to the next webpage once the form is submitted by using the header function. I have already validated the form using HTML and Javascript however the PHP has validation in it so that any errors that get past the Javascript and HTML are identified and the user is notified, however this is not possible at the minute as the user is redirected before they are notified.

How would I identify the user if the PHP locates an error? Is it necessary as will the only errors be by people who are intentionally trying to be malicious?

My code is:

<?php 

header("location: (next webpage)"); if(isset($_POST['submit'])){

$data_missing = array();    

if(empty($_POST['email_banned'])){

    // Adds name to array
    $data_missing[] = 'Email';

} else {

    // Trim white space from the name and store the name
    $email_banned = trim($_POST['email_banned']);

}

if(empty($_POST['notes'])){

    // Adds name to array
    $data_missing[] = 'Notes';

} else {

    // Trim white space from the name and store the name
    $notes = trim($_POST['notes']);

}

if(empty($data_missing)){

    require_once('mysqli_connect.php');

    $query = "INSERT INTO banned_emails (id, email_banned, created_on, notes) VALUES ( NULL, ?, NOW(), ?)";

    $stmt = mysqli_prepare($dbc, $query);


    //i Interger
    //d Doubles         
    //s Everything Else


    mysqli_stmt_bind_param($stmt, "ss", $email_banned, $notes);

    mysqli_stmt_execute($stmt);

    $affected_rows = mysqli_stmt_affected_rows($stmt);

    if($affected_rows == 1){

        echo 'Student Entered';



        mysqli_stmt_close($stmt);

        mysqli_close($dbc);

    } else {

        echo 'Error Occurred<br />';
        echo mysqli_error();

        mysqli_stmt_close($stmt);

        mysqli_close($dbc);

    }

} else {

    echo 'You need to enter the following data<br />';

    foreach($data_missing as $missing){

        echo "$missing<br />";

    }

}

}

?>

Thanks :)

  • 写回答

1条回答 默认 最新

  • duanjiebian6712 2014-12-12 16:11
    关注

    You can use $_SESSION to store errors, and retrieve them later.

    $_SESSION['errors'] = array('an error message', 'a second error message');
    

    Then in the script the user has been redirected to :

    while($err = array_shift($_SESSION['errors'])){
    ?>
        <p class='p_error'><?=$err?></p>
    <?php
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测