Redirect failed in codeigniter - codeigniter

I am trying to get the fc_id of the immunization then past it in profiling but the problem is the redirect() doesn't work. I successfully transfer my fc_id but the problem is i can't go to profiling view when i click the button.
Here some photo to understand my question clearly
So here's my code.
View:
$(document).on("click", "#view_profile", function(e){
e.preventDefault();
var view_id = $(this).attr("value");
$.ajax({
url: "<?php echo base_url(); ?>immunization/view_profile",
type: "post",
dataType: "json",
data: {
view_id: view_id
}
});
});
View Profile
Immunization Controller:
public function view_profile(){
$this->load->helper('url');
$this->load->library('session');
if ($this->input->is_ajax_request()) {
$view_id = $this->input->post('view_id');
$this->session->set_tempdata('view_id', $view_id);
redirect('profiling/index');
}
}
Profiling Controller:
public function index(){
$this->load->helper('url');
$this->load->library('session');
$this->load->view('profiling');
}

You are trying to redirect in an ajax request and thats not gonna work. Just post back some message if everything is ok and in ajax succes check that message and redirect from there with location.href = yoururl like:
In your php:
$this->session->set_tempdata('view_id', $view_id);
echo json_encode('msg' => 'ok');
js:
$.ajax({
url: "<?php echo base_url(); ?>immunization/view_profile",
type: "post",
dataType: "json",
data: {
view_id: view_id
}
success: function(resp){
if (resp.msg == 'ok') location.href = 'profiling/index';
}
});

Related

wordpress ajax form i found 404 when i submit it

i found 404 error
var endpoint='';
var bookingform=$("#BookingForm").serialize();
jQuery.ajax({
url: endpoint,
type: "POST",
data:{'action':'submitform1','bookingform':bookingform},
processData: false,
contentType: false,
success: function (result) {
$("#propertydata").html(dat.data)
},
error: function(dat1){
alert("error");
}
You are sending ajax to empty URL -> var endpoint='';
You need to send it to wordpress ajax url like this:
<script type="text/javascript">
var endpoint = "<?php echo admin_url('admin-ajax.php'); ?>";
</script>
You need to paste the above code in header.php file of your theme.
Secondly, in the success function, you are using the variable that is not defined (dat) you need to use something like this:
jQuery.ajax({
url: endpoint,
type: "POST",
data:{'action':'submitform1','bookingform':bookingform},
success: function (result) {
var dat = JSON.parse(result);
$("#propertydata").html(dat.data)
},
error: function(dat1){
alert("error");
}
});

Unable to receive data using AJAX post in Laravel

I am trying to send some variables(data),one is the checkbox text, and other is the value in the textarea field to the controller in Laravel through ajax, below is the script:
<script>
$('#btn1').on('click', function() {
$('input[type="checkbox"]').on('click', function() {
var aa=$(this).next('label').text();
var bb=$('textarea#txt2').val();
$.ajaxSetup({
headers:
{'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')}
});
$.ajax({
type: "POST",
url: "/masterdata",
//dataType: 'json',
data: {aa,bb},
success:function(){
console.log(data);
}
,error:function(){
console.log("Error!!!!");
}
});
});
});
</script>
On trying to retrieve the request values in the controller, only the request token gets displayed, and the ajax function doesn't display the success or error message either.What am I missing here?
you need to set a variable on ajax success like this:
$.ajax({
type: "POST",
url: "/masterdata",
//dataType: 'json',
data: {aa,bb},
success:function(response){
console.log(response);
}
,error:function(){
console.log("Error!!!!");
}
});
Try your data as in this format:
data:{'posa': aa, 'posb': bb},
and
success:function(data){
console.log(data);
}
Hope this helps.
i found two issue in your ajax request.
you have to change data: {aa,bb}, to object like data: {aa : aa, bb : bb},
Your success response log is wrong. Current code success:function(), it should be success:function(data)
Full output of new code is :
<script>
$('#btn1').on('click', function() {
$('input[type="checkbox"]').on('click', function() {
var aa=$(this).next('label').text();
var bb=$('textarea#txt2').val();
$.ajaxSetup({
headers:
{'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')}
});
$.ajax({
type: "POST",
url: "/masterdata",
dataType: 'json',
data: {aa : aa, bb : bb},
processData: false,
cache: false,
async :false,
success:function(data){
console.log(data);
}
,error:function(){
console.log("Error!!!!");
}
});
});
});
</script>
To get parameter value in controller use
/**
* Store.
*
* #param Request $request
* #return Response
*/
public function store(Request $request)
{
$aa = $request->input('aa');
$bb = $request->input('bb');
//Your code here
}
<script>
$('#btn1').on('click', function() {
$('input[type="checkbox"]').on('click', function() {
var aa=$(this).next('label').text();
var bb=$('textarea#txt2').val();
$.ajaxSetup({
headers:
{'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')}
});
$.ajax({
type: "POST",
url: "/masterdata",
//dataType: 'json',
data: 'aa=test'+'&bb=test',
success:function(){
console.log(data);
}
,error:function(){
console.log("Error!!!!");
}
});
});
});
</script>

