How to get current window URI last segment - codeigniter

My current window URL http://192.168.20.2/vtp/attendance/rawAttendance and parameter form submit by this URL "<?php echo base_url(); ?>index.php/attendance/submitParam" in ajax. With this code below
$last = $this->uri->total_segments();
$data['lastSegment'] = $this->uri->segment($last);
I got the last URL segment but this not the current window URL segment, this is parameter form URL segment. How do I get my current window URL last segment in my submitParam controllerwhen I submit the parameter form.
submit param;
$("#submitparam").click(function (e) { // passing down the event
$.ajax({
url: "<?php echo base_url(); ?>index.php/attendance/submitParam",
type: "POST",
data: $("#param").serialize() + '&fromAjax=' + true,
success: function (data) {
$("#result").html(data);
},
error: function () {
alert("Fail")
}
});
e.preventDefault(); // could also use: return false;
});
controller:
public function submitParam() {
//post from view param
$round = $this->input->post('round', TRUE);
$batch = $this->input->post('batchid', TRUE);
$fromdate = $this->input->post('FromDate', TRUE);
$todate = $this->input->post('ToDate', TRUE);
//raw Attendance
$data['IDS'] = $this->AttendanceModel->raw_attendance_TID($batch);
$data['Dates'] = $this->AttendanceModel->raw_attendance_Data($batch,$fromdate,$todate);
//get Batch Attendance
$data['attendance'] = $this->AttendanceModel->get_attendance($batch,$fromdate,$todate);
//pass param to preview as attendance title
$data['batch']=$batch;
$data['fromDate']=$fromdate;
$data['toDate']=$todate;
//get url last segment
$last = $this->uri->total_segments();
$lastSegment = $this->uri->segment($last);
//load view by url last segment
if ($this->input->post("fromAjax")) {
$this->load->view('attendance/'.$lastSegment, $data );
}
}

Add a hidden field in your form with the name url_parameter. set the value of the last paramater which you want in your controller and get that field's value by post/get method.

Try this :
$record_num = end($this->uri->segment_array());

Related

how to fill field values using ajax in yii2?

