简体   繁体   中英

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.

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.

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. 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;

$.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;

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>";` 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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