0

I have a Bootstrap form and after complete the form I want remain in the same page and display a thank you message just below the submit button.

Here my HTML code

<div id="form">
<div class="row">
<div class="col-md-12"><h3>RESTA IN CONTATTO</h3>
<form id="form_members" role="form" data-toggle="validator" novalidate action="form-data.php" method="POST">
<div class="form-group">
<label for="firstname" class="control-label">Nome</label>
<input type="text" class="form-control" name="firstname" id="name" placeholder="Inserisci il Nome" required>
</div>
<div class="form-group">
<label for="lastname" class="control-label">Cognome</label>
<input type="text" class="form-control" name="lastname" id="lastname" placeholder="Inserisci il Cognome" required>
</div>
<div class="form-group">
<label for="email" class="control-label">Email</label>
<input type="email" class="form-control" name="email" id="email" placeholder="Enter the Email" data-error="Inserire email valida" required>
<div class="help-block with-errors"></div>
</div>
<div class="form-group">
<div class="checkbox">
<label>
<input type="checkbox" id="terms" required data-error="Devi essere d'accordo con i termini di condizione d'uso">Privacy
</label>
<div class="help-block with-errors"></div>
</div>
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary" name="submit" id="submit" onclick="this.form.clear()" value="submitmessage">Registrati</button>
</div>
</form>
<div id="submitmessage"></div>

and Here my php Code

<?php
$link = mysqli_connect("","","")  or die("failed to connect to server !!");
mysqli_select_db($link,"");
if(isset($_POST['submit']))
{
$errorMessage = "";
$firstname=$_POST['firstname'];
$lastname=$_POST['lastname'];
$email=$_POST['email'];

// Validation will be added here

if ($errorMessage != "" ) {
echo "<p class='message'>" .$errorMessage. "</p>" ;
}



else{
//Inserting record in table using INSERT query
$insqDbtb="INSERT INTO `test`.`members`
(`firstname`, `lastname`, `email`) VALUES ('$firstname', '$lastname', '$email')";
mysqli_query($link,$insqDbtb) or die(mysqli_error($link));
}
}
?>
j08691
  • 204,283
  • 31
  • 260
  • 272
  • 1
    [Little Bobby](http://bobby-tables.com/) says [your script is at risk for SQL Injection Attacks.](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) Learn about [prepared](http://en.wikipedia.org/wiki/Prepared_statement) statements for [MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php). Even [escaping the string](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string) is not safe! – Jay Blanchard Jun 08 '16 at 18:41
  • 1
    `mysqli_connect()` requires 4 arguments, not 3. – Jay Blanchard Jun 08 '16 at 18:42
  • 1
    If you want to stay in the same page you will need AJAX to return a success message from your PHP. – Jay Blanchard Jun 08 '16 at 18:43
  • You need to use ajax and call the api on form submit click event and get the json from the api and display it on the page. – vikram mistry Jun 08 '16 at 19:04

2 Answers2

0

First check if your insert actually happen by assigning result to a variable, ie:

$res=mysqli_query($link,$insqDbtb);

Now, below your form you can add the following php code and you can use this variable in an if statement to echo different things:

if($_POST && $res)
{/*this code is executed if form is submitted and insert worked*/
 echo ' <div class="alert alert-success" role="alert">
            Grazie per esserti registrato!
        </div>';
}
elseif ($_POST && !$res)
{ /*you can write a different message if insert did not happen*/
echo '<div class="alert alert-danger" role="alert">
           <strong>Oh no!</strong> Provaci ancora
     </div>';

 }

Note that this might only work if you use php to process the form and scripts are on the same page so you would also need to modify your form like this: <form action="register.php" method="post" accept-charset="utf-8"> (where register.php stands in for the name of your file) and the submit button like this:

<input type="submit" name="submit" value="Submit" id="submit_button" class="btn btn-primary" />

Then at the top of your page, after you have set the parameters for the connection you put the following statement:

if ($_SERVER['REQUEST_METHOD'] === 'POST') { 

and you can put all the form validation here. What happens is the first time you go to the file it will be through a GET so nothing happens, after you submit the form you access the same file but with a POST so the submission is validated.

  • Sorry but I did not understand the first part where I have to assign the variable? I'm new about php e and MySql so be patient =) By the way the Form is working but I just need to improve the Thank you message and once Submit I would like to stay in the same page. – Matteo Nasuelli Jun 08 '16 at 20:31
0

To submit the form and stay on the same page you have two possibilites:
A) As other people have suggested you use Ajax
B) The other possibility which has the same visible result for the user is that when you submit your form you go back to the same page, so the user will see the same page but different things will be displayed depending on weather he has already submitted the form or not
To achieve this second solution you can do the following:
1)In your head you establish the connection (the php you have is improvable but as a first attempt should do the job)
2) In your page you put your form with the following modification:

<form action="FILNAME.php" method="post" accept-charset="utf-8">

and

<input type="submit" name="submit" value="Submit" id="submit" class="btn btn-default" />

THis way when a user clicks the button the form is submitted to the same page.

3) Now you need to check if your page is beening accessed for the first time (ie thoruh a GET) or after the form has been submitted, so where you want your message to appear you put the following if statement:

if ($_SERVER['REQUEST_METHOD'] === 'POST') { 
}

3) Within this bracket you put all your php to validate the form and after validation your insert command

$insqDbtb="INSERT INTO `test`.`members`(`firstname`, `lastname`, `email`) VALUES ('$firstname', '$lastname', '$email')";
$res = mysqli_query($link, $insqDbtb); 

4) If the query created a new row you can write the thank you message:

if (mysqli_affected_rows($link) === 1) {echo '<div class="alert alert-success"><h3>Thanks!</h3><p>Thank you for registering! </p></div>';

If not you can write an alert instead

I learned this method from a book by Larry Ullman, you can find the scripts with examples for free here