how to use php variable in a external javascript file by ajax? - ajax

Basically, I want to use a php variavle in a external Js file by ajax. For instance:
in a example.php file
<?php
$a="123"
//and then I want to call a show() function through a onclick event later
<xxxxxxxxxxxxx...... onclick="show()">;
?>
in another example2.js file there is a function
show()
{
var b
// I want to assign $a's value to this variable b here.=> b=a
//but I only want to do this way by using ajax.
}
Is there anybody can tell me how to do that? Thanks.

did you try to insert the ajax_object.responceText into a value in a and then get it with javascript?

Related

How to call helper function in laravel 5.5

I am using laravel 5.5. I have created a helper.php in app\Http. I am calling this helper in my blade file by using
{!! Helper::functionName() !!}
this is working fine. but i want to hold this helper result in a variable like
{!! $Result=Helper::functionName() !!}
But currently this is printing this result. How to solve this. please help.
So that i can make any if condition on this $Result.
In my helpers.php
namespace App\Http\Helpers;
class Helper
{
public static function functionName()
{
return "mydata";
}
}
There is no point to use helper like this. You should run the helper in controller and pass calculated data into view. In most cases you shouldn't set any variables in views or make any calculations - those should be passed from controller to view and view should only use them.
In this case, you can use "<?php ?>".
So result:
<?php $Result=Helper::functionName(); ?>
may be this is not possible because in laravel "{{}}" this means echo "" so by default it will print the value. return the value from helper function and use in your blade

how i pass multiple variables from view to controller in codeigniter inside form_open

my view page
i have variables $month,$year,$id.in my viewpage.i want to pass this to controller function using echo form_open for updation
echo form_open('money_c/updatemanualdata/'.$id,$month,$year);?>
how will call this in my controller money_c
function updatemanualdata('')..?>here what will i put please help
If your form action will be
echo form_open('money_c/updatemanualdata/'.$id.'/'.$month.'/'.$year);
You can catch that in your controller using url segment like this.
be sure to load the url helper $this->load->helper('url')
function updatemanualdata($id, $month, $year){}

How to use AJAX in Joomla component to load the State field based on the country selected?

Inside a component's view, I have something like this:
<?php echo TestcompHelperFind::loadStates(...); ?>
<?php echo TestcompHelperFind::loadCounties(...); ?>
The above static functions load <select> dropdowns with the state names and countries respectively.
The class TestcompHelperFind is located in the file /administrator/components/com_testcomp/helpers/find.php.
How do I load States dropdown list based on the country selected using AJAX? I'm not sure what url I should provide in the ajax function.
On the client, you will need a function that watches the country select for changes, and when it happens calls the appropriate url with a callback that will populate the counties select.
On the server, you need to output the select content.
Since you have the html output already working, let's use this approach. As an alternative you could have your server method return a json object and use the javascript to parse it and populate the select. But let's stick to html communication, i.e. the server returns the html contents of the select.
1. On the server
1.a. Output the counties select
We only need to return the result of the TestcompHelperFind::loadCounties(...); to the ajax call. This is achieved easily writing a new method in the component's controller, i.e. the controller.php in the root of the component folder or one of the sub-controllers if appropriate. It's up to you to place it in a meaningful spot.
Inside the controller simply add a new public task such as
class SomethingController extends JController
{
public function getCountiesHTML() {
$input = JFactory::getApplication()->input;
$country = $input->getCMD('filter_country');
// load helper if necessary, then:
echo TestcompHelperFind::loadCounties($country);
exit; // this will stop Joomla processing, and not output template modules etc.
}
Please note the exit; at the end, this will make Joomla output only the component's output (our echo) and not the whole template/modules etc.
1.b Add an ID to the country and county selects so that it will be possible to manipulate them on the client; I'll assume filter_country and filter_county ;
2. On the client
you will want to invoke the url
index.php?option=com_something&action=getCountiesHTML&filter_country=UK
when the country select is changed. It will also need to cancel any pending requests to avoid overlapping messages. To keep things simple, let's assume you use a library to handle Ajax, I'll write an example for jQuery:
<script>
var xhr;
jQuery(function($) {
$('#filter_country').change(function(){
var filterCountry = $('#filter_country').val();
if (xhr && xhr.abort) {xhr.abort();xhr=false;}
xhr = jQuery.ajax(
url: 'index.php',
data: 'option=com_something&task=getCountiesHTML&filter_country='+filterCountry,
success: function(data){
jQuery('#filter_county').replaceWith(data);
}
);
});
});
</script>
For cancelling the previous request, please see a dedicated answer such as this one.

call cakePHP element via Ajax

just a quick question, Is it possible to call a cakePHP element via jQuery Ajax? I know the standard way to call an element in cakePHP is:
<?php echo $this->element('path_to_element', 'data_to_send_to_element'); ?>
But what if I wanna call my element inside the $.ajax or .load() function? How do I achieve this?
Thank you
To call anything in Cake, by Ajax or otherwise, you need to define an action in a controller. You could create a view too, but you can also have the action render an element directly by setting the viewPath. Example:
class MyController extends AppController {
// Apply Ajax layout automatically
var $components = array('RequestHandler');
function doSomething() {
$this->autoRender = false;
... // set parameters needed by the element...
// render an element
$this->viewPath = 'elements';
$this->render('path_to_element');
}
}

How to call the controller variable in view file using joomla?

I am new in joomla, My code is like this
//on controller
function listing()
{
JRequest::setVar( 'view', 'hello' );
JRequest::setVar('hidemainmenu', 0);
parent::display();
}
//on view.html.php
i want to fetch this 'hidemainmenu'
How can i fetch can anyone help??
If the code above is that of your view.html.php file then you can pass the variable through to your template file by using a line like so:
$this->assignRef( 'hidemainmenu', $hidemainmenu);
Then in your tmpl/default.php file for example you can access this variable like so:
$this->hidemainmenu
If the code is in the Controller :
get the Variable in the view.html.php
as
$hidemainmenu = JRequest::getVar('hidemainmenu');
Try this in the view.html.php or default.php

Resources