I am working with two php file. The first file contains a search engine that outputs the results in the second php file. This second php file is displayed in an iframe just underneath the search engine of the first php file. this file also contains data that needs to be submitted using post. I can submit the form and redirect it to a 3rd php file. But I dont want to go away from the current page. So far what I can do is to call an isset function on the same page and fire the insert command to my table. But it refreshes the 2nd php file. Refreshing will reset the search values passed from the 1st php file which is not good. Please help
This is my 2nd php file:
<!DOCTYPE html>
<html>
<head>
<title>RFC System</title>
<script src="jquery-1.12.4.js"></script>
<link rel="stylesheet" type="text/css" href="style2.css">
<link rel="stylesheet" type="text/css" href="modal.css">
</head>
<body>
<?php
require ("../funcs.php"); site_security();
set_error_handler ("my_error_handler");
set_time_limit(0);
date_default_timezone_set ("Asia/Manila");
require("../connmysql.php");
$session_user_id = $_SESSION["USERCID"];
$session_branch = $_SESSION["BRANCHCODE"];
$branchname=$_POST['branchname'];
$empbio=$_POST['empbio'];
$dtrdate_from=$_POST['dtrdate_from'];
$dtrdate_to=$_POST['dtrdate_to'];
##########HELP HERE!
if(isset($_POST['reason'])){
#save to database without refreshing the page
}
#####################
if((empty($branchname)) || (empty($empbio)) || (empty($dtrdate_from)) || (empty($dtrdate_to))){
echo "<font color='#FF0000'><b>Please fill-in all fields to start search</b></font>";
}else{
$startDate = DateTime::createFromFormat("Y/m/d","$dtrdate_from",new DateTimeZone("Asia/Manila"));
$endDate = DateTime::createFromFormat("Y/m/d","$dtrdate_to",new DateTimeZone("Asia/Manila"));
$periodInterval = new DateInterval( "P1D" ); // 1-day, though can be more sophisticated rule
$period = new DatePeriod( $startDate, $periodInterval, $endDate );
$queryname=mysqli_query($conn,"select a.nsemp_fname,a.nsemp_mname,a.nsemp_lname,a.nsemp_sufixname,a.nsemp_empid,a.nsemp_bionum,a.nsemp_brcode from rfc_prf_nsemp a where a.nsemp_bionum=12476")or die();
while($row=mysqli_fetch_array($queryname)){
$fname=$row[0];
$mname=$row[1];
$lname=$row[2];
$suffix=$row[3];
$empid=$row[4];
$bionum=$row[5];
$brcode=$row[6];
}
echo"<hr/>
<h3><b>Name:</b>$fname $mname[0] $lname $suffix<br/>
<b>Employee No:</b> $empid<br/>
<b>Branch:</b> $brcode<br/></h3>
<table class='x'><tr>";
foreach($period as $date){
$x = $date->format("Y-m-d");
$day = date('l', strtotime($x));
if($day=="Sunday"){$background="#00e673";}else if($day=="Saturday"){$background="#ffff99";}else{$background="#ffffff";}
echo"<td height='150px' style='border-style: solid; background:$background'>
<form method='post' action='#'>
<b><font color='#1a1aff'>$x<br/>$day</font></b><br/><br/>";
$query=mysqli_query($conn,"select dtr_time,(CASE in_or_out WHEN 0 THEN 'IN' WHEN 1 THEN 'OUT' END) AS status,datacid from rfc_bio_dtr where dtr_date='$x' and empbio='12476';")or die(mysqli_error());
$check="";
while($row=mysqli_fetch_array($query)){
echo "$row[0] $row[1] <br/>";
$check .=$row[1];
}
if(preg_match('(IN)', $check) && preg_match('(OUT)', $check)) { echo" ";}else{
$queryReason=mysqli_query($conn,"select tk_reason_id,tk_reason_desc from rfc_timekeeping_param;")or die();
echo "<br/><br/>Remarks:<select name='' id=''>";
while($row=mysqli_fetch_array($queryReason)){
echo "<option value='$row[0]'>$row[1]</option>";
}
echo"</select><br/>Notes:<br/><textarea></textarea><input type='submit' value='save' name='reason' id='reason' class='totobutton'>";
}
echo"</form></td>";
}
}
?>
</body>
Here is an updated code using jquery/ajax. There's a problem with the next table data, they dont get submitted. only the first table data gets submitted:
<!DOCTYPE html>
<html>
<head>
<title>RFC System</title>
<script src="jquery-1.12.4.js"></script>
<link rel="stylesheet" type="text/css" href="style2.css">
<link rel="stylesheet" type="text/css" href="modal.css">
<script>
$(function () {
$('#myform').on('submit', function (e) {
e.preventDefault();
$.ajax({
type: 'post',
url: 'savetodb.php',
data: $('#myform').serialize(),
success: function () {
alert('form was submitted');
}
});
});
});
</script>
</head>
<body>
<?php
require ("../funcs.php"); site_security();
set_error_handler ("my_error_handler");
set_time_limit(0);
date_default_timezone_set ("Asia/Manila");
require("../connmysql.php");
$session_user_id = $_SESSION["USERCID"];
$session_branch = $_SESSION["BRANCHCODE"];
$branchname=$_POST['branchname'];
$empbio=$_POST['empbio'];
$dtrdate_from=$_POST['dtrdate_from'];
$dtrdate_to=$_POST['dtrdate_to'];
if((empty($branchname)) || (empty($empbio)) || (empty($dtrdate_from)) || (empty($dtrdate_to))){
echo "<font color='#FF0000'><b>Please fill-in all fields to start search</b></font>";
}else{
$startDate = DateTime::createFromFormat("Y/m/d","$dtrdate_from",new DateTimeZone("Asia/Manila"));
$endDate = DateTime::createFromFormat("Y/m/d","$dtrdate_to",new DateTimeZone("Asia/Manila"));
$periodInterval = new DateInterval( "P1D" ); // 1-day, though can be more sophisticated rule
$period = new DatePeriod( $startDate, $periodInterval, $endDate );
$queryname=mysqli_query($conn,"select a.nsemp_fname,a.nsemp_mname,a.nsemp_lname,a.nsemp_sufixname,a.nsemp_empid,a.nsemp_bionum,a.nsemp_brcode from rfc_prf_nsemp a where a.nsemp_bionum=12476")or die();
while($row=mysqli_fetch_array($queryname)){
$fname=$row[0];
$mname=$row[1];
$lname=$row[2];
$suffix=$row[3];
$empid=$row[4];
$bionum=$row[5];
$brcode=$row[6];
}
echo"<hr/>
<h3><b>Name:</b>$fname $mname[0] $lname $suffix<br/>
<b>Employee No:</b> $empid<br/>
<b>Branch:</b> $brcode<br/></h3>
<table class='x'><tr>";
foreach($period as $date){
$x = $date->format("Y-m-d");
$day = date('l', strtotime($x));
if($day=="Sunday"){$background="#00e673";}else if($day=="Saturday"){$background="#ffff99";}else{$background="#ffffff";}
echo"<td height='150px' style='border-style: solid; background:$background'>
<b><font color='#1a1aff'>$x<br/>$day</font></b><br/><br/>";
$query=mysqli_query($conn,"select dtr_time,(CASE in_or_out WHEN 0 THEN 'IN' WHEN 1 THEN 'OUT' END) AS status,datacid from rfc_bio_dtr where dtr_date='$x' and empbio='12476';")or die(mysqli_error());
$check="";
while($row=mysqli_fetch_array($query)){
echo "$row[0] $row[1] <br/>";
$check .=$row[1];
}
if(preg_match('(IN)', $check) && preg_match('(OUT)', $check)) { echo" ";}else{
$queryReason=mysqli_query($conn,"select tk_reason_id,tk_reason_desc from rfc_timekeeping_param;")or die();
?>
<form id="myform">
<select name="reason" value="reason">
<?php
while($row=mysqli_fetch_array($queryReason)){
echo "<option value='$row[0]'>$row[1]</option>";
}
?></select>
<textarea name="notes" value="notes"></textarea><br>
<input name="submit" type="submit" value="Submit">
</form><?php
}
echo"</td>";
}
}
?>
</body>
Here's savetodb.php
<?php
require("../connmysql.php");
$fname=$_POST['reason'];
$lname=$_POST['notes'];
$queryx=mysqli_query($conn,"insert into test (fname,lname) values ('$fname','$lname');")or die(mysqli_error());
?>