0

So after I used the new JavaScript. Here is all of the code. Just keep in mind their is still top body missing. Didn't put that in as it might not be relevant to my problem.

            <?php
                // define variables and set to empty values
                $nameErr = $surnameErr = $emailErr = $contact_numberErr = "";
                $name = $surname = $email = $comment = $contact_number = "";

                if ($_SERVER["REQUEST_METHOD"] == "POST") {
                   if (empty($_POST["name"])) {
                     $nameErr = "Name is required";
                   } else {
                     $name = test_input($_POST["name"]);
                     // check if name only contains letters and whitespace
                     if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
                       $nameErr = "Only letters and white space allowed"; 
                     }

                   }
                   if (empty($_POST["surname"])) {
                     $surnameErr = "Surname is required";
                   } else {
                     $surname = test_input($_POST["surname"]);
                     // check if name only contains letters and whitespace
                     if (!preg_match("/^[a-zA-Z ]*$/",$surname)) {
                       $surnameErr = "Only letters and white space allowed"; 
                     }
                   }

                   if (empty($_POST["contact number"])) {
                     $contact_numberErr = "Please provide contact details";
                   } else {
                     $contact_number = test_input($_POST["contact number"]);
                     // check if name only contains letters and whitespace
                     if (!preg_match("/^[a-zA-Z ]*$/",$contact_number)) {
                       $contact_number = "Only numbers allowed"; 
                     }
                   }

                   if (empty($_POST["email"])) {
                     $emailErr = "Email is required";
                   } else {
                     $email = test_input($_POST["email"]);
                     // check if e-mail address is well-formed
                     if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
                       $emailErr = "Invalid email format"; 
                     }
                   }

                   if (empty($_POST["comment"])) {
                     $comment = "";
                   } else {
                     $comment = test_input($_POST["comment"]);
                   }
                }

                function test_input($data) {
                   $data = trim($data);
                   $data = stripslashes($data);
                   $data = htmlspecialchars($data);
                   return $data;
                }
                ?>

                    <div class="container" align="center">
                        <div class="row featured-boxes login">
                            <div class="col-md-6">
                                <div class="featured-box featured-box-secundary default info-content">
                                    <div class="box-content">
                                        <h4 align="center">Customer Feedback</h4>
                                            <p><span class="error">* required field.</span></p>
                                        <form method="post" action="insert_customer_feedback.php" name="customer_feedback">
                                            <div class="row">
                                                <div class="form-group">
                                                    <div class="col-md-12">
                                                        <span class="error"><label>Name</label> *<?php echo $nameErr;?></span>
                                                        <input type="text" value="" class="form-control input-lg" id="txtName" name="txtName">

                                                    </div>
                                                </div>
                                            </div>

                                            <div class="row">
                                                <div class="form-group">
                                                    <div class="col-md-12">
                                                        <span class="error"><label>Surname</label> *<?php echo $surnameErr;?></span>
                                                        <input type="text" value="" class="form-control input-lg" id="txtSurname" name="txtSurname">

                                                    </div>
                                                </div>
                                            </div>

                                            <div class="row">
                                                <div class="form-group">
                                                    <div class="col-md-12">
                                                        <span class="error"><label>E-mail</label> *<?php echo $emailErr;?></span>
                                                        <input type="text" value="" class="form-control input-lg" id="txtEMail" name="txtEmail">

                                                    </div>
                                                </div>
                                            </div>

                                            <div class="row">
                                                <div class="form-group">
                                                    <div class="col-md-12">
                                                        <span class="error"><label>Contact Number</label> *<?php echo $contact_numberErr;?></span>
                                                        <input type="text" value="" class="form-control input-lg" id="txtContact_number" name="txtContact_number">

                                                    </div>
                                                </div>
                                            </div>

                                            <div class="row">
                                                <div class="form-group">
                                                    <div class="col-md-12">
                                                        <label>Comment</label>
                                                        <input type="text" value="" class="form-control input-lg" id="txtComment" name="txtComment">
                                                        </div>
                                                    </div>
                                                <input type="button" value="Submit" class="btn btn-primary pull-right push-bottom" onClick="Confirm(this.form)">
                                            </div>
                                        </div>
                                    </form>

       <script type="text/javascript">
        function Confirm(form){
        alert("Thank you for your feedback.");
        form.submit();
        window.location.href = "/index.php";
        }
       </script>

For some reason it does not want to load my data into MySQL when having this peace of JavaScript (window.location.href = "/index.php";) added.

3 Answers3

0

When you do the form.submit() you tell the form to perform the action which is stated in the forms action attribute. Thus it will automatically be redirected to whatever is set there.

Example:

<form action="submit.php" method="POST">

You code will redirect the user to the submit.php after he dismisses the alert message. When redirected to the submit.php you will have to handle a redirect from there on.

This is of course unless you are using AJAX to post the form or something like that.

Then you can just add:

window.location.href = "/index.html"; 

Although, in that case, it would better be placed in the success-function of the AJAX call:

function Confirm(form){alert("Thank you for your feedback.");form.submit(); window.location.href = "/index.html";}
SharpC
  • 6,974
  • 4
  • 45
  • 40
Ole Haugset
  • 3,709
  • 3
  • 23
  • 44
  • After adding that JavaScript it now disables the whole form and does not pull in the data from the form to MySQL? what can be hampering it, because as soon as I remove that peace of code it pull the data into the database. –  Oct 02 '14 at 17:54
  • Without knowing your code thats near impossible to answer. Please update your question to include it – Ole Haugset Oct 02 '14 at 18:38
  • I have updated the code as per request. –  Oct 02 '14 at 18:47
0

you can simply do it using

function Confirm(form){alert("Thank you for your feedback.");form.submit();
window.location = "location";
}
Ekansh Rastogi
  • 2,418
  • 2
  • 14
  • 23
0

If you want to do the redirect in JavaScript it would be:

window.location.replace("yourwebsite.com/index.php");

If you wanted to do it in PHP, then it would be:

header('Location:index.php');
Harry12345
  • 1,144
  • 6
  • 20
  • 47