MichaelMcDonald
Posts: 640
|
Posted: 12/07/2016, 10:07 AM |
|
Following the file upload/insert (ajax success) a 1 second delay and then trigger click a button on the page to refresh a div .....
This uses AJAX post and a CCS Before Initialize Page event
Once the file is uploaded a quick check that it is in fact a .pdf and not a .txt file with php tags....
On your HTML button "onclick=insertinspol();"
The JS... (is jquery but getting success on Update Panels calling functions with direct bind on the button)
<script>
function insertinspol(){
var inslictypeid = $('#ModalSupplierPanel2recordInspolinslictypeid').val();
var datestart = $('#ModalSupplierPanel2recordInspoldatestart').val();
var dateend = $('#ModalSupplierPanel2recordInspoldateend').val();
var insurerauth = $('#ModalSupplierPanel2recordInspolinsurerauth').val();
var inslicnumber = $('#ModalSupplierPanel2recordInspolinslicnumber').val();
var file = $('#ModalSupplierPanel2recordInspolFileUpload1FileControl').val();
var datestarttimestamp = Date.parse($('#ModalSupplierPanel2recordInspoldatestart').val());
var dateendtimestamp = Date.parse($('#ModalSupplierPanel2recordInspoldateend').val());
if(file !== ''){
var requestfileuploadext = file.split('.')[1];
if(requestfileuploadext !== "pdf"){
$('#ModalSupplierPanel2recordInspolerrormessage').val("Invoice Is Not A .pdf");
}
}
if(file == ''){
$('#ModalSupplierPanel2recordInspolerrormessage').val("Document is required.");
}
if(inslicnumber== ''){
$('#ModalSupplierPanel2recordInspolerrormessage').val("Insurance policy or Licence number is required.");
}
if(insurerauth == ''){
$('#ModalSupplierPanel2recordInspolerrormessage').val("Insurer or Licencing Authority is required.");
}
if(datestart !== '' && dateend !== ''){
if(datestarttimestamp >= dateendtimestamp){
$('#ModalSupplierPanel2recordInspolerrormessage').val("Date Start must precede Date End.");
}
}
if(dateend == ''){
$('#ModalSupplierPanel2recordInspolerrormessage').val("Date End is required.");
}
if(datestart == ''){
$('#ModalSupplierPanel2recordInspolerrormessage').val("Date Start is required.");
}
if(inslictypeid == ''){
$('#ModalSupplierPanel2recordInspolerrormessage').val("Insurance Policy or Licence Type is required.");
}
if(file !== '' && inslicnumber !== '' && insurerauth !== '' && dateend !== '' && datestart !=='' && inslictypeid !== '' && requestfileuploadext == 'pdf'){
$( "#ModalSupplierPanel2recordInspolButton_Insert" ).hide();
var file_data = $('#ModalSupplierPanel2recordInspolFileUpload1FileControl').prop('files')[0];
var form_data = new FormData();
form_data.append('file', file_data);
form_data.append('inslictypeid', inslictypeid);
form_data.append('datestart', datestart);
form_data.append('dateend', dateend);
form_data.append('insurerauth', insurerauth);
form_data.append('inslicnumber', inslicnumber);
$.ajax({
url: 'insertinspol.php', // point to server-side PHP script
dataType: 'text', // what to expect back from the PHP script, if anything
cache: false,
contentType: false,
processData: false,
data: form_data,
type: 'post',
success: function(phpReturnResult){
setTimeout(function(){
$( "#ModalSupplierPanel2recordInspolButton_Reset" ).trigger( "click" );
}, 1000);
}
});
}
}
<script>
The php...
Page BeforeInitialize Event
if(CCGetSession("supplierid") > 0){
$supplierid = CCGetSession("supplierid");
$inslictypeid = CCGetFromPost("inslictypeid");
$datestart = CCGetFromPost("datestart");
$datestarttimestamp = CCGetFromPost("datestarttimestamp");
$dateend = CCGetFromPost("dateend");
$dateendtimestamp = CCGetFromPost("dateendtimestamp");
$insurerauth = CCGetFromPost("insurerauth");
$inslicnumber = CCGetFromPost("inslicnumber");
if($inslictypeid > 0 and $datestart !== '' and $dateend !== '' and $insurerauth !== '' and $inslicnumber !== '' and $supplierid > 0){
$datestartyear = substr($datestart,7,4);
$datestartmonth = substr($datestart,3,3);
if($datestartmonth = "Jan"){
$datestartmonth = "01";
}
if($datestartmonth = "Feb"){
$datestartmonth = "02";
}
if($datestartmonth = "Mar"){
$datestartmonth = "03";
}
if($datestartmonth = "Apr"){
$datestartmonth = "04";
}
if($datestartmonth = "May"){
$datestartmonth = "05";
}
if($datestartmonth = "Jun"){
$datestartmonth = "06";
}
if($datestartmonth = "Jul"){
$datestartmonth = "07";
}
if($datestartmonth = "Aug"){
$datestartmonth = "08";
}
if($datestartmonth = "Sep"){
$datestartmonth = "09";
}
if($datestartmonth = "Oct"){
$datestartmonth = "10";
}
if($datestartmonth = "Nov"){
$datestartmonth = "11";
}
if($datestartmonth = "Dec"){
$datestartmonth = "12";
}
$datestartday = substr($datestart,0,2);
$datestart = $datestartyear."-".$datestartmonth."-".$datestartday;
$datestarttimestamp = strtotime($datestart);
$dateendyear = substr($dateend,7,4);
$dateendmonth = substr($dateend,3,3);
if($dateendmonth = "Jan"){
$dateendmonth = "01";
}
if($dateendmonth = "Feb"){
$dateendmonth = "02";
}
if($dateendmonth = "Mar"){
$dateendmonth = "03";
}
if($dateendmonth = "Apr"){
$dateendmonth = "04";
}
if($dateendmonth = "May"){
$dateendmonth = "05";
}
if($dateendmonth = "Jun"){
$dateendmonth = "06";
}
if($dateendmonth = "Jul"){
$dateendmonth = "07";
}
if($dateendmonth = "Aug"){
$dateendmonth = "08";
}
if($dateendmonth = "Sep"){
$dateendmonth = "09";
}
if($dateendmonth = "Oct"){
$dateendmonth = "10";
}
if($dateendmonth = "Nov"){
$dateendmonth = "11";
}
if($dateendmonth = "Dec"){
$dateendmonth = "12";
}
$dateendday = substr($dateend,0,2);
$dateend = $dateendyear."-".$dateendmonth."-".$dateendday;
$dateendtimestamp = strtotime($dateend);
if($dateendtimestamp > $datestarttimestamp){
$db = new clsDBfm();
$SQL = "INSERT INTO inslic (inslictypeid, datestart, dateend, insurerauth, inslicnumber, supplierid) VALUES ('$inslictypeid', '$datestart', '$dateend', '$insurerauth', '$inslicnumber', '$supplierid')";
$db->query($SQL);
$idinslic = mysql_insert_id();
$reference = $idinslic;
$now = TIME();
$UserID = CCGetSession("UserID");
$reference = $UserID.$now.$idinspol.".pdf";
$SQL= "UPDATE inslic SET filename = '$reference' WHERE idinslic = '$idinslic'";
$db->query($SQL);
$temp = explode(".", $_FILES["file"]["name"]);
$newfilename = $reference;
move_uploaded_file($_FILES['file']['tmp_name'], '../inslic/' . $newfilename);
sleep(1);
$filename = $newfilename;
$folderpath = '../inslic/';
$filepath = $folderpath.$filename;
$fileContents = file_get_contents($filepath);
if(stristr($fileContents,"<?php")){
unlink($filepath);
}
}
}
}
_________________
Central Coast, NSW, Australia.
|