-1

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";` 
}
Crisoforo Gaspar
  • 3,740
  • 2
  • 21
  • 27
manuel mourato
  • 801
  • 1
  • 12
  • 36
  • 1
    possible duplicate of [Check if form submitted and do something](http://stackoverflow.com/questions/18837352/check-if-form-submitted-and-do-something) – CBroe Oct 18 '14 at 14:07
  • @CBroe , It's not the same. I already know how to check if it was submitted. My doubt is: how do I redirect To the Same Page, after I submit. – manuel mourato Oct 18 '14 at 14:14
  • Oh, so you mean the [http://en.wikipedia.org/wiki/Post/Redirect/Get](http://en.wikipedia.org/wiki/Post/Redirect/Get) pattern … – CBroe Oct 18 '14 at 14:18
  • If you wish to remain in the same page after query, you can either use Ajax or set your form to `action=""` – Funk Forty Niner Oct 18 '14 at 14:25

3 Answers3

1

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.

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
Kevin
  • 41,694
  • 12
  • 53
  • 70
  • OP didn't properly indent the code. It was re-edited and I changed it to what it should be. *Crossing fingers* ;) – Funk Forty Niner Oct 18 '14 at 14:24
  • @Fred-ii- oh okay, i see, the column name, thanks, actually while tying this answer i don't know what to make of that space if its really part of it. thanks fred – Kevin Oct 18 '14 at 15:01
  • You're welcome. Yeah, I know that because OP posted another question yesterday where OP was not using backticks for the column name containing a space http://stackoverflow.com/q/26435616/ - Which I gave an answer for; that's why I remember. – Funk Forty Niner Oct 18 '14 at 15:02
  • 1
    @Fred-ii- ahh, a continuation. okay i understand now, backticks indeed – Kevin Oct 18 '14 at 15:06
1

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,
                         )
                   ); 
};
Steve_B19
  • 538
  • 3
  • 10
0
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>";` 
}