Jump to content

A self submitting form


psonawane

Recommended Posts

Hi all below is my php code. its a contact form and all i want to do is to display thank you message below the submit button. Please help me what changes should I make?

<!DOCTYPE html>
<?php
if (isset($_POST['submit'])) {
    mysql_connect("localhost", "root", "pranit");
    mysql_select_db("simtech");
    $cname = $_POST['cname'];
    $cont_no = $_POST['cont'];
    $email = $_POST['email'];
    $mess = $_POST['mess'];

    $sql = "insert into gen_enq(cname,cont_no,email_id,message)values('$cname','$cont_no','$email','$mess')";
    if (mysqli_connect_errno()) {
        echo 'Failed To connect';
    }
    $query = mysql_query($sql);
    if ($query) {
        header("Location: contact");        
    } else {
        die(mysql_error());
    }
}
?>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Software Development Company - Malegaon, Website Designing Company</title>
        <link rel="stylesheet" href="main.css" type="text/css">
        <link rel="icon" href="media/simtechlogo.png" type="image/png">
        <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
        <meta name="keywords" content="WOW Slider, Slideshow jQuery Free Download, jQuery Slideshow Background" />
        <meta name="description" content="WOWSlider created with WOW Slider, a free wizard program that helps you easily generate beautiful web slideshow" />
        <!-- Start WOWSlider.com HEAD section -->
        <link rel="stylesheet" type="text/css" href="media/engine1/style.css" />
        <script type="text/javascript" src="media/engine1/jquery.js"></script>
        <!-- End WOWSlider.com HEAD section -->

        <SCRIPT TYPE="text/javascript"> 
            <!-- 
            //Disable right click script 
            //visit http://www.rainbow.arch.scriptmania.com/scripts/ 
            var message="Sorry, right-click has been disabled"; 
            /////////////////////////////////// 
            function clickIE() {if (document.all) {(message);return false;}} 
            function clickNS(e) {if 
                (document.layers||(document.getElementById&&!document.all)) { 
                    if (e.which==2||e.which==3) {(message);return false;}}} 
            if (document.layers) 
            {document.captureEvents(Event.MOUSEDOWN);document.onmousedown=clickNS;} 
            else{document.onmouseup=clickNS;document.oncontextmenu=clickIE;} 
            document.oncontextmenu=new Function("return false") 
            // --> 
        </SCRIPT> 
        <SCRIPT TYPE="text/javascript"> 
            <!-- 
            //Disable select-text script (IE4+, NS6+)
            //visit http://www.rainbow.arch.scriptmania.com/scripts/ 
            /////////////////////////////////// 
            function disableselect(e){
                return false
            } 
            function reEnable(){
                return true
            } 
            //if IE4+
            document.onselectstart=new Function ("return false") 
            //if NS6
            if (window.sidebar){
                document.onmousedown=disableselect
                document.onclick=reEnable
            }
            // -->
        </SCRIPT>
    </head>
    <body>
        <script type="text/javascript">
 
            $(document).ready(function(){
 
                $('body').fadeIn(1000);
 
            });
 
        </script>
    <center>
        <div class="wrapper">           
            <center>
                <p>
                    <img src="media/sim1.png" height="200" width="400">
                </p>                                  
            </center>
            <div class="container">
                <div class="navMenu">
                    <ul>
                        <li><a href="index.php"><img src="media/home.png"></a></li>
                        <li><a href="about.php">About Us</a></li>
                        <li><a href="#">Services</a></li>
                        <li><a href="#">Portfolio</a></li>
                        <li><a href="#">Our Products</a></li>
                        <li><a href="#">Downloads</a></li>
                        <li><a href="#">Contact Us</a></li>
                    </ul>
                </div>                 
                <div class="social">
                    <ul>
                        <li><a href="#"><img src="media/facebook.png"></a></li>
                        <li><a href="#"><img src="media/twitter.png"></a></li>
                        <li><a href="#"><img src="media/linkedin.png"></a></li>
                    </ul>
                </div>
                <div class="contss">
                    <ul>
                        <li style="border-right: 1px solid #555555;">
                            <img src="media/Graphic2.jpg"><hr>
                            Vrindavan Park, Sayali Appt. 60 Feet Road, <br>
                            Near BSNL Exchange Office, Malegaon - 423203<br>
                            Contact: 02554 - 250135 / +91 - 9096812185.                            
                        </li>
                        <li style="border-radius: 10px; background-image: url(media/menu.png); background-size: cover; margin-left: 30px;-webkit-box-shadow: 0 8px 6px -6px #00ADC5; -moz-box-shadow: 0 8px 6px -6px #00ADC5; box-shadow: 0 8px 6px -6px #00ADC5; ">
                            <h3>Contact Us</h3>
                            <form action="contact" method="post">
                                <table>
                                    <tr>
                                        <th>Your Name:</th>
                                        <td><input type="text" name="cname"></td>
                                    </tr>
                                    <tr>
                                        <th>Your Contact No:</th>
                                        <td><input type="text" name="cont"></td>
                                    </tr>
                                    <tr>
                                        <th>Your Email:</th>
                                        <td><input type="text" name="email"></td>
                                    </tr>
                                    <tr>
                                        <th>Your Message:</th>
                                        <td><textarea name="mess"></textarea></td>
                                    </tr>
                                    <tr>
                                        <th></th>
                                        <td><button type="submit" name="submit">&checkmark; Submit</button></td>
                                    </tr>
                                </table>
                            </form>
                            <br>                            
                        </li>
                    </ul>
                </div>
            </div>
            <br><br><br><br>
            <div class="footer">  
                <b>&COPY; Copyright 2013 - 14 : All Rights Reserved</b><br><hr>
                <table>
                    <tr>
                        <th>
                            <a href="#">Home</a><br><br>
                            <a href="#">About Us</a><br><br>
                            <a href="#">Software Development</a><br><br>
                            <a href="#">Web Designing</a><br>
                        </th>
                        <th>
                            <a href="#">Web Application Development</a><br><br>
                            <a href="#">Web Hosting</a><br><br>
                            <a href="#">Search Engine Optimization</a><br><br>
                            <a href="#">Android Development</a><br>
                        </th>
                        <th>
                            <a href="#">Softwares</a><br><br>
                            <a href="#">Web Sites</a><br><br>
                            <a href="#">Web Applications</a><br><br>
                            <a href="#">Software Products</a><br>
                        </th>
                        <th>                       
                            <a href="#">Web Application Products</a><br><br>
                            <a href="#">Android Products</a><br><br>
                            <a href="#">Broucher Downloads</a><br><br>
                            <a href="#"></a><br>
                        </th>                    
                    </tr>
                </table><br>
            </div>
    </center>
