0

I know what you're thinking of commenting, "this is a duplicate answer" - this isn't completely true, I've searched high to low to find out how to do it but I can't seem to find anything.

The question, how can I replace a form with a thank you message? This is different because I my form submits to a php page which contains sql connecting and adding the form DATA to a database in MYSQL. When submitting the data it adds to the database. But it stays on the php page.

Heres my code:

<div class="index-contant">
  <div class="margin">
   <h2>GET A FREE QUOTE</h2>
   <div class="row" style="margin-left:auto !important; margin-right:auto !important; display:block !important;">
    <form name="contactForm" action="php/sql_insert.php" method="GET">
     <div class="col-sm-4 col-md-4">
      <input type="text" class="form-control" name="name" id="name" placeholder="*Name" id="name"required>
       <br>
      <input type="email" class="form-control" name="email" id="email" placeholder="*Email" id="email"required>
       <br>
      <input type="text" class="form-control" name="subject" id="subject" placeholder="*Subject" id="subject" required>
     </div>
     <div class="col-sm-8 col-md-8">
      <textarea class="form-control textarea-resize" rows="7" id="message" name="message" placeholder="*Message" required></textarea>
     </div>
      <p style="padding:0 !important; margin:0 !important"> &nbsp </p>
     <div style="margin-left:15px; margin-right:15px;">
      <input type="submit" class="button form-submit" value="SEND">
     </div>
    </form>
   </div>
  </div>
 </div>

<?php

include('sql_connect.php');

mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

$name=$_POST['name'];
$email=$_POST['email'];
$subject=$_POST['subject'];
$message=$_POST['message'];


$sql="INSERT INTO $tbl_name(name, email, subject, message)VALUES('$name', '$email', '$subject', '$message')";
$result=mysql_query($sql);
?> 

<?php 
// close connection 
mysql_close();
?>

I want to be able to send the data to MySQL and stay on the same page. And then replace the form with a thank you message. I have tried using GET and POST, neither of them keep me on the same page.

  • 1
    you'll need to use javascript with ajax to do this, and i can assure you there are tons of duplicate questions for exactly this on this site alone – Mike Corcoran Oct 14 '15 at 18:59
  • think you could point me in the right direction? – Jacob Punton Oct 14 '15 at 19:01
  • sure, are you willing to use or already using jquery on the page? or did you want to use pure javascript? – Mike Corcoran Oct 14 '15 at 19:08
  • No i would prefer to use full js, however, i have bootstrap jquery connected. – Jacob Punton Oct 14 '15 at 19:17
  • Possible duplicate of [Use jQuery to replace form with 'Thank You'?](http://stackoverflow.com/questions/21082372/use-jquery-to-replace-form-with-thank-you) – Mike Corcoran Oct 14 '15 at 19:18
  • an example of pretty much the same thing you want to do can be seen as an answer [here](http://stackoverflow.com/questions/21082372/use-jquery-to-replace-form-with-thank-you). you will probably need to tweak sql_insert.php to return some sort of json response indicating success or failure so you know whether or not to show the thank you message – Mike Corcoran Oct 14 '15 at 19:20
  • Your script is vulnerable to sql inject. – jewelhuq Oct 14 '15 at 20:57

1 Answers1

0

index.php (Just took an example of index.php. change your name here)

<div class="index-contant">
    <div class="margin">
        <?
        if(!$_GET['SuccessMessage']=="Success")
        <?}?>
        <form>
            .
            .
            //Your Code
            .
            .
        </form>
        <?}
        else
        {?>
        <div class="alert alert-success" role="alert">
            Success
        </div>
        <?}?>
    </div>
</div>  

sql_insert.php

<?php

include('sql_connect.php');

mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
..
//Your Code
.
.
mysql_close();

header("location:index.php?SuccessMessage=Success"); 
//Put this in your last line to redirect it to previous page.
//I've used index.php, you write your page name
?>
Nana Partykar
  • 10,556
  • 10
  • 48
  • 77
  • I don't want a .php extension for my homepage - bad SEO. But good example. – Jacob Punton Oct 14 '15 at 19:15
  • Actually just checked, the SEO is fine. However, tried the index.php snippet and it isn't closed properly somewhere. Would you be able to edit this please? Also, FYI, I'm a designer, not a developer. – Jacob Punton Oct 14 '15 at 19:33
  • 1
    @JacobPunton if you want SEO-friendly URLs, you might look into using a PHP micro-framework that supports [front controllers](https://en.wikipedia.org/wiki/Front_Controller_pattern). – alexw Oct 14 '15 at 21:57