HTML/CSS/JS

Jquery Ajax submit form upload

jQuery Function Code

jQuery(document).on(‘click’, ‘.button-custom’, function(){

box1 = new ajaxLoader(“html”);

type_submit = jQuery(this).attr(‘id’);

var data = new FormData();
data.append( ‘type_submit’, type_submit);
data.append( ‘text_1’, jQuery(‘.text_1’).val());
data.append( ‘text_2’, jQuery(‘.text_2’).val());
data.append( ‘text_3’, jQuery(‘#text_3’).val());
data.append( ‘id’, jQuery(‘.hcp_info_id’).val());
data.append( ‘fileToUpload_1’, jQuery(‘input[name=fileToUpload_1]’)[0].files[0]);
// data.append( ‘fileToUpload_2’, jQuery(‘input[name=fileToUpload_2]’)[0].files[0]);
data.append(“action”, “updateFormDraft”);
jQuery.ajax({
type: “POST”,
url: ajaxurl,
contentType: false,
processData: false,
data: data,

success: function(data){
if(data == ‘logged’){
box1.remove();
jQuery(‘.message’).text(‘Form data can not update’);
return false;
}

if( /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) ) {
box1.remove();
jQuery(‘#popup_box_form’).hide();

id = null;
alert(‘Record saved successfully’);
return false;
}else{

box1.remove();
jQuery(‘#popup_box_form’).hide();
jQuery(‘.container’).show();
jQuery(‘#container’).hide();
console.log(type_submit);
if(type_submit == ‘submit_draft’){
jQuery(‘.message’).text(‘Record saved successfully’);
updateState(jQuery(‘.hcp_info_id’).val(), “WAITING_FOR_ADDITIONAL_HCP_INFORMATION”);
}else{
jQuery(‘.message’).text(‘Thank you. Your additionnal information for your contract has been sent.’);
updateState(jQuery(‘.hcp_info_id’).val(), “WAITING_FOR_FULLY_EXCUTED_AGREEMENT”);
}

focusToContractTable();

id = null;
return false;
}
},
error: function(){
if( /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) ) {
alert(“An error occurred”);
}else{
jQuery(‘.message’).text(‘An error occurred’);
box1.remove();
}
}
});
});

 

 

Controller:

public function updateFormDraft(){
global $wpdb;
$table = “wp_hcp_info”;
$id = $_POST[‘id’];
$text_1 = $_POST[‘text_1’];
$text_2 = $_POST[‘text_2’];
$text_3 = $_POST[‘text_3’];
$typeSubmit = $_POST[‘type_submit’];
if($typeSubmit == ‘back’){
$state = ‘WAITING_FOR_ADDITIONAL_HCP_INFORMATION’;
if($id){
$formData = array(
‘text_1’ => NULL,
‘text_2’ => NULL,
‘text_3’ => $text_3,
‘state’ => $state,
‘fileToUpload_1’ => NULL,
‘fileToUpload_2’ => NULL,
);

$condition = array(
‘ID’ => $id,
);

$res = $wpdb->update($table, $formData, $condition);
if(!$res){
var_dump($wpdb->last_query);
echo “logged”;
die();
return false;
}
}else{
return false;
}

}else{

if($typeSubmit== ‘submit_draft’){
$state = ‘WAITING_FOR_ADDITIONAL_HCP_INFORMATION’;
}else{
$state = ‘WAITING_FOR_FULLY_EXCUTED_AGREEMENT’;
}

$posted_data = isset( $_POST ) ? $_POST : array();
$file_data = isset( $_FILES ) ? $_FILES : array();
$data = array_merge( $posted_data, $file_data );

if (! function_exists( ‘wp_handle_upload’ ) ) {
require_once( ABSPATH . ‘wp-admin/includes/file.php’ );
}
$flag = false;
$upload_file = wp_upload_dir();
$css_dirname = $upload_dir[‘basedir’].’/upload/’;

$uploadedfile1 = $_FILES[‘fileToUpload_1’];

$upload_overrides = array( ‘test_form’ => false );

$oldData = $this->hcpModel->getHcpDataById($id);

$file1Old = $oldData[0]->fileToUpload_1;
$file2Old = $oldData[0]->fileToUpload_2;

$movefile1 = wp_handle_upload( $uploadedfile1, $upload_overrides );

$uploadedfile2 = $_FILES[‘fileToUpload_2’];

$upload_overrides = array( ‘test_form’ => false );

$movefile2 = wp_handle_upload( $uploadedfile2, $upload_overrides );

if($movefile1[‘url’]){
$file1Url = $movefile1[‘url’];
}else{
$file1Url = $file1Old;
}

if($movefile2[‘url’]){
$file2Url = $movefile2[‘url’];
}else{
$file2Url = $file2Old;
}
if($id){
$formData = array(
‘text_1’ => $text_1,
‘text_2’ => $text_2,
‘text_3’ => $text_3,
‘state’ => $state,
‘fileToUpload_1’ => $file1Url,
‘fileToUpload_2’ => $file2Url,
);

$condition = array(
‘ID’ => $id,
);

$res = $wpdb->update($table, $formData, $condition);
if(!$res){
var_dump($wpdb->last_query);
echo “logged”;
die();
return false;
}
// $displayOption = get_option(‘hcp_contract_display_options’);

// $showPdfColumn = $displayOption[“pdf_column”];
// if($showPdfColumn){
// $this->pdfCreat($id);
// }
}else{
return false;
}
}
}

 

 

Class ShortCode Call Ajaxj:

 

class ShortCode {

private $contractControler;

public function __construct(){
$this->init();
$this->contractControler = new ContractController;
}

public function init(){
add_shortcode(‘hcp_contract’, array($this,’hcpContract’));
add_action(‘update_display_name’, array($this,’fixUserDisplayName’));
add_action(‘wp_enqueue_scripts’,array($this,’loadCssJs’));
add_action(‘wp_ajax_updateConsentAllData’, array($this,’updateConsentAllData’));
add_action(‘wp_ajax_updateConsentData’, array($this,’updateConsentData’));
add_action(‘wp_ajax_searchHcpText’, array($this,’searchHcpText’));
add_action(‘wp_ajax_updateFormDraft’, array($this,’updateFormDraft’));
add_action(‘wp_ajax_getDataPopupById’, array($this,’getDataPopupById’));
add_action(‘wp_ajax_showPdfSignature’, array($this,’showPdfSignature’));
add_action(‘wp_ajax_showPdfNoSignature’, array($this,’showPdfNoSignature’));
add_action(‘wp_ajax_showConfirmSignature’, array($this,’showConfirmSignature’));
add_action(‘wp_ajax_updateFormConfirmedStatus’, array($this,’updateFormConfirmedStatus’));

}

public function hcpContract($atts, $content = null) {
global $wpdb, $current_user;

if (!defined(‘ABSPATH’)) {
/** Load WordPress Bootstrap */
require_once(‘../../../../wp-load.php’);
}
if (is_user_logged_in()) {

if (in_array(“subscriber”, $current_user->roles)) {
?>
<p style=”text-align: center;font-size:25px;color:red”><?php echo $this->contractControler->translator->__(“Your accound has not been approved yet. Please try again later”);?>.</p>
<?php
} else {
echo $this->contractControler->showTextOptionFromPage();
//Display search form
echo $this->contractControler->buildHtmlSearchForm();

//Display HCP table
echo $this->contractControler->showTableContract();
}
} else {
?>
<p style=”text-align: center;font-size:25px;color:red”><?php echo $this->contractControler->translator->__(“Please login to view this page”);?></p>

<?php
}
return do_shortcode($content);
}

/*
Fix first last on register.
*/

public function fixUserDisplayName() {
$users = get_users();
foreach ($users AS $user) {

//set the display name
$info = get_userdata($user->ID);

$display_name = trim($info->first_name . ‘ ‘ . $info->last_name);

if (!$display_name)
$display_name = $info->user_login;

$args = array(
‘ID’ => $user->ID,
‘display_name’ => $display_name
);
wp_update_user($args);
}
}

public function loadCssJs() {
$contractPath = plugins_url();
wp_enqueue_script( ‘custom’, $contractPath.’/hcp-contract/js/custom.js’);
wp_enqueue_script( ‘sorttable’, $contractPath.’/hcp-contract/js/sorttable.js’);
wp_enqueue_script( ‘list’, $contractPath.’/hcp-contract/js/list.js’);
wp_enqueue_style( ‘contractstyle’, $contractPath.’/hcp-contract/css/contractstyle.css’ );

$upload_dir = wp_upload_dir();
$css_dirname = $upload_dir[‘basedir’].’/css/’;
$css_basedir = $upload_dir[‘baseurl’].’/css/’;
if (file_exists( $css_dirname)){
$files = scandir($css_dirname);
foreach($files as $file){
$ext = pathinfo($file, PATHINFO_EXTENSION);
if($ext == ‘css’){
$namePathCss = $css_basedir . basename($file);
$filename = basename($namePathCss);
wp_enqueue_style(basename($file), $namePathCss);
}
}
}

wp_enqueue_style( ‘datetimepicker’, $contractPath.’/log_manager/jquery.datetimepicker.css’ );
wp_enqueue_script( ‘datetimepicker’, $contractPath.’/log_manager/jquery.datetimepicker.full.min.js’);
// including ajax script in the plugin ajaxurl
?>

var ajaxurl = ”;

<?php
}
public function updateConsentAllData(){
$this->contractControler->updateConsentAllData();

}
public function updateConsentData(){
global $wpdb;
$this->contractControler->updateConsentData();
}

public function searchHcpText(){
global $wpdb;
$this->contractControler->searchHcpText();
}

public function updateFormDraft(){
global $wpdb;
$this->contractControler->updateFormDraft();
}

public function getDataPopupById(){
global $wpdb;
$this->contractControler->getDataPopupById();
}

public function showPdfSignature(){
global $wpdb;
$this->contractControler->showPdfSignature();
}
public function showPdfNoSignature(){
global $wpdb;
$this->contractControler->showPdfNoSignature();
}

public function showConfirmSignature(){
global $wpdb;
$this->contractControler->showConfirmSignature();
}

public function updateFormConfirmedStatus(){
global $wpdb;
$this->contractControler->updateFormConfirmedStatus();
}
}

$view = new ShortCode();

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s