</body>
</html>

Link to comment
Share on other sites

a self submitting form? is that like a self fulfilling prophecy?

 

Basicly you need to take the form action out, change the button to type=button, add an onclick to the button that wiill triger an AJAX script that submits the from variables to the contact page, add an echo with your message of choce to the contact page, have the AJAX call read this on success and append the message to your contss div.

 

it's that simple.

Link to comment
Share on other sites

Hi trq. All I've tried is to echo out after submitting the form and before redirecting user to the same page. But it prints that thank you message on the top left corner and I did not want it to be. I wanted it to be printed exactly below the submit buttons. I've removed the echo for the code mentioned above. I wanted help about the same. But as muddy-funster says let me try Ajax code. Am a novice programmer. I've been programming jsp and Servlets but now learning php. Thanks for your helps.

Link to comment
Share on other sites

it's that simple.

Trying to add in a bunch of JS and Ajax only makes the form needlessly complex. All that needs done is to set a variable after submit which then triggers the display of said message.

 

<?php

if (isset($_POST['submit'])){
   //Do all your processing

   //On Success, redirect to self with a variable to indicate success.
   header('Location: contact.php?success=1');
   exit;
}
?>
//..html
<tr>
  <th></th>
  <td><button type="submit" name="submit">&checkmark; Submit</button></td>