I am using ajax in yii2.
How to fill fields without submit form. Without submitting the validation is working, but how to add field value.
In below code $this->name is my field name.
if($this->statusOk){
$this->name = "gana";
}else{
return $this->addError('branch_code', ' code can’t be found');
}
you can set your value in save function(in model) or before saving your model(in controller) ,and use custom validation just for validation.
Try this,custom ajax
$(document).ready(function () {
$("body").on("blur", "#id", function () {
var data = $("#nameField").val();
$.ajax({
url : "validURL",
type : "post",
data : {"sortcode":data},
success: function (response)
{
var json_obj = $.parseJSON(response);
if(json_obj.errorMessage) {
// do something
}else {
// do something
}

$_FILES not working with ajax submit in woocommerce checkout

I am try to upload file on woocommerce checkout page but I am not able to upload file. I have also try to get value using print_r($_FILES) in create_order function in class-wc-checkout.php file but it will return blank array
When I have remove class checkout from checkout form then it will working fine (without ajax submit)
I want to submit form without ajax but I need ajax remain on checkout page because I have add some extra price using ajax (update price without refresh)
My ajax call to add extra fee.
jQuery('#PageCount').change(function () {
var state = jQuery('#PageCount').val();
var current_price = jQuery('#carttot').val();
var data = {
action: 'woocommerce_apply_state',
security: wc_checkout_params.apply_state_nonce,
state: state,
current_price: current_price
};
jQuery.ajax({
type: 'POST',
url: wc_checkout_params.ajax_url,
data: data,
success: function (code) {
console.log(code);
if (code === '0') {
jQuery('body').trigger('update_checkout');
}
},
dataType: 'html'
});
return false;
});
and php code or woocommerce hooks to add extra fee
add_action('wp_ajax_woocommerce_apply_state', 'calculate_per_page', 10);
add_action('wp_ajax_nopriv_woocommerce_apply_state', 'calculate_per_page', 10);
function calculate_per_page() {
if (isset($_POST['state'])) {
global $woocommerce;
$weight = WC()->cart->cart_contents_weight;
$state = $_POST['state'];
$current_price = $_POST['current_price'];
$val = $current_price * $state -$current_price;
session_start();
$_SESSION['val'] = $val;
}
}
add_action('woocommerce_cart_calculate_fees', 'woo_add_cart_fee');
function woo_add_cart_fee() {
session_start();
$extracost = $_SESSION['val'];
WC()->cart->add_fee('Per page charges:', $extracost);
}
And my function to upload file..
add_action('woocommerce_checkout_update_order_meta', 'custom_checkout_field_update_order_meta');
function custom_checkout_field_update_order_meta( $order_id ) {
if ($_FILES['avatar']['name']) {
if ( ! function_exists( 'wp_handle_upload' ) ) {
require_once( ABSPATH . 'wp-admin/includes/file.php' );
}
$upload_dir = wp_upload_dir();
$path = $upload_dir['path'];
$fileName = $_FILES["avatar"]["name"];
$fileTmpLoc = $_FILES["avatar"]["tmp_name"];
$pathAndName = $path."/".$fileName;
$moveResult = move_uploaded_file($fileTmpLoc, $pathAndName);
update_post_meta( $order_id, 'uploaded_file_to_translate', $_FILES['avatar']['name']);
if ($_POST['billing_output_to_send'])
update_post_meta( $order_id, 'How do you want your translation sent to you?', esc_attr(htmlspecialchars($_POST['billing_output_to_send'])));
if ($_POST['PageCount'])
update_post_meta( $order_id, 'How many pages are in your documents?', esc_attr(htmlspecialchars($_POST['PageCount'])));
}
}
How it will possible?
$_FILES you can not get it at woocommerce_checkout_update_order_meta hook because of woocommerce serialize the form and submit form throw ajax,
as we know that form serialize can not submit files requests. but I have two solutions for doing this may be it work for you.
You can use a plugin woocommerce upload file in the checkout. you can easily find it on google
But if you want to upload custom files then you can use this
when onchange file fire an ajax and push file array in session when user checkout, get the files from the session and upload to the directory, and then destroy your session.
$('.fileupload').change(function(e){
var action = 'update_session_files';
e.preventDefault();
var fd = new FormData();
fd.append('action', action);
var files = $('input[name="file"]')[0].files[0];
fd.append('billing_artist_headshot', files);
$.ajax({
url: my_ajax_object.ajax_url,
type: 'post',
data: fd,
contentType: false,
processData: false,
success: function(response){
},
});
});
and ajax action
add_action( 'wp_ajax_nopriv_update_session_files', 'update_session_function' );
add_action( 'wp_ajax_update_session_files', 'update_session_function' );
function update_session_function(){
session_start();
$_FILES['billing_artist_headshot'];
// upload your file here with Wordpress and get the id.
}

zf2 and ajax I can't get the parametrs

I can not get the data passing from the controller ajax me there any solution?
button click action
$(".bajaAlumno").click(function () {
var urlform = "<?php echo $this->url(null, array('controller'=>'Academius','action' =>'bajaAlumnos' ) ); ?>";
var dato= $(this).attr('id');
var myData = {textData:dato};
$.ajax({
type:"POST",
url:"/Academius/bajaAlumnos",
data:{data:myData},
success: function(data){
//The callback function, that is going to be executed
//after the server response. data is the data returned
//from the server.
// Show the returned text
//$("#answer").text(data.text);
//$("#answer").text(data.text);
alert('enviado');
}
});
});
and controller
public function bajaAlumnosAction()
{
die(var_dump($this->params()->fromPost()));
}
one answer?

refresh Page after logged in

in the index Page, the user needs to login..
after login,
<?php
include("dbinit.php");
$text="";
$eadd = $_POST['eadd'];
$pass = $_POST['pass'];
if (filter_var($eadd, FILTER_VALIDATE_EMAIL)) {
$result = mysqli_query($link,"SELECT * FROM account WHERE Eadd='".$eadd."'");
if (mysqli_num_rows($result)<=0){
$text = "<font color=red>Invalid Emailaddress and Password Combination!</font>";
}
else
{
while($row = mysqli_fetch_array($result)){
$passH = $row['Pass'];
$passS = $row['hash'];
}
if(md5($pass.$passS) == $passH){
$_SESSION['account'] = $eadd;
$text = "<font color=red>Login Successful!</font>";
}else{
$text = "<font color=red>Invalid Emailaddress and Password Combination!</font>";
}
}
mysqli_free_result($result);
} else {
$text = "<font color=red>Invalid Emailaddress!</font>";
}
mysqli_close($link);
echo $text;
?>
in the index Page,
function login(){
var eadd = $('#eadd').val();
var pass = $('#pass').val();
$.ajax({
type: "POST",
url: "login.php",
data: {eadd:eadd, pass:pass}
}).done(function( result ) {
$("#loginMsg").html(result);
});
}
how can i redirect or refresh the Page after he logged in?
after loggedin, the index page must refresh..
do i need to Put window.history.pushState("", "", '/newpage');?
how to use it?
window.top.location.reload();
Use that in your ajax success callback
To redirect instead to a differnt page use:
window.top.location = '/some/page/withcats';
Use:
function login(){
var eadd = $('#eadd').val();
var pass = $('#pass').val();
$.ajax({
type: "POST",
url: "login.php",
data: {eadd:eadd, pass:pass}
}).done(function( result ) {
$("#loginMsg").html(result);
//you may want to check result has no errors or something
window.top.location.reload();
});
}
Error handling:
You might want to check for an error, so that if the login is unsuccessful you do not want to refresh the page. To do that relies on knowing what you php function will return E.g.:
function login(){
var eadd = $('#eadd').val();
var pass = $('#pass').val();
$.ajax({
type: "POST",
url: "login.php",
data: {eadd:eadd, pass:pass}
}).done(function( result ) {
//this will make sure the page only refreshes if login is succesful, if not display error
if(result === "<font color=red>Login Successful!</font>"){
window.top.location.reload();
}else{
$("#loginMsg").html(result);
}
});
}
how can i redirect or refresh the Page after he logged in?
by using a regular form submission instead of Ajax.

How to return ajax quired value in wordpress?

I have created a ajax call back but it returns an error value. The code is bellow which I'm using.
I'm sending value from checkboxes when it checked. and want to return value from postedfile listing_submit_demo.php
jQuery(function() {
jQuery('.list_category').bind('click',function() {
if(jQuery(this).is(':checked')) {
jQuery('#some_textarea').html(jQuery(this).val());
var id = jQuery(this).attr('id');
jQuery.ajax({
type:"POST",
data: {idvalue: id},
url: "<?php echo FRONTENDURL; ?>listing_submit_demo.php",
success:function(data){
alert(data);
}
});
}
});
});
In listing_submit_demo.php I'm getting checkbox id value and retrieving values from database and then returning the listed value but it returns value with debug error. So how to get proper value from it? The listing_submit_demo.php code is bellow:
<?php
//error_reporting(0);
$idvalue = $_POST['idvalue'];
global $cfield_tbl_name;
global $wpdb;
$query = "SELECT * FROM $cfield_tbl_name";
$val = $wpdb->get_results($query);
foreach($val as $x){
$cate = explode(',',$x->field_cate);
if(in_array($idvalue, $cate)){
$y .= $x->f_var_nm . ",";
}
}
echo $y;
//?>
Any help will be thankful in advance.
I have a suggestion that you can hook with the action "wp_ajax_$youraction" and "wp_ajax_nopriv_$youraction"
http://codex.wordpress.org/Plugin_API/Action_Reference/wp_ajax_%28action%29
The example is inside the link, wordpress ajax should work fine with these hook.

Resources