I'm trying to insert data to MySQL with a form that is in a Bootstrap modal but it doesn't work.
I don't know why but my form method is POST and it seems to be a GET because it prints the data in the web address.
When I try to insert the data through a basic form with the same php code (no format, simple as hell) it inserts the data.
Here's my form in the bootstrap modal.
<div class="container 2">
<button type="button" class="btn btn-primary btn-lg btn-block" data-toggle="modal" data-target="#newRoute">
CREAR NUEVA RUTA
</button>
<div class="modal fade" id="newRoute" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog modal-md" role="document">
<div class="modal-content">
<div class="modal-body">
<div class="container-fluid">
<div class="row">
<form class="route" method="post">
<div class="col-md-6 col-sm-6 col-xs-6">
<div class="form-group">
<label for="name">Ponle un nombre a la ruta</label>
<input type="text" class="form-control" id="name" name="name" required>
</div>
<div class="form-group">
<label for="city">Ciudad</label>
<input type="text" class="form-control" id="city" name="city" required>
</div>
<div class="form-group">
<label for="length">Distancia</label>
<input type="text" class="form-control" id="length" name="length" required>
</div>
</div>
<div class="col-md-6 col-sm-6 col-xs-6">
<div class="form-group">
<label for="start_point">Punto de salida</label>
<input type="text" class="form-control" id="start_point" name="start_point"
required>
</div>
<div class="form-group">
<label for="difficulty">Dificultad</label>
<input type="text" class="form-control" id="difficulty" name="difficulty"
required>
</div>
<div class="form-group">
<label for="date">Fecha de la ruta</label>
<input type="date" class="form-control" id="date" name="date" required>
</div>
</div>
<div class="col-md-12 col-sm-12 col-xs-12">
<div class="form-group">
<label for="description">Detalles de la ruta</label>
<textarea class="form-control" rows="5" id="description"
name="description"></textarea>
</div>
</div>
<div class="modal-footer">
<div class="form-group">
<button type="submit" name="submit" id="submit" class="btn btn-primary"
value="Enviar">
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
and here is my PHP code
include('db/db.php');
if(isset($_POST['submit'])) {
// Adjudicar name a variable
$name = stripslashes($_POST['name']);
$name = mysqli_real_escape_string($conn, $name);
// Adjudicar city a variable
$city = stripslashes($_POST['city']);
$city = mysqli_real_escape_string($conn, $city);
// Adjudicar length a variable
$length = stripslashes($_POST['length']);
$length = mysqli_real_escape_string($conn, $length);
// Adjudicar start_point a variable
$start_point = stripslashes($_POST['start_point']);
$start_point = mysqli_real_escape_string($conn, $start_point);
// Adjudicar difficulty a variable
$difficulty = stripslashes($_POST['difficulty']);
$difficulty = mysqli_real_escape_string($conn, $difficulty);
// Adjudicar date a variable
$date = stripslashes($_POST['date']);
$date = mysqli_real_escape_string($conn, $date);
// Adjudicar description a variable
$description = stripslashes($_POST['description']);
$description = mysqli_real_escape_string($conn, $description);
// QUERY
$query = "INSERT INTO routes (name, city, length, start_point, difficulty, user_id, date, description) VALUES ('$name','$city',$length,'$start_point','$difficulty',".$_SESSION['id'].",'$date','$description')";
// Se realiza la query
$result = mysqli_query($conn,$query);
//Condición si se realiza la query correctamente
if($result){
header("Location: routes.php");
echo '<script type="text/javascript">alert("'.$query.'");</script>';
}else{
echo '<script>alert("ERROR");</script>';
}}
my db.php (connection to database)
$conn = new mysqli('localhost', 'root', "", 'users');
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
Thanks in advance!
PS. When I submit the form, this is what appears in the address (all the fields were filled by sample text). It seems to be a get form.
http://localhost/Proyecto/routes.php?name=Test&city=Test&length=20&start_point=Test&difficulty=Test&date=2018-05-12&description=Test+description&submit=Enviar
Try to check if you have forget to close any other form somewhere on your code that should be with method="get" or without method attribute
and so when you submit the data it gets the method from that form.
The code you have publish here should work correctly.
Your form code is correct, I have checked it on my system. You must be checking/testing some other page or form.
Related
So, I cannot find the solution to the problem I'm having. I'm really new to coding but learned how to start coding using basic HTML, PHP, PDO, and AJAX. So my problem comes from a form that retrieves dates from a calendar using the type=date from the form. The code of the form is down below.
<div class="col-lg-4">
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title text-center"><i class="fa fa-bar-chart fa-fw"></i> Ingreso de reporte</h3>
</div>
<div id="alert_success" class="panel-body">
<br>
<form method="post" class="form-horizontal" role="form" action="ajax_form_post.php" id="insertreport">
<div class="form-group">
<label class="control-label col-sm-2" for="video" style="color:#777;">ID de video</label>
<div class="col-sm-10">
<input type="text" name="video" class="form-control" id="video" placeholder="Ingresa id del video" required>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="date_i" style="color:#777;">Fecha de arriendo</label>
<div class="col-sm-10">
<input type="date" name="date_i" class="form-control" id="date_i" placeholder="" required>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="date_f" style="color:#777;">Fecha de devolución</label>
<div class="col-sm-10">
<input type="date" name="date_f" class="form-control" id="date_f" placeholder="" required>
</div>
</div>
<div class="form-group">
<div class="col-sm-10">
<input type="hidden" name="c_id" class="form-control" id="user_id" value="<?php echo $id ?>" required>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<input type="submit" class="btn btn-primary" name="update_customer" value="Enviar" id="submitdata">
</div>
</div>
</form>
<div class="text-right">
<i class="fa fa-arrow-circle-right"></i>
</div>
</div>
</div>
</div>
Now the problem starts with this Ajax form I built. BTW the script is working fine, the problem is inside this set of code.
<?php
/****************Get customer info to ajax *******************/
//require database class files
require("includes/pdocon.php");
//instatiating our database objects
$db = new Pdocon ;
if(isset($_POST['c_id'])){
$id = $_POST['c_id'];
$date_i = date("Y-m-d", strtotime($_POST['date_i']));
$date_f = date("Y-m-d", strtotime($_POST['date_f']));
$raw_v_id = clean_data($_POST['video']);
$v_id = val_int($raw_v_id);
$db->query('SELECT * FROM videos WHERE v_id = :v_id');
$db->bindvalue(':v_id', $v_id, PDO::PARAM_INT);
$row = $db->fetchSingle();
$db->query('INSERT INTO arriendo (transaccion, c_id, v_id, f_arriendo, f_devolucion)
VALUES (NULL, :c_id, :v_id :f_arriendo, :f_devolucion)');
$db->bindvalue(':f_arriendo', $date_i, PDO::PARAM_STR);
$db->bindvalue(':f_devolucion', $date_f, PDO::PARAM_STR);
$db->bindvalue(':c_id', $id, PDO::PARAM_INT);
$db->bindvalue(':v_id', $v_id, PDO::PARAM_INT);
$run = $db->execute();
}
if($run){
echo "<p class='bg-success text-center' style='font-weight:bold;'>Valor actualizado </p>";
}
?>
I get the following error:
Fatal error: Uncaught PDOException: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ''2021-08-05', '2021-08-06')' at line 2
Any help or a little guidance would be greatly appreciated. Thanks in advance.
Modal will not trigger my PHP validation.
I can't get my modal to use my PHP validation. HTML validation works. It connects to the DB and makes the updates, but skips the server-side validation. I would appreciate any help, this is new to me why a modal won't validate.
My PHP and Modal form is in one file. I know I need more error messages added to my form, but leaving the phone number and first name out triggers no error messages, so I will add more once I fix this issue. The code just executes and changes the DB.
<?php
//ini_set( 'display_errors', 1 );
//error_reporting( E_ALL );
session_start();
// Include db config
require_once 'includes/dbh.inc.php';
// Process form when post submit
// if($_SERVER['REQUEST_METHOD'] === 'POST'){
// Sanitize POST
$_POST = filter_input_array(INPUT_POST, FILTER_SANITIZE_STRING);
$error=array();
// $error = false;
// $error = isset($_SESSION['error']);
// $error = $_SESSION['error'];
function test_input($data)
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
$first = test_input($_POST['first']);
$last = test_input($_POST['last']);
$email = test_input($_POST['email']);
$unit = $_SESSION['unit'];
$bed = test_input($_POST['bed']);
$bath = test_input($_POST['bath']);
$web = test_input($_POST['web']);
$phone = test_input($_POST['phone']);
$manage = test_input($_POST['manage']);
$ck = $_POST['rent'];
$id = $_SESSION['id'];
// Validate name
if(empty($first) || empty($last)){
$error['name'] = "Enter name";
}
// Validate email
if(empty($email)){
$error['email'] = "Please enter email";
}
// check if e-mail address is well-formed
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$error['email'] = "Invalid email format";
}
// Validate name
if(empty($bed) || empty($bath)){
$error['rooms'] = 'Please enter bed/bath #';
}
if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&##\/%?=~_|!:,.;]*[-a-z0-9+&##\/%=~_|]/i",$web)) {
$error['web'] = "Invalid URL";
}
// Validate name
if(empty($phone)){
$error['phone'] = 'Please enter your phone';
}
//Check phone # format 000-000-0000
if(!preg_match("/^([1]-)?[0-9]{3}-[0-9]{3}-[0-9]{4}$/i", $phone)) {
$error['phone'] = 'Please enter correct phone format';
}
//Check manage format
if (!preg_match("/^(\s\(([^)]+)\))?[[:punct:]]?\p{Lu}+(?:[\s'-]?[\p{L}\d]+)+(\(([^)]+)\))*$/", $manage)) {
$error['manage'] = 'Please enter correct management format';
}
if (empty($error)) {
var_dump($error);
print_r($error);
$sql = 'UPDATE condos_hp SET user_firstname=:first, user_lastname=:last, user_email=:email, user_bed=:bed, user_bath=:bath, user_web=:web, user_phone=:phone, user_manage=:manage, rent=:rent WHERE id=:id';
// Prepare statement
$stmt = $pdo->prepare($sql);
// Bind params
$stmt->bindParam(':first', $first, PDO::PARAM_STR);
$stmt->bindParam(':last', $last, PDO::PARAM_STR);
$stmt->bindParam(':email', $email, PDO::PARAM_STR);
// $stmt->bindParam(':unit', $unit);
$stmt->bindParam(':bed', $bed, PDO::PARAM_INT);
$stmt->bindParam(':bath', $bath, PDO::PARAM_INT);
$stmt->bindParam(':web', $web, PDO::PARAM_STR);
$stmt->bindParam(':phone', $phone, PDO::PARAM_STR);
$stmt->bindParam(':manage', $manage, PDO::PARAM_STR);
$stmt->bindParam(':rent', $ck, PDO::PARAM_INT);
$stmt->bindParam(':id', $id, PDO::PARAM_INT);
$stmt->execute(array(':first'=>$first, ':last'=>$last, ':email'=>$email, ':bed'=>$bed, ':bath'=>$bath, ':web'=>$web, ':phone'=>$phone, ':manage'=>$manage, ':rent'=>$ck, ':id'=>$id));
// if($stmt){
// header('Location: index1.php');
//}
if (isset($_POST['update'])) {
// session_start();
//Then delete all SESSION variables
session_unset();
//And destroy the current session that is running
session_destroy();
// exit(header("Location: http://www.harbour-place.com/login0.php?update=success"));
if (headers_sent()) {
die("Redirect failed. Please click on this link: <a href=http://www.harbour-place.com/login0.php>");
}
else{
exit(header("Location: http://www.harbour-place.com/login0.php?update=success"));
}
}
}
include_once 'includes/mailer2.php';
?>
<!-- Button trigger modal -->
<div class="row justify-content-center">
<button type="button" class="btn btn-primary btn-sm" data-toggle="modal" data-target="#exampleModalCenter">
UPDATE
</button>
</div>
</div>
<!-- Modal -->
<div class="modal fade" id="exampleModalCenter" tabindex="-1" role="dialog" aria-labelledby="userupdateform" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="exampleModalCenterTitle">UPDATE RENTAL UNIT # <?php echo htmlentities($_SESSION['unit']) ?> </h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body">
<form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
<div class="form-row justify-content-center">
<div class="form-group col-md-4">
<label >First Name</label>
<input type="text" class="form-control form-control-sm <?php echo (!empty($error['name'])) ? 'is-invalid' : ''; ?>" name="first" value="<?php echo htmlentities($_SESSION['first']) ?>">
<span class="invalid-feedback"><?php echo $error['name']; ?></span>
</div>
<div class="form-group col-md-4">
<label >Last Name</label>
<input type="text" class="form-control form-control-sm" name="last" value=<?php echo htmlentities($_SESSION['last']) ?> required>
</div>
</div>
<div class="form-row justify-content-center">
<div class="form-group col-md-8">
<label >Email</label>
<input type="text" class="form-control form-control-sm" name="email" value=<?php echo htmlentities($_SESSION['email']) ?> required>
</div>
<!--<div class="form-group col-md-4">
<label >Unit</label>
<input type="text" class="form-control form-control-sm" name="unit" value=<php echo $_SESSION['u_unit'] ?> required>
</div>-->
</div>
<div class="form-row justify-content-center">
<div class="form-group col-md-2">
<label >Bed</label>
<input type="text" class="form-control form-control-sm" name="bed" value=<?php echo htmlentities($_SESSION['bed']) ?> required>
</div>
<div class="form-group col-md-2">
<label >Bath</label>
<input type="text" class="form-control form-control-sm" name="bath" value=<?php echo htmlentities($_SESSION['bath']) ?> required>
</div>
<div class="form-group col-md-4">
<label >Web Site - www.myunit.com</label>
<input type="text" class="form-control form-control-sm" name="web" value=<?php echo htmlentities($_SESSION['web']) ?> required>
</div>
</div>
<div class="form-row justify-content-center">
<div class="form-group col-md-3">
<label >Phone 000-000-0000</label>
<input type="text" id="yourphone2" class="form-control form-control-sm <?php echo (!empty($error['phone'])) ? 'is-invalid' : ''; ?>" name="phone" value="<?php echo htmlentities($_SESSION['phone']) ?>">
<span class="invalid-feedback"><?php echo $error['phone']; ?></span>
</div>
<div class="form-group col-md-5">
<label >Managment Co (VRBO, Self,etc)</label>
<input type="text-capitalize" class="form-control form-control-sm" name="manage" value="<?php echo htmlentities( $_SESSION['manage']) ?>" required>
</div> </div>
<!--<div class="form-row justify-content-center">
<div class="form-group col-md-8">
<label >Managment Co - VRBO etc.</label>
<input type="text" class="form-control form-control-sm" name="manage" value="" required>
</div></div>-->
<div class="form-row justify-content-center">
<div class="custom-control custom-checkbox">
<input type="hidden" name="rent" value="0">
<input type="checkbox" value="1" name="rent" class="custom-control-input" id="customCheck1" checked="checked">
<label class="custom-control-label" for="customCheck1">Check to show on the rental site</label>
</div></div>
<p class="text-center mb-1"><small><i class="fas fa-lock"></i>Your Information is Safe With us!<br> You will need to re-login after clicking Update<br>This also updates the HP rental website listing. </small></p>
<button type="submit" class="btn btn-primary btn-sm" name="update">UPDATE HP UNIT</button>
</form>
</div>
</div>
</div>
</div>
</body>
</html>
There are a couple issues here
You are setting $error to a boolean value, trying to define an array key with a value on a boolean doesn't work, it will continue to just be a boolean [Edit] if the value of $error evaluates to false before assigning array key => values to it, it will convert to an array but if it evaluates to true it will result in Warning: Cannot use a scalar value as an array and continue to be a boolean
You are never checking $error to stop the script from executing and updating the database
I have created a form using Bootstrap modal, a database connection via PHP as well as a simple form validation. When I fill out form and hit submit, it seems to connect to database and no form errors come back. It states: "-1 rows are affected. Thank you for your RSVP" but when I look on PHPMyAdmin there are no values inputted into my database.
Below is my PHP:
<?php
//check if form is submitted and not a hack/bot. Checks to see if form is submitte by the post method then runs fxn.
if ($_SERVER['REQUEST_METHOD'] == 'POST'){
//grab all values that come from form in these variables. Each line below grabs info typed/selected in each form input. The value in square brackets is the name value of each input. Keep the $variables different from input names.
$rsvpvar = $_POST['rsvpBox'];
$fullnamevar = $_POST['fullName'];
$emailvar = $_POST['emailBox'];
$attendvar = $_POST['attend'];
$guestsvar = $_POST['extraGuests'];
$commentsvar = $_POST['additionals'];
//If fxn checks to see if all above variables have values stored in them using !empty fxn and if not if runs the echo. Use !empty (not empty) and not isset because isset will allow db connection even if some form values are blank. If there is a value stored in all form inputs then we incude this php or run the connetion to database.
if(!empty($rsvpvar) && !empty($fullnamevar) && !empty($emailvar) && !empty($attendvar) && !empty($guestsvar) && !empty($commentsvar)){
//Define the below vaiables within parenthesis.
$hostname = "rsvp.db";
$username = "******";
$password = "******";
$dbname = "rsvp";
//Connection to mysql database. OR die drops connection to database for security reasons. Connect error gives message.
$dbc = mysqli_connect($hostname, $username, $password, $dbname) OR die("Could not connect to database, ERROR: ".mysqli_connect_error());
//Set encoding
mysqli_set_charset($dbc, "utf8");
//insert into table "rsvp" by getting or querying values stored in $dbc variable (db info). You insert into the corresponding rows of the db table (i.e. RSVP), then you grab what you're inserting using the VALUES. What you're inserting comes from the above variables.
mysqli_query($dbc, "INSERT INTO rsvp(RSVP, FULLNAME, EMAIL, ATTEND, GUESTS, COMMENTS) VALUES('$rsvpvar', '$fullnamevar', '$emailvar', '$attendvar', $guestsvar', '$commentsvar')");
//define variable $registered which checks database to make sure values were inserted and rows were affected then outputs how many rows affected. Make sure the $dbc or connection info is inside parenthesis.
$registered = mysqli_affected_rows($dbc);
echo $registered." rows are affected. Thank you for your RSVP";
}else{
echo "ERROR: You did not fill out each section";
}
}else{
echo "Please fill out everything on the RSVP form";
}
?>
and my html:
<!-- Modal -->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
<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>
<h2 class="modal-title" id="myModalLabel">RSVP</h2>
</div>
<form role="form" method="post" action="php/rsvp.php">
<div class="modal-body">
<div class="form-inline row response">
<div class="col-sm-12">
<h2>Ya Comin'?</h2>
<div class="radio-inline">
<label class="control-label">
<input type="radio" name="rsvpBox" id="attending" value="option1">
I'll be there with bells on!
</label>
</div>
<div class="radio-inline">
<label class="control-label">
<input type="radio" name="rsvpBox" id="notAttending" value="option2">
Sorry, wish I was cooler...
</label>
</div>
</div>
</div>
<hr>
<div class="deets row">
<div class="col-sm-12">
<h2>Your Deets</h2>
<div class="form-group col-lg-6">
<label class="control-label" for="fullName">Full Name</label>
<input type="text" name="fullName" class="form-control" id="fullName" placeholder="FULL NAME">
</div>
<div class="form-group col-lg-6">
<label class="control-label" for="email">Email</label>
<input type="email" name="emailBox" class="form-control" id="emailBox" placeholder="EMAIL#EXAMPLE.COM">
</div>
</div>
</div>
<hr>
<div class="row days">
<div class="col-sm-12">
<h2>Days Attending</h2>
<div class="checkbox">
<label class="control-label">
<input type="checkbox" name="attend" value="option1">
Friday: Rehersal Dinner & Beach Party
</label>
</div>
<div class="checkbox">
<label class="control-label">
<input type="checkbox" name="attend" value="option2">
Saturday: Wedding & Reception
</label>
</div>
</div>
</div>
<hr>
<div class="guests row">
<div class="col-sm-12">
<h2>Number of Additional Guests</h2>
<select id="extraGuests" name="extraGuests" class="form-control">
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
</select>
</div>
</div>
<div class="extras row">
<div class="col-sm-12">
<h2>Anything Else?</h2>
<h4>Main Course Served Will be Chicken</h4>
<div class="form">
<div class="form-group">
<label class="control-label" for="message"></label>
<textarea name="additionals" id="additionals" class="form-control" rows="3" placeholder="Food specifications, allergies, guest names, etc..."></textarea>
</div>
</div>
</div>
</div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="submit" name="submit" id="submit" value="send" class="btn btn-primary">Submit</button>
</div>
</form>
</div>
</div>
</div>
Please help as I am not an expert on databases...
Your all POST variable is :
$rsvpvar = $_POST['attending'];
$emailvar = $_POST['emailBox'];
$fullnamevar = $_POST['fullName'];
$attend1var = $_POST['friday'];
$attend2var = $_POST['saturday'];
$guestsvar = $_POST['extraGuests'];
$commentsvar = $_POST['additionals'];
SO, should be condition :
if(!empty($rsvpvar) && !empty($emailvar) && !empty($fullnamevar) && !empty($attend1var) && !empty($attend2var) && !empty($guestsvar) && !empty($commentsvar)){
Please use same name in 2 radio input field :
<input type="radio" name="attending" id="attending" value="option1" checked>.
<input type="radio" name="attending" id="notAttending" value="option2">
Note : your code is open for SQL Injection.
I am new to PHP/Sql server. What I am trying to do here is to set the 'action' field of a 'Modal' to be determined based on the query result.If the query is valid , then it will redirect the page where the result will be shown , otherwise it will go back to the search page .
Here is the code snippet for Modal
<!--modal-->
<div class="modal fade" id="my_modal" role="dialogue">
<div class="modal-dialog modal-md">
<div class="modal-content">
<div class="modal-header">
<strong> Provide your credentials to proceed </strong>
<button class="close" data-dismiss="modal">×</button>
</div>
<div class="modal-body">
<div class="row">
<div class="col-md-12">
<form name="mform" action="" method="post" id="mform" >
<div class="form-group ">
<label for="first">Name</label>
<input type="text" name="name" id="name" class="form-control">
</div>
<div class="form-group ">
<label class="control-label" for="email">Email</label>
<input type="email" name="email" id="email" class="form-control">
</div>
<div class="form-group ">
<label class="control-label" for="con">Contact No</label>
<input type="text" name="con" id="con" class="form-control">
</div>
<div class="form-group ">
<label class="control-label" for="DOB">Date of Birth</label>
<input type='datetime' name="DOB" id="DOB" class="form-control">
</div>
<div class="form-group ">
<label class="control-label" for="con">Policy No</label>
<input type="text" name="pon" id="pon" class="form-control">
</div>
<div class="form-group">
<button type="submit" name="msubmit" id="msubmit" class="btn btn-success btn-lg" style="width: 100%;" value="Submit">
<span class="glyphicon glyphicon-ok-sign"></span>Submit
</button>
</div>
</form>
</div>
</div>
</div>
<div class="modal-footer">
</div>
</div>
</div>
</div>
<!--modal-->
The action field in the #my_modal is kept blank , as i intend to according to the query result.
here is the php code
<?php
if (isset($_POST['msubmit']) == 'Submit') {
include './verify_dob.php';
$name = $_POST['name'];
$email = $_POST['email'];
$contact = $_POST['con'];
$policy_no = $_POST['pon'];
$DOB_str = $_POST['DOB'];
echo '<br>';
$action = verify_dob($DOB_str, $policy_no);
echo $action;
if ($action) {
echo "<script type='text/javascript'>";
echo "alert('Success');";
echo "</script>";
$URL ='showInfo.php?pon='.$policy_no; //redirect if the $action is true
echo "<script>location.href='$URL'</script>";
exit(0);
}
else{
echo "<script type='text/javascript'>";
echo "</script>";
$URL ='index.php'; //otherwise
echo "<script>location.href='$URL'</script>";
exit(0);
}
}
?>
The function verify_dob() is given here
<?php
$action = '';
function verify_dob($DOB_str, $policy_no)
{
require './connection.php';
$cr_vw_chk = "IF OBJECT_ID('dbo.vw_search_verify') IS NULL
exec ('
CREATE VIEW dbo.vw_search_verify
AS
SELECT Policyno, Dateofbirth FROM dbo.tbl_Traditional_Policysummery3')
ELSE
exec('ALTER VIEW dbo.vw_search_verify
AS
SELECT Policyno, Dateofbirth FROM dbo.tbl_Traditional_Policysummery3')";
$vw_chk_q = sqlsrv_query($con, $cr_vw_chk);
$check_dob = "select Dateofbirth from vw_search_verify where Policyno='" . $policy_no . "'";
$check_dob_q = sqlsrv_query($con, $check_dob);
if (sqlsrv_fetch($check_dob_q) === false) {
die(print_r(sqlsrv_errors(), true));
}
$dt = new DateTime($DOB_str);
$date_db = sqlsrv_get_field($check_dob_q, 0);
$interval = $dt->diff($date_db);
if ($interval->format('%a') === '0') {
return true;
} else {
return false;
}
}
?>
the purpose of the function is verify whether the inserted DOB is matching with the DOB in DB.With that in mind a view is created.
After clicking the submit button in Modal, the code is supposed to go "showInfo.php" along with get value policy no.
otherwise it should return back to the previous page.
I have also used header of php, but it is showing header value is already passed.
while trying to get advantage of jQuery , onsubmit event , i tried to pass the js variables to php function. As these two languages differ, that is no longer an option.
I have used sql server 2014 as the back end.
How do I accomplish the task ????
After submit in signup page my signUp page redirects to info.php where I want to collect additional info of user using email id he gives on signup page but when I tried to get the email id of user through sessions, session return empty value.
THIS IS MY SIGNUP CODE
<?php
session_start();
if(isset($_POST['submit'])){
$name= $_POST['_user'];
$email = $_POST['_email'];
$pass = $_POST['_password'];
//Insert Data
$sql = "INSERT INTO signup(name,email,password)
VALUES('$name','$email','$pass')";
//Data Validation
if(mysqli_query($conn,$sql)){
echo "<script>alert('SignUp Successfull')</script>";
$_SESSION['user_email'] = $email;
header('Location: info.php');
}
else{
echo "<script>window.alert('You are already a user.')</script>";
}
}
mysqli_close($conn);
?>
AND THIS MY INFO.PHP CODE
<?php
session_start();
if(isset($_POST['_submit'])){
if(empty($_POST['_address']) || empty($_POST['_country']) || empty($_POST['_number']) || empty($_POST['_cnic']) || empty($_POST['_passport'])){
echo "<script>window.alert('All fields are required')</script>";
}
else{
$address = $_POST['_address'];
$country = $_POST['_country'];
$number = $_POST['_number'];
$cnic = $_POST['_cnic'];
$passport = $_POST['_passport'];
$email=$_SESSION['user_email'];
$query = "INSERT INTO info(email,address,country,mobile,cnic,passport)
VALUES('$email','$address','$country','$number','$cnic','$passport')";
if(mysqli_query($conn,$query)){
header('Location: ../index.php');
}
else{
echo "<script>window.alert('Error While Entering the data!.')</script>";
}
}
}
mysqli_close($conn);
?>
In addition I use this global session variable for login page and it works fine.
UPDATE
SIGNUP HTML CODE
<div class="outside">
<form class="form-horizontal" role="form" method="post">
<div class="form-group">
<label class="control-label col-sm-3 glyphicon glyphicon-user" for="name"></label>
<div class="control-label col-sm-8">
<input type="text" name="_user" class="form-control" id="name" placeholder="Full Name">
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-3" for="email">
<img class="glyphicon1" src="../assests/at-sign.png">
</label>
<div class="control-label col-sm-8">
<input type="email" name="_email" class="form-control" id="email" placeholder="Enter Email">
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-3 glyphicon glyphicon-lock" for="password"></label>
<div class="control-label col-sm-8">
<input type="password" name="_password" class="form-control" id="password" placeholder="Enter Password">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-3 col-sm-3">
<button name="submit" id="submit" value="Upload" type="submit" class="btn btn-default">Confirm SignUp</button>
</div>
</div>
<p>Already a User? LogIn</p>
</form>
</div>
Use this for Returns the auto generated id used in the last query
mysqli_insert_id($link)