CodeIgniter - Nothing is shown in the view

I don’t know what the problem is. I have an ajax which sends username to the controller:
function my_profile(username){
$.ajax({
url: "member/my_profile",
type: "get",
data: "username="+username,
success: function(){
window.location.href = 'member/my_profile';
}
});
}
And this is my controller:
function my_profile(){
$username = $this->input->get('username');
$data['username'] = $username;
$this->load->view('my_profile' , $data);
}
I have already echo the $username to test that it can alert(msg) from the ajax. It works just find. Problem is nothing is shown in my view:
<h1>My Profile</h1>
<?php
echo $username;
?>
I don’t know why. I tried initialized $data['username'] = 'adam' and this works.
The problem is your window.location.href = 'member/my_profile';. This will redirect you to the profile page without any username value.
You probably want to do:
window.location.href = 'member/my_profile?username='+username;
Though, I still don't understand why you have that AJAX call there. Couldn't you just do:
function my_profile(username){
window.location.href = 'member/my_profile?username='+username;
}
Your AJAX call is loading the page then discarding the contents, I don't think you need it here.
$.ajax({
url: "member/my_profile",
type: "get",
data: "username="+username,
success: function(){
window.location.href = 'member/my_profile';
}
});
Should be :
$.ajax({
url: "member/my_profile?username=" + username,
type: "get",
success: function(){
window.location.href = 'member/my_profile';
}
});

Redirecting after Ajax post

I want the success on ajax post to go to the home page. For some reason I keep doing it wrong. Any idea what I should do to fix this?
window.APP_ROOT_URL = "<%= root_url %>";
Ajax
$.ajax({ url: '#{addbank_bankaccts_path}',
type: 'POST',
beforeSend: function(xhr) {xhr.setRequestHeader('X-CSRF-Token', '#{form_authenticity_token}')},
dataType: "json",
data: 'some_uri=' + response.data.uri ,
success: function(APP_ROOT_URL) {
window.location.assign(APP_ROOT_URL);
}
});
success: function(response){
window.location.href = response.redirect;
}
Hope the above will help because I had the same problem
You can return the JSON from server with redirect status and redirect URL.
{"redirect":true,"redirect_url":"https://example.com/go/to/somewhere.html"}
And in your jQuery ajax handler
success: function (res) {
// check redirect
if (res.redirect) {
window.location.href = res.redirect_url;
}
}
Note you must set dataType: 'json' in ajax config. Hope this is helpful.
Not sure why, but window.location.href did not work for me. I ended up using window.location.replace instead, which actually worked.
$('#checkout').click(function (e) {
e.preventDefault();
$.ajax('/post/url', {
type: 'post',
dataType: 'json'
})
.done(function (data) {
if (data.cartCount === 0) {
alert('There are no items in cart to checkout');
}
else {
window.location.replace('/Checkout/AddressAndPayment');
}
});
});

How to get the params of AJAX-query on the server-side in CodeIgniter?

I have the AJAX-code, which calls to controller of CodeIgniter's back-end:
<script>
$(document).ready(function(){
$("#select_bank").change(function(){
selected_bank = $("#select_bank option:selected").text();
$.ajax({
url:'<?=base_url().'atm/select_region/&'+selected_bank; ?>',
success:function(msg){
}
});
});
});
So, I want to get this params in the controller (CodeIgniter), but, because of this is not at the any form, using the
$bank = $this->input->post('')
gives no effect. really, I'd like to clarify this moment
You need to tell the ajax function that you're sending POST data
$.ajax({
type: "POST",
dataType: 'html',
url: <?= base_url ?> + "atm/select_region",
data: {nameofpostvariable:valuethatyousend},
success: function(output){
},
error: function(output){
alert('error');
}
});
On the line data: {nameofpostvariable:valuethatyousend}, you are creating a $_POST['nameofpostvariable']

Resources