</tr>
<?php if (isset($_GET['success'])): ?>
<tr>
  <td colspan="2">Your message has been sent successfully!</td>
</tr>
<?php endif; ?>
Link to comment
Share on other sites

Although there are numerous problems with your page, like no validation on fields, no-compatibility with Firefox etc. But I have rewritten the PHP code for you. I haven't done anything fancy but made all the fields compulsory and also a string will be echoed out to the user indication whether the form was submitted or not.  Also added the possibility to email the form details to the site administrator. You should also consider changing database username and password, database as they are exposed to the world, you can get into a lot of trouble.

 

Hope it will help you

Regards,

    <!DOCTYPE html>
    <html>
    <?php

            if ($_SERVER['REQUEST_METHOD'] == 'POST' &&
                !empty($_POST['cname']) &&
                !empty($_POST['cont']) &&
                !empty($_POST['email']) &&
                !empty($_POST['mess']) &&
                isset($_POST['submit'])
                ) {

                $host = 'localhost';
                $user = 'root';
                $pass = 'pranit';
                $db = 'simtech';

                $conn = @mysqli_connect($host, $user, $pass, $db);

                if(!$conn) {die ("Can't Connect");}

                $name = $_POST['cname'];
                $cont = $_POST['cont'];
                $email = $_POST['email'];
                $mess = $_POST['mess'];
                $db_table = 'gen_enq';

                $query = "INSERT INTO 
                                {$db_table}
                                 (cname, cont_no, email_id, message)
                            VALUES
                                    ('{$name}', '{$cont}', 
                                     '{$email}','{$mess}')
                        ";

                $results = mysqli_query($conn, $query);

                if (!$results) 
                { 
                    die("Can't Process the Form right now.");

                } else {

                    $admin_email = 'your_email@xyz.com';
                    $email_subject = 'New Form Submitted';
                    $email_message = " {$name} has contacted you via your website form. His email address is {$email} and his message is as follows \" {$mess} \" 
                                     ";

                    /* Remove (//) from the line below and adjust the above variables to use the email functionality to the Websiste Administrator. */

                  // mail ($admin_email, $email_subject, $email_message);

                    $status = "Your Information is submitted, We'll contact you shortly. Thanks!";
                }
            }
            else {

                    $status = "Your Inforamtion was not submitted. All fields are required.";
            }
