Corne Corne - 21 days ago 5
Javascript Question

Redirect after clicking submit

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.

Answer

you can simply do it using

function Confirm(form){alert("Thank you for your feedback.");form.submit();
window.location = "location";
}