I am trying sent one data from one view to another controller and set the data for another view. Here is ajax code working fine
$.ajax({
url: "<?php echo Router::url(array('controller'=>'users','action'=>'exchange_process'));?>",
type: "POST",
data: {"point_origin": point_origin },
success: function(){
alert("success");
}
});
In controller I have received this data by bellow code
public function exchange_process()
{
if($this->request->is(array('post', 'ajax'))) {
$point_origin=$_POST['point_origin'];
}
$this->set("pointorg",$point_origin);
}
In another view I have tried
<?php echo $pointorg ?>
It's not working.
if I try
public function exchange_process()
{
if($this->request->is(array('post', 'ajax'))) {
// $point_origin=$_POST['point_origin'];
}
$point_origin=123;
$this->set("pointorg",$point_origin);
}
It's working,but if I try
public function exchange_process()
{
if($this->request->is(array('post', 'ajax'))) {
// $point_origin=$_POST['point_origin'];
$point_origin=123;
}
$this->set("pointorg",$point_origin);
}
It's not working.
change your if clause to:
if($this->request->is('post') || $this->request->is('ajax')) {
and it should be working fine
CakePHP 2.3 doesnot support array of type headers, please check
http://book.cakephp.org/2.0/en/appendices/2-4-migration-guide.html#cakerequest
you will need to check individually or use Ajax only if request is to be sent using Ajax only.
Related
Ajax works well for pre-existing forms. But if I add a new form using it and submit it, then I get to the method page.
Added a new form, filled in the field and clicked Submit: http://joxi.ru/DmBOW4KUzep962
I get to the method page, instead of displaying the result in the console: http://joxi.ru/EA4P710TOekbOA
After reloading the page, everything works well.
What could be the matter, tell me please.
Front:
$('.add-answer-form').on('submit', function (e) {
e.preventDefault();
$.ajax({
type: "POST",
url: `/admin/courses/48/lessons/96/answerAdd`,
data: $(this).serialize(),
success: function (data) {
console.log('!!!!!', data);
}
});
});
Back:
public function answerAdd(Request $request, Course $course, Lesson $lesson, Test $test, Answer $answer){
$this->answer->fill($request->all())->save();
return response ()->json ($this);
}
I solved this issue as follows:
Instead of this:
$ ('.add-answer-form').on('submit', function (e) {
Added this:
$ ('#quest-add-form').on('submit', '.add-answer-form', function (e) {
This is suitable in cases where you need to work with the form that was added by Ajax.
I have the Ajax request
Here is the code of script
<script>
$('#variant_model').change(function(){
var value = $('select#variant_model').val();
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
$.ajax({
type:"POST",
url: "showVariantModel",
data: value,
success: function(data){
alert(data);
}
})
});
</script>
When I put in my web.php the code
Route::post('/showVariantModel', function(){
if(Request::ajax()){
return var_dump(Response::json(Request::all()));
}
});
Everything looks fine and I receive the response in my alert.
But when I want to put everything into controller I receive the error 500 in the console
Below I will add my code from my web.php and Controller.
Framework is Laravel 5.8
//web.php
Route::post('/showVariantModel', 'VariantsController#checkAttribute');
//VariantsController.php
public function checkAttribute()
{
if(Request::ajax()){
return var_dump(Response::json(Request::all()));
}
Who knows what am I doing incorrect, please give an advice...
Updating the error
https://i.stack.imgur.com/iDvvG.jpg
Thank's Md.Sukel Ali I updated my controller. Not it looks
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Variants;
class VariantsController extends Controller
{
public function checkAttribute(Request $request)
{
if($request->ajax()){
return response()->json($request->all());
}
}
}
Everything works fine now.
Thank you.
The closing tag is missing
public function checkAttribute()
{
if(Request::ajax()){
return Response::json(Request::all());
}
}
Instead of blinding guess what happening under the hood of ajax call. I suggest you to learn how to debug ajax request.
Go to your browser right click and Inspect then go to Network tab then you can see your request. Click on your request then look for response tab. There you can find exactly what happened.
public function checkAttribute(Request $request)
{
if($request->ajax())
{
return response()->json($request->all());
}
}
You need to add imports in your controller:
use Illuminate/Http/Request;
use Illuminate/Http/Response;
My table contains many rows from database. After clicking edit button in a row, I want to redirect it to another page. I am able get the data, but it cannot redirect to the other page.
I'm using bootstrap template, so there are different file to load on view. I do not know how to load view that consist of more than one files.
Ajax
$('#example2').on('click','#edit',function(){
var id_per = $(this).closest('tr').attr('id');
alert("ini"+id_per);
$.ajax({
url:"<?php echo base_url('c_dokter/ubahdt_perawatan/');?>" + id_per,
type:"POST",
data:{
id_perawatan : id_per,
id_pasien : id_pasien
},
dataType:'JSON',
success:function(data) {
},
error:function() {
alert('error ... ');
}
});
Controller
public function ubahdt_perawatan($id_perawatan){
$data['pasien']=$this->m_pasien->spes_Perawatan($id_perawatan);
if ($this->input->is_ajax_request()) {
echo json_encode($data);
exit;
}
$data['pasien']=$this->m_pasien->DataPerPasien($id_pasien)->result();
$data['sidebar']='member/dokter/sidebar_psn';
$data['content']='member/dokter/edit_perawatan';
$this->load->view('member/dokter/main',$data);
}
Edited answer:
public function ubahdt_perawatan($id_perawatan){
$data['pasien']=$this->m_pasien->spes_Perawatan($id_perawatan);
if ($this->input->is_ajax_request())
{
echo json_encode($data);
}
}
public function test()
{
$id_pasien=$this->uri->segment(2);
$data['pasien']=$this->m_pasien->DataPerPasien($id_pasien)->result();
$this->load->view('member/dokter/sidebar_psn');
$this->load->view('member/dokter/edit_perawatan');
$this->load->view('member/dokter/main',$data);
}
set path to test() in routes.php..
$route['test/(:any)']='controller/test';
then in ajax success function
success:function(data){
window.location.href="<?php echo site_url('test/'.data);";
},
try it.
I am trying to post value using javascript in form_dropdown('') but it is not posting data on form.
Jquery library is loaded. On alert it display record of dep_selected
JQUERY On view page
function get_subdepartment() {
var dep_selected = $('select[name=txtDept]').val();
$.ajax({
data: {
dep_selected: dep_selected,
},
type: 'POST',
url: 'addVacancy/getSubDept',
success: function(data){ //alert(dep_selected);
console.log(data);
$('.subdepartment').html(data);
}
})
}
Controller Page:
function getSubDept(){
if($this->input->post('txtDept')){
echo "getSubDept > Dept: ".$this->input->post('txtDept');
}
else{
echo "getSubDept > No Return";
}
}
It display "getSubDept > No Return" Please help.
Well it's obvious, that in your controller you are requesting a post value by the key txtDept, but you don't have that when you send data with AJAX. What you have now is:
var dep_selected = $('select[name=txtLocation]').val();
and
data: {
dep_selected: dep_selected,
},
So you should simply change the first line in your controller method to
if($this->input->post('dep_selected')) {
I suggest you to do some reading about POSTing values with AJAX and how to work with JSON.
Assume that I have a public function in IndexController called test():
public function test(){
//some code here
}
In index.phtml view file, I want to use JQUERY AJAX to call test() function but have no idea about this.
Code:
Click me to call test() function()
<script>
callTestFunction = function(){
$.ajax({
type: "POST",
Url: ***//WHAT SHOULD BE HERE***
Success: function(result){
alert('Success');
}
});
}
</script>
I would suggest writing an action for it. If there is logic inside of test() that you need other actions to be able to use, the factor that out.
There are a couple of reasons for having it be its own action:
You can test the results of the action directly instead of having to go through a proxy.
You can take advantage of context switching depending on if you need JSON returned or HTML
It is an action that you are trying to hit via AJAX, so there's no need to hide it.
Remember that not every action has to be its own full fledged page. One thing to make sure you do is disabling the auto-rendering of the view inside this action so it doesn't complain about not being able to find it.
public function ajaxproxyAction(){
if (is_callable($_REQUEST['function_name'])) {
return call_user_func_array($_REQUEST['function_name'], $_REQUEST['function_params'])
}
}
<script>
callTestFunction = function(){
$.ajax({
type: "POST",
Url: '/controller/ajaxproxy/',
data: { function_name: 'echo', function_params: 'test' }
Success: function(result){
alert('Success');
}
});
}
</script>
public function ajaxproxy2Action(){
if (method_exists($this, $_REQUEST['function_name'])) {
$retval = call_user_func_array(array($this, $_REQUEST['function_name']), $_REQUEST['function_params']);
echo json_encode(array('function_name' => $_REQUEST['function_name'], 'retval' => $retval));
die;
}
}
just think about this way ;)