?>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Software Development Company - Malegaon, Website Designing Company</title>
    <link rel="stylesheet" href="main.css" type="text/css">
    <link rel="icon" href="media/simtechlogo.png" type="image/png">
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
    <meta name="keywords" content="WOW Slider, Slideshow jQuery Free Download, jQuery Slideshow Background" />
    <meta name="description" content="WOWSlider created with WOW Slider, a free wizard program that helps you easily generate beautiful web slideshow" />
    <!-- Start WOWSlider.com HEAD section -->
    <link rel="stylesheet" type="text/css" href="media/engine1/style.css" />
    <script type="text/javascript" src="media/engine1/jquery.js"></script>
    <!-- End WOWSlider.com HEAD section -->
     
    <SCRIPT TYPE="text/javascript">
    <!--
    //Disable right click script
    //visit http://www.rainbow.arch.scriptmania.com/scripts/
    var message="Sorry, right-click has been disabled";
    ///////////////////////////////////
    function clickIE() {if (document.all) {(message);return false;}}
    function clickNS(e) {if
    (document.layers||(document.getElementById&&!document.all)) {
    if (e.which==2||e.which==3) {(message);return false;}}}
    if (document.layers)
    {document.captureEvents(Event.MOUSEDOWN);document.onmousedown=clickNS;}
    else{document.onmouseup=clickNS;document.oncontextmenu=clickIE;}
    document.oncontextmenu=new Function("return false")
    // -->
    </SCRIPT>
    <SCRIPT TYPE="text/javascript">
    <!--
    //Disable select-text script (IE4+, NS6+)
    //visit http://www.rainbow.arch.scriptmania.com/scripts/
    ///////////////////////////////////
    function disableselect(e){
    return false
    }
    function reEnable(){
    return true
    }
    //if IE4+
    document.onselectstart=new Function ("return false")
    //if NS6
    if (window.sidebar){
    document.onmousedown=disableselect
    document.onclick=reEnable
    }
    // -->
    </SCRIPT>
 
    </head>
    <body>
    <script type="text/javascript">
    $(document).ready(function(){
    $('body').fadeIn(1000);
    });
    </script>
    <center>
    <div class="wrapper">
    <center>
    <p>
    <img src="media/sim1.png" height="200" width="400">
    </p>
    </center>
    <div class="container">
    <div class="navMenu">
    <ul>
    <li><a href="index.php"><img src="media/home.png"></a></li>
    <li><a href="about.php">About Us</a></li>
    <li><a href="#">Services</a></li>
    <li><a href="#">Portfolio</a></li>
    <li><a href="#">Our Products</a></li>
    <li><a href="#">Downloads</a></li>
    <li><a href="#">Contact Us</a></li>
    </ul>
    </div>
    <div class="social">
    <ul>
    <li><a href="#"><img src="media/facebook.png"></a></li>
    <li><a href="#"><img src="media/twitter.png"></a></li>
    <li><a href="#"><img src="media/linkedin.png"></a></li>
    </ul>
    </div>
    <div class="contss">
    <ul>
    <li style="border-right: 1px solid #555555;">
    <img src="media/Graphic2.jpg"><hr>
    Vrindavan Park, Sayali Appt. 60 Feet Road, <br>
    Near BSNL Exchange Office, Malegaon - 423203<br>
    Contact: 02554 - 250135 / +91 - 9096812185.
    </li>
    <li style="border-radius: 10px; background-image: url(media/menu.png); background-size: cover; margin-left: 30px;-webkit-box-shadow: 0 8px 6px -6px #00ADC5; -moz-box-shadow: 0 8px 6px -6px #00ADC5; box-shadow: 0 8px 6px -6px #00ADC5; ">
    <h3>Contact Us</h3>
    <form action="" method="post">
    <table>
    <tr>
    <th>Your Name:</th>
    <td><input type="text" name="cname"></td>
    </tr>
    <tr>
    <th>Your Contact No:</th>
    <td><input type="text" name="cont"></td>
    </tr>
    <tr>
    <th>Your Email:</th>
    <td><input type="text" name="email"></td>
    </tr>
    <tr>
    <th>Your Message:</th>
    <td><textarea name="mess"></textarea></td>
    </tr>
    <tr>
    <th></th>
    <td><button type="submit" name="submit">&checkmark; Submit</button>
    </td>
    </tr>
    </table>
    </form>
    <?php 
            if ($_SERVER['REQUEST_METHOD'] == 'POST' &&
              isset($status)){

                echo $status;
            }
    ?>
    <br>
    </li>
    </ul>
    </div>
    </div>
    <br><br><br><br>
    <div class="footer">
    <b>&COPY; Copyright 2013 - 14 : All Rights Reserved</b><br><hr>
    <table>
    <tr>
    <th>
    <a href="#">Home</a><br><br>
    <a href="#">About Us</a><br><br>
    <a href="#">Software Development</a><br><br>
    <a href="#">Web Designing</a><br>
    </th>
    <th>
    <a href="#">Web Application Development</a><br><br>
    <a href="#">Web Hosting</a><br><br>
    <a href="#">Search Engine Optimization</a><br><br>
    <a href="#">Android Development</a><br>
    </th>
    <th>
    <a href="#">Softwares</a><br><br>
    <a href="#">Web Sites</a><br><br>
    <a href="#">Web Applications</a><br><br>
    <a href="#">Software Products</a><br>
    </th>
    <th>
    <a href="#">Web Application Products</a><br><br>
    <a href="#">Android Products</a><br><br>
    <a href="#">Broucher Downloads</a><br><br>
    <a href="#"></a><br>
    </th>
    </tr>
    </table><br>
    </div>
    </center>
    </body>
    </html>
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.