简体   繁体   English

如何将用户重定向到同一HTML页面,但显示错误消息[php]?

[英]How to redirect user to the same HTML page, but with an error message [php]?

I have a form that has two inputs. 我有一个具有两个输入的表单。 After the user fills the form and clicks Submit, these inputs are compared with a MySQL table data. 用户填写表单并单击Submit之后,会将这些输入与MySQL表数据进行比较。

What I want to do is: when the user clicks Submit in the form, it redirects the user to THE SAME PAGE , but with a Message. 我想做的是:当用户单击表单中的Submit时,它将用户重定向到THE SAME PAGE ,但带有一条消息。

If the user's input matches the data in the table, then the Message should be 如果用户的输入与表中的数据匹配,则消息应为

"Your input matches the database" “您的输入与数据库匹配”

If the user's input doesn't match, the message should be the opposite. 如果用户输入不匹配,则消息应相反。

This is my code so far. 到目前为止,这是我的代码。 Again, what I need to know is, how do I remain IN THE SAME PAGE, after the submit button is pressed. 同样,我需要知道的是,在按下提交按钮之后,如何保持在同一页面中。

$sql1 = " SELECT `Numero Cedula` FROM Teste WHERE `Numero Cedula`=$numero_cedula
and `Id`=$integer "   ;

$objGet = mysqli_query($con, $sql1);


if( mysqli_num_rows($objGet) > 0 ) {
    echo "Your input matches the database";
} else {
    echo "Your input does not match the database";` 
}

Alternatively, you could devise (some kind of) a flash session behavior on this case. 或者,您可以针对这种情况设计(某种)闪存会话行为。 Upon submission, set a message in the session. 提交后,在会话中设置一条消息。 Then echo it after redirection, then just simply unset it. 然后在重定向后回显它,然后只需取消设置即可。

Rough example: 粗略的例子:

session_start();
$current_url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";

if(isset($_POST['submit'])) { // upon submission
    $sql1 = "SELECT `Numero Cedula` FROM Teste WHERE `Numero Cedula` = $numero_cedula and Id = $integer";
    $objGet = mysqli_query($con, $sql1);
    if( mysqli_num_rows($objGet) > 0 ) {
        $_SESSION['msg'] = "Your input matches the database";
    } else {  
         $_SESSION['msg'] = "Your input does not match the database";  
    }

    header('Location: ' . $current_url);
     exit;
}


if(isset($_SESSION['msg'])) {
    echo '<div class="message">' . $_SESSION['msg'] . '</div>';
    unset($_SESSION['msg']);
}

Sidenote: Use prepared statements as well. 旁注:也使用准备好的陈述。

Don't redirect use AJAX. 不要重定向使用AJAX。 Heres an example, you could expand this to include fields which are wrong (not advised) or to include number of tries left etc.. whatever you need. 这是一个示例,您可以将其扩展为包含错误的字段(不建议使用)或包含剩余尝试次数等。

AJAX; AJAX;

$.post('yourUrl', $('yourForm').serialize(), function (data) {
$result = parseJSON(data);
//append your message somewhere
$('body').append($result.message);
$('body').append('You have ' + $result.numberOfTries + ' Left');   
}

PHP; PHP;

if (//your validation is good!)
{  
 //redirect to your stuffs
}
else
{ 
  echo json_encode(array(
                         'successful'=>false, 
                         'message'=>'Sorry, Those Details Are Incorrect',
                         'numberOfTries'=>$triesLeft,
                         )
                   ); 
};
if( mysqli_num_rows($objGet) > 0 ) {
    echo "<script>alert('Your input matches the database')</script>";
}
else {
    echo "<script>alert('Your input does not match the database')</script>";` 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM