How to retain Form data After Incomplete Form Submission - javascript

This is my code,
PHP
if(isset($_POST['submit'])){
if (empty($_POST["name"])) {
$NameErr = "Name is required";
} else {
$name = Test_User_Input($_POST["name"]);
$name = mysqli_real_escape_string($connection , $name);
}
if (empty($_POST["contact"])) {
$ContactErr = "Contact is required";
} else {
$contact = Test_User_Input($_POST["contact"]);
$contact = mysqli_real_escape_string($connection , $contact);
}
if (empty($_POST["email"])) {
$EmailErr = "Email is required";
} else {
$email = Test_User_Input($_POST["email"]);
$email = mysqli_real_escape_string($connection , $email);
}
if (empty($_POST["pan"])) {
$PanErr = "PAN is required";
} else {
$pan = Test_User_Input($_POST["pan"]);
$pan = mysqli_real_escape_string($connection , $pan);
}
if (empty($_POST["dob"])) {
$DobErr = "DOB is required";
} else {
$dob = Test_User_Input($_POST["dob"]);
$dob = mysqli_real_escape_string($connection , $dob);
}
if (empty($_POST["gender"])) {
$GenderErr = "Gender is required";
} else {
$gender = Test_User_Input($_POST["gender"]);
$gender = mysqli_real_escape_string($connection , $gender);
}
if (!empty($name) && !empty($contact) && !empty($email) && !empty($pan) &&
!empty($dob) && !empty($gender){
$query = "INSERT INTO form ( name, contact, email, pan, birthday,
gender )VALUES ('$name' , '$contact' , '$email' , '$pan' , '$dob' ,
'$gender')";
}
if ($insert_query) {
echo "Form Filled";
} else {
echo "Please Fill the form";
}
}
function Test_User_Input($Data)
{
return $Data;
}
HTML
<form action="" autocomplete="off" method="post">
<div class="col-md-6 col-sm-6 col-xs-12 marB20">
<div class="form-group-kyc">
<label>Full Name:</label><span class = "error_msg"><?php echo $NameErr; ?></span>
<input type="text" name="name" id="name" placeholder="Full Name" class="form-control-kyc" value="<?php echo isset($_POST['name']) ? $_POST['name'] :' '?> ">
</div>
</div>
<div class="col-md-6 col-sm-6 col-xs-12 marB20">
<div class="form-group-kyc">
<label>Contact Number:</label><span class = "error_msg"><?php echo $ContactErr; ?></span>
<input type="text" name="contact" id="contact" placeholder="Contact Number" class="form-control-kyc" value="<?php echo isset($_POST['contact']) ? $_POST['contact'] : '' ?> ">
</div>
</div>
<div class="col-md-6 col-sm-6 col-xs-12 marB20">
<div class="form-group-kyc">
<label>Email Id:</label><span class = "error_msg"><?php echo $EmailErr; ?></span>
<input type="email" name="email" id="email" placeholder="Email id" class="form-control-kyc" value="<?php echo isset($_POST['email']) ? $_POST['email'] : '' ?> ">
</div>
</div>
<div class="col-md-6 col-xs-12 col-sm-6 marB20">
<div class="form-group-kyc">
<label>PAN:</label><span class = "error_msg"><?php echo $PanErr; ?></span>
<input type="text" name="pan" id="pan" placeholder="PAN Card No." class="form-control-kyc" value="<?php echo isset($_POST['pan']) ? $_POST['pan'] : '' ?> ">
</div>
</div>
<div class="col-md-6 col-xs-12 col-sm-6 marB20">
<div class="form-group-kyc">
<label>DOB:</label><span class = "error_msg"><?php echo $DateErr; ?></span>
<input type="date" name="dob" id="dob" placeholder="DOB." class="form-control-kyc" value="">
</div>
</div>
<div class="col-md-6 col-xs-12 col-sm-6 marB20">
<div class="form-group-kyc">
<label>Gender:</label> <span class = "error_msg"><?php echo $GenderErr; ?></span>
<select name ="gender" id = "gender" class="form-control-kyc">
<option value="male"> Male </option>
<option value="female"\> Female </option>
</select>
</div>
</div>
<div class="col-md-12 marT30">
<div class="form-group-kyc">
<input type="submit"
id="submit" name = "submit" class="itg-button" value="Submit">
</div>
</form>
I want to Know, when the user fails to submit the form, how can I retain the form values, so user don't have to refill it.
Also if user forgets to fill one value , How to focus on that particular Field.?
ALSO
when the form is submitted and when I refresh The page, the form is again Refilled and data is sent again.
How can I prevent that?
One option is to redirect to another page after the submission, but again when the user will press back button, the form data is sent again.
Is there anything here which can be done?

For retain the filled values of the fields.
<input type="text" name="name" id="name" placeholder="Full Name" class="form-control-kyc" value="<?php echo if(isset($_POST['$name'])? $_POST['name'] : ''; ) ?>">
To avoid the auto resubmission of the data you can check whether the submit button pressed or not, if not the form data will not be added to Database and redirect it to index.php
<?php
if(isset($_POST['your-submit-button-name'])){
//within this curly brackets add your codes
}else{
header('Location: index.php');
die();
}
?>

If you want to keep the values stored you can simply echo the post value back as shown below.
<input type="text" name="name" id="name" placeholder="Full Name" class="form-control-kyc" value="<?php echo isset($_POST['name']) ? $_POST['name'] : '' ?>">

Related

How can I redirect users to different pages after inserting into the database using Sweet Alert in PHP?

I am finding it difficult to use the sweet alert after a successful login and also add the sweet alert after a user successfully inserts his/her data into the database. I am finding it difficult to use the sweet alert after a successful login and also add the sweet alert after a user successfully inserts his/her data into the database.x
<?php
include ("dbcon.php");
session_start();
if(isset($_POST['login'])){
//get the user data
$username = mysqli_real_escape_string($conn , $_POST['username']);
$email = mysqli_real_escape_string($conn , $_POST['email']);
$pwd = mysqli_real_escape_string($conn , $_POST['pdf]);
if(empty($username) || empty($email) || empty($pwd)){
header("Location:../../login.php?loginempty");
exit();
}else{
$select = "SELECT * FROM users WHERE email ='$email' ";
$result = mysqli_query($conn , $select);
$result_check = mysqli_num_rows($result);
if($result_check < 1){
header("Location:../../login.php?invaliduid");
exit();
}else{
if($row=mysqli_fetch_assoc($result)){
$cpwd = $row['pwd'];
//de-hashed the password
// $hashedpwd = password_verify($pwd, $row['pdf]);
if($pwd != $cpwd){
header("Location: ../../login.php?invalidpwd");
exit();
}elseif($pwd == $cpwd){
$_SESSION['uid'] =$row['username'];
$username = $_SESSION['uid'];
echo "
Swal.fire({
title: 'Welcome to bottles beach',
text: 'You successfully submitted the form,
icon: 'success',
showCancelButton: false,
confirmButtonColor: '#3085d6',
confirmButtonText: 'Great, show me the site!'
}).then((result) => {
if (result.confirmed) {
location = '../../dashboard.php'
}
})
";
// header("Location:../../dashboard.php?loginsuc");
// exit();
}
}
}
}
}
?>
<?php
session_start();
include ("dbcon.php");
if(isset($_POST["reg-btn"])) {
$username = mysqli_real_escape_string($conn , $_POST['username']);
$email = mysqli_real_escape_string($conn , $_POST['email']);
$phone = mysqli_real_escape_string($conn , $_POST['phone']);
$pwd = mysqli_real_escape_string($conn , $_POST['pwd']);
$cpwd = mysqli_real_escape_string($conn , $_POST['cpwd']);
// $verify_token = md5(random());
// VALIDATION
if(strlen(trim($pwd)) < 5) {
// Password Length
header("Location:../../register.php?passwordshort");
exit();
}
if($pwd != $cpwd ) {
// If Password = Confirm Password
header("Location:../../register.php?comfirmpass");
exit();
}
// Check for Empty Inputs
if (empty($username)|| empty($email)|| empty($phone)|| empty($pwd)|| empty($cpwd) ) {
header ("Location:../../register.php?signupempty");
exit();
}else {
//VALIDATE EMAIL
if(!filter_var($email,FILTER_VALIDATE_EMAIL)){
header ("Location:../../register.php?invalidemail");
exit();
}else {
$select = "SELECT * FROM users WHERE username = '$username' AND pwd ='$pwd'";
$result = mysqli_query($conn , $select);
$result_check = mysqli_num_rows($result);
if($result_check > 0) {
echo "ooooops!!! user already exists";
}else {
$insert = "INSERT INTO users (username, email, phone, pwd, cpwd) VALUES ('$username', '$email', '$phone', '$pwd', '$cpwd')";
$result = mysqli_query($conn , $insert);
if(!$result) {
echo "ooooops!!! an error was encountered while inserting data to the database";
}else {
echo "
Swal.fire({
title: 'Welcome to bottles beach',
text: 'You successfully submitted the form,
icon: 'success',
showCancelButton: false,
confirmButtonColor: '#3085d6',
confirmButtonText: 'Great, show me the site!'
}).then((result) => {
if (result.confirmed) {
location = '../../login.php'
}
})
";
// header("location: ../../login.php");
// exit();
}
}
}
}
}else {
header ("Location:../../register.php?signupempty");
exit();
}
?>
<?php
$page_title = "Login Form";
include ('assets/includes/header.php');
include ('assets/includes/navbar.php');
?>
<div class="py-5">
<div class="container">
<div class="row justify-content-center">
<div class="col-md-6">
<div class="card shadow">
<div class="card-header">
<h5>Login Form</h5>
</div>
<div class="card-body">
<form action="assets/action/login_action.php" method="POST" autocomplete="no">
<div class="form-group mb-3">
<label for="username">Username</label>
<input type="text" name="username" id="username" class="form-control" autocomplete="off">
</div>
<div class="form-group mb-3">
<label for="email">Email</label>
<input type="email" name="email" id="email" class="form-control" autocomplete="off">
</div>
<div class="form-group mb-3">
<label for="pwd">Password</label>
<input type="text" name="pwd" id="pwd" class="form-control" autocomplete="off">
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary" name="login">Login Now</button>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
<?php
include ('assets/includes/footer.php');
?>
<?php
$page_title = "Registration Form";
include ('assets/includes/header.php');
include ('assets/includes/navbar.php');
// include ('assets/action/dbcon.php');
?>
<div class="py-5">
<div class="container">
<div class="row justify-content-center">
<div class="col-md-6">
<div class="card shadow">
<div class="card-header">
<h5>Registration Form With Email Verification</h5>
</div>
<div class="card-body">
<form action="assets/action/code.php" method="POST">
<div class="form-group mb-3">
<label for="username">Username</label>
<input type="text" name="username" id="username" class="form-control" autocomplete="off">
</div>
<div class="form-group mb-3">
<label for="email">Email</label>
<input type="email" name="email" id="email" class="form-control" autocomplete="off">
</div>
<div class="form-group mb-3">
<label for="phone">Phone Number</label>
<input type="text" name="phone" id="phone" class="form-control" autocomplete="off">
</div>
<div class="form-group mb-3">
<label for="pwd">Password</label>
<input type="text" name="pwd" id="pwd" class="form-control" autocomplete="off">
</div>
<div class="form-group mb-3">
<label for="cpwd">Confirm Password</label>
<input type="text" name="cpwd" id="cpwd" class="form-control" autocomplete="off">
</div>
<div class="form-group">
<button type="submit" name="reg-btn" class="btn btn-primary">Register Now</button>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
<?php
include ('assets/includes/footer.php');
?>
difficult adding sweet alert redirection after a user successfully inserts his / her details
<button type="submit" class="btn btn-primary" name="login">Login Now</button>
try to use javascript code on button click.
<script>
$(document).ready(function()
{
$("#login").click(function()
{
var username = $("#username").val();
var password = $("#password").val();
$.ajax({
type: "POST",
url: "check.php", <--- //your php code for checking credentials
data: {username:username, password:password},
cache: false,
success: function(data){
swal({
title: "Success!",
text: "Login Successfully!",
type: "success"
});
window.location.href = "dashboard.php"; <--your link to next page
},
error: function(xhr, status, error) {console.error(xhr);}
});
});
});
</script>

Else statement being run even when if statement is true?

I'm trying to establish a verification system for a form, whereas one of the input elements is called verification. The form should only be submitted (as an email to someone else) if the value of verification is 108 — which works. However, when I run my code, I find that the code inside the else statement (of the embedded if-statement) also runs, and I get a popup window that asks me what 3+105 is. Why is this?
HTML
<form class="pb-5" action="" method="POST">
<div class="form-row">
<div class="form-group col-md-6">
<label for="inputName4">Name</label>
<input
type="text"
class="form-control"
id="inputName"
name="name"
value="<?php echo isset($_POST['name']) ? htmlspecialchars($_POST['name'], ENT_QUOTES) : ''; ?>"
required
/>
</div>
<div class="form-group col-md-6">
<label for="inputCompany">Company</label>
<input
type="text"
class="form-control"
id="inputCompany"
name="company"
value="<?php echo isset($_POST['company']) ? htmlspecialchars($_POST['company'], ENT_QUOTES) : ''; ?>"
required
/>
</div>
</div>
<div class="form-row">
<div class="form-group col-md-6">
<label for="subject">Subject</label>
<input
type="text"
class="form-control"
id="subject"
name="subject"
value="<?php echo isset($_POST['subject']) ? htmlspecialchars($_POST['subject'], ENT_QUOTES) : ''; ?>"
required
/>
</div>
<div class="form-group col-md-6">
<label for="inputEmail">Email</label>
<input
type="email"
class="form-control"
id="inputEmail"
name="email"
value="<?php echo isset($_POST['email']) ? htmlspecialchars($_POST['email'], ENT_QUOTES) : ''; ?>"
required
/>
</div>
</div>
<div class="form-group">
<label for="verification">What is 5+103?</label>
<input
type="text"
class="form-control"
id="verification"
name="verification"
required
></input>
</div>
<div class="form-group">
<label for="textarea">Message</label>
<textarea
class="form-control"
id="textarea"
rows="3"
name="message"
required
><?php echo isset($_POST['message']) ? htmlspecialchars($_POST['message'], ENT_QUOTES) : ''; ?>"</textarea>
</div>
<button type="submit" class="btn btn-primary my-3">
Send
</button>
</form>
PHP
$verification = $_POST["verification"];
settype($verification, "integer");
if($_SERVER["REQUEST_METHOD"] == "POST") {
if($verification == 108) {
mail($to, $subject, $message, $headers);
echo "<script> alert('Thank you for your mail!')</script>";
} elseif ($_SERVER["REQUEST_METHOD"] == "POST" && mail($to, $subject, $message, $headers) === false) {
echo "<script> alert('An error has occurred.')</script>";
} else {
echo "<script> alert('What is 3+105?')</script>";
}
} else {
return;
}
If I can clarify my issue in any way, please do not hesitate to let me know! Thanks in advance!
I suppose you have to rewrite your if ... else ...
$verification = $_POST["verification"];
settype($verification, "integer");
if($_SERVER["REQUEST_METHOD"] == "POST") {
if($verification == 108) {
//TEST IF MAIL IS CORRECTLY SEND
if( mail($to, $subject, $message, $headers) === false){
echo "<script> alert('An error has occurred.')</script>";
}
else{
echo "<script> alert('Thank you for your mail!')</script>";
}
}
else {
echo "<script> alert('What is 3+105?')</script>";
}
}

php doesn't add a div, it changes the page completely

I am trying to make a contact form in PHP and HTML. The code works but the problem is that is completely changes the page simply ruining the process of having only a single change: the div that goes on the bottom of the form. Here is my php file:
<?php
if (isset($_POST["submit"])) {
$name = $_POST['name'];
$title = $_POST['title'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$url = $_POST['url'];
$message = $_POST['message'];
$human = intval($_POST['human']);
$from = 'Partner from Website';
$to = 'jordanwhite916#gmail.com';
$subject = 'VetConnexx Partner Inquiry';
$body = "From: $name\n E-Mail: $email\n Phone: $phone\n Message:\n $message";
// Check if name has been entered
if (!$_POST['name']) {
$errName = 'Please enter your name';
}
if (!$_POST['title']) {
$errTitle = 'Please enter your title';
}
if (!$_POST['phone']) {
$errPhone = 'Please enter your phone';
}
// Check if email has been entered and is valid
if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
$errEmail = 'Please enter a valid email address';
}
if (!$_POST['url'] || !filter_var($_POST['url'], FILTER_VALIDATE_URL)) {
$errURL = 'Please enter a valid website';
}
//Check if message has been entered
if (!$_POST['message']) {
$errMessage = 'Please enter your message';
}
//Check if simple anti-bot test is correct
if ($human !== 5) {
$errHuman = 'Your anti-spam is incorrect';
}
// If there are no errors, send the email
if (!$errName && !$errEmail && !$errMessage && !$errHuman && !$errTitle && !$errPhone && !$errURL) {
if (mail ($to, $subject, $body, $from)) {
$result='<div class="success">Thank You! I will be in touch</div>';
} else {
$result='<div class="danger">Sorry there was an error sending your message. Please try again later</div>';
}
}
}
?>
<div class="panel panel-default">
<div class="panel-body">
<p>Become a VetConnexx Business Partner.</p>
<br />
<br />
<p>VetConnexx brings the mission focused discipline, integrity, and motivation of the US Armed Forces
to your customers. VetConnexx has been tested by the best and exceeds the standards expected of
Fortune 100 companies and their privately held peers.</p>
<br />
<br />
<p>We can bring the same level of service to your business. To discuss our client services, please
contact us at VetPartners#VetConnexx.com</p>
<br />
<br />
<form class="form-horizontal" role="form" method="post" action="businesspartners.php">
<div class="form-group">
<label for="name" class="col-sm-2 control-label">Name</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="name" name="name" placeholder="First & Last Name" value="<?php echo htmlspecialchars($_POST['name']); ?>">
<?php echo "<p class='text-danger'>$errName</p>";?>
</div>
</div>
<div class="form-group">
<label for="title" class="col-sm-2 control-label">Title</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="title" name="title" placeholder="Title" value="<?php echo htmlspecialchars($_POST['title']); ?>">
<?php echo "<p class='text-danger'>$errTitle</p>";?>
</div>
</div>
<div class="form-group">
<label for="phone" class="col-sm-2 control-label">Phone</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="phone" name="phone" placeholder="Phone" value="<?php echo htmlspecialchars($_POST['phone']); ?>">
<?php echo "<p class='text-danger'>$errPhone</p>";?>
</div>
</div>
<div class="form-group">
<label for="email" class="col-sm-2 control-label">Email</label>
<div class="col-sm-10">
<input type="email" class="form-control" id="email" name="email" placeholder="example#domain.com" value="<?php echo htmlspecialchars($_POST['email']); ?>">
<?php echo "<p class='text-danger'>$errEmail</p>";?>
</div>
</div>
<div class="form-group">
<label for="url" class="col-sm-2 control-label">URL</label>
<div class="col-sm-10">
<input type="url" class="form-control" id="url" name="url" placeholder="www.examplewebsite.com" value="<?php echo htmlspecialchars($_POST['url']); ?>">
<?php echo "<p class='text-danger'>$errURL</p>";?>
</div>
</div>
<div class="form-group">
<label for="message" class="col-sm-2 control-label">Message</label>
<div class="col-sm-10">
<textarea class="form-control" rows="4" name="message" placeholder="How may we help you?"><?php echo htmlspecialchars($_POST['message']);?></textarea>
<?php echo "<p class='text-danger'>$errMessage</p>";?>
</div>
</div>
<div class="form-group">
<label for="human" class="col-sm-2 control-label">2 + 3 = ?</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="human" name="human" placeholder="Your Answer">
<?php echo "<p class='text-danger'>$errHuman</p>";?>
</div>
</div>
<div class="form-group">
<div class="col-sm-10 col-sm-offset-2">
<input id="submit" name="submit" type="submit" value="Send" class="btn btn-primary">
</div>
</div>
<div class="form-group">
<div class="col-sm-10 col-sm-offset-2">
<?php echo $result; ?>
</div>
</div>
</form>
</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js"></script>
The link to the website I am trying to develop is located here: Go to Business Partners, and fill out the information in the contact form to see what may be wrong. I only want the div on the bottom to show after the Send button is click, not for a completely white page with black text and forms to come up that has the same content. Here's the link to the website:
http://www.sampsonvision.com/VetConnexxWebsite
I suggest you want to use AJAX call to your PHP file instead of a simple submission. Because after form submission, the only content appears on a page is that one which was generated by the script. Your script outputs only one div.

javascript - stop refresh if error is returned from php

I was wondering whether it would be possible to add something to my javascript which would allow a refresh if the form is submitted fine however will not refresh the page if an error is returned back from PHP.
is this possible? if so could anyone provide me with any guidance that will help me achieve this please. I have manage to get the javascript to refresh when the form is submitted and a message is returned.
this is the js:
function addCall() {
var data = $('#addForm').serialize();
$.post('../Admin/ManageCourses_AddSubmit.php', data, function(response){
$("#addForm").html(response);
//'soft'reload parent page, after a delay to show message
setTimeout(function(){
$('#addModal').modal('hide')
location.reload();
},3500);
}).fail(function(jqXHR, textStatus) {
alert( "Request failed: " + textStatus );
});
}
and the php which processes the form and validates it:
<?php
session_start();
if (empty($_POST['course_title'])) {
$message = " Value is required";
}
else {
$course_title = trim($_POST['course_title']);
# Validate Course #
if (!ctype_alpha($course_title)) {
$message = '<p class="error">Course title should be alpha characters only.</p>';
}
elseif (strlen($course_title) < 3 OR strlen($course_title) > 50) {
$message = '<p class="error">Course title should be within 3-50 characters long.</p>';
}
else {
include "../includes/db_conx.php";
try
{
$db_conx = new PDO("mysql:host=$mysql_hostname;dbname=$mysql_dbname", $mysql_username, $mysql_password);
$db_conx->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = $db_conx->prepare("INSERT INTO `insights`.`course_details` (`course_title`) VALUES (:course_title)");
$sql->bindParam(':course_title', $course_title, PDO::PARAM_STR);
$sql->execute();
$message = "<p class='text-success'> Record Successfully Added <span class='glyphicon glyphicon-ok'/></p>";
}
catch(Exception $e) {
if( $e->getCode() == 23000)
{
$message = 'Course already exists in database';
}
else
{
$message = $e->getMessage();
}
}
}
}
die($message);
?>
It would really useful to the user to be able to make amendments to the form instead of rewriting it all again.
the form:
<div class="modal fade" id="addModal" tabindex="-1" role="dialog" aria-labelledby="addModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">×</span></button>
<h4 class="modal-title">Add New Record: </h4>
</div>
<div class="modal-body">
<form id="addForm" class="addForm">
<div class="form-group">
<label class="control-label" for="forename">Forename:</label>
<div class="controls">
<input id="forename" name="forename" type="text" placeholder="Enter forename(s)" class="form-control" maxlength="100" required="">
</div>
</div>
<div class="form-group">
<label class="control-label" for="surname">Surname:</label>
<div class="controls">
<input id="surname" name="surname" type="text" placeholder="Enter surname" class="form-control" maxlength="100" required="">
</div>
</div>
<!-- Text input-->
<div class="form-group">
<label class="control-label" for="email">Email:</label>
<div class="controls">
<input id="email" name="email" type="email" placeholder="Enter a valid email" class="form-control" maxlength="255" value="" required="">
</div>
</div>
<div class="form-group">
<label class="control-label" for="username">Username:</label>
<div class="controls">
<input id="username" name="username" type="text" placeholder="username" class="form-control" value="" maxlength="50" required="">
</div>
</div>
<div class="form-group">
<label class="control-label" for="password">Password:</label>
<div class="controls">
<input id="password" name="password" type="password" placeholder="password" class="form-control" maxlength="40" required="">
</div>
</div>
<div class="form-group">
<label class="control-label" for="confirm_password">Confirm Password:</label>
<div class="controls">
<input id="confirm_password" name="confirm_password" type="password" placeholder="retype password" class="form-control" maxlength="40" required="">
</div>
</div>
<?php
include "../includes/db_conx.php";
try {
$db_conx = new PDO("mysql:host=$mysql_hostname;dbname=$mysql_dbname", $mysql_username, $mysql_password);
$db_conx->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt2 = $db_conx->prepare('SELECT * FROM role_type');
$stmt2->execute();
$roles = $stmt2->fetchAll(PDO::FETCH_ASSOC);
}
catch(Exception $e)
{
die ("Could not connect to the database $mysql_dbname :" . $e->getMessage());
}
?>
<div class="control-group">
<label class="control-label" for="role_type">Select User Type:</label><p></p>
<select name="role">
<option value=''>Select One</option>";
<?php foreach($roles as $role): ?>
<option value="<?php echo $role['role_type_code'] ?>"><?php echo $role['role_title'] ?></option>
<?php endforeach ?>
</select>
</div>
<p></p>
<?php
include "../includes/db_conx.php";
try {
$db_conx = new PDO("mysql:host=$mysql_hostname;dbname=$mysql_dbname", $mysql_username, $mysql_password);
$db_conx->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt3 = $db_conx->prepare('SELECT * FROM course_details ORDER BY course_title');
$stmt3->execute();
$courses = $stmt3->fetchAll(PDO::FETCH_ASSOC);
}
catch(Exception $e)
{
die ("Could not connect to the database $mysql_dbname :" . $e->getMessage());
}
?>
<div class="control-group">
<label class="control-label" for="course_details">Select Course:</label><p></p>
<select name="course">
<option value=''>Select One</option>";
<?php foreach($courses as $course): ?>
<option value="<?php echo $course['course_code'] ?>"><?php echo $course['course_title'] ?></option>
<?php endforeach ?>
</select>
</div>
<input type="hidden" name="form_token" value="<?php echo $form_token; ?>" />
</form>
</div>
<div class="modal-footer">
<div class="btn-toolbar">
<button type="button" class="btn btn-default" class="pull-right" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-success" class="pull-right" onclick="addUCall();">Submit <span class="glyphicon glyphicon-saved"></button>
</div>
</div>
</div>
</div>
</div>
Thank you in advance.
Your message variable contain both error and success message. It will reload page every time when it receives response.
Add this after $("#addForm").html(response); -
var n = response.search("class=\'text-success\'");
if(n!=-1){
//reload success
}
else{
//stay on page, errors
}
As you're returning the error messages in the response, check for the error class and do the reload if necessary.
$.post('../Admin/ManageCourses_AddSubmit.php', data, function(response){
if(response.contains("text-success")) {
// put your logic here for success
} else {
// logic for errors
}
});

Wordpress PHP validation using Modal Contact form

Hi im just new to wordpress i'm just confused on how to link my php validation in wordpress.
i have a send.php in my root folder located in wordpress\wp-content\themes\myTheme. my problem is that whenever i click submit in my contact form. it redirects me to the pa Object not found which in fact the send.php is located. am i doing something wrong? or i linked it incorrectly? here is my code for my Modal Contact Form.
<div class = "modal fade" id = "contact" role = "dialog">
<div class = "modal-dialog">
<div class = "modal-content">
<div class = "modal-header">
<h4>Message Me :3 </h4>
</div>
<div class = "modal-body">
<form class="form-horizontal" name="commentform" method="post" action="send.php">
<div class="form-group">
<label class="control-label col-md-4" for="first_name">First Name</label>
<div class="col-md-6">
<input type="text" class="form-control" id="first_name" name="first_name" placeholder="First Name"/>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-4" for="last_name">Last Name</label>
<div class="col-md-6">
<input type="text" class="form-control" id="last_name" name="last_name" placeholder="Last Name"/>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-4" for="email">Email Address</label>
<div class="col-md-6 input-group">
<span class="input-group-addon">#</span>
<input type="email" class="form-control" id="email" name="email" placeholder="Email Address"/>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-4" for="comment">Question or Comment</label>
<div class="col-md-6">
<textarea rows="6" class="form-control" id="comments" name="comments" placeholder="Your question or comment here"></textarea>
</div>
</div>
<div class="form-group">
<div class="col-md-6">
<button type="submit" value="Submit" class="btn btn-primary pull-right">Send</button>
</div>
</div>
</form>
</div>
and here is my send.php code
<?php
if(isset($_POST['email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "ccmanere#gmail.com";
$email_subject = "Your email subject line";
function died($error) {
// your error code can go here
echo "We are very sorry, but there were error(s) found with the form you submitted. ";
echo "These errors appear below.<br /><br />";
echo $error."<br /><br />";
echo "Please go back and fix these errors.<br /><br />";
die();
}
// validation expected data exists
if(!isset($_POST['first_name']) ||
!isset($_POST['last_name']) ||
!isset($_POST['email']) ||
!isset($_POST['comments'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
$first_name = $_POST['first_name']; // required
$last_name = $_POST['last_name']; // required
$email_from = $_POST['email']; // required
$comments = $_POST['comments']; // required
$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+#[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$email_from)) {
$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
}
$string_exp = "/^[A-Za-z\s.'-]+$/";
if(!preg_match($string_exp,$first_name)) {
$error_message .= 'The First Name you entered does not appear to be valid.<br />';
}
if(!preg_match($string_exp,$last_name)) {
$error_message .= 'The Last Name you entered does not appear to be valid.<br />';
}
if(strlen($comments) < 2) {
$error_message .= 'The Comments you entered do not appear to be valid.<br />';
}
if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Form details below.\n\n";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "First Name: ".clean_string($first_name)."\n";
$email_message .= "Last Name: ".clean_string($last_name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Comments: ".clean_string($comments)."\n";
// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
#mail($email_to, $email_subject, $email_message, $headers);
sleep(2);
echo "<meta http-equiv='refresh' content=\"0; url=http://tutsme-webdesign.info/index.php\">";
?>
<?php
}
?>

Categories

Resources