What is the best way to execute a codenighter controller/method from an external script?
Trying to integrate my existng CI app with a non-CI app and need to get output of CI and show it inside the other app.
I would take a look at either the xmlrpc library or rest (baseed on https://github.com/philsturgeon/codeigniter-restserver).
Another option is you could set up a controller/view method that outputs the info you need in a format that you can work with and use curl to get that page.
ajax.
http://api.jquery.com/jQuery.ajax/
<script src="/js/jquery.js"></script>
<div id="result"></div>
<script>
$.ajax({
url: 'controller/method',
success: function(data) {
$('#result').html(data);
alert('Load was performed.');
}
});
</script>
Related
I have a project in PHP using CodeIgniter in an HMVC approach.
Now, I run into a problem where I'm really confused what to do next.
Here's my situation:
I have a view which looks like this:
//logs_v_month.php
<div id="logs" class="tab-pane active">
</div>
<script type="text/javascript">
$(function(){
$.ajax({
url: "<?php echo load_global_assets('system/js/log.js')?>",
dataType: "script",
success: function(e){}
});
});
</script>
Then I have a controller:
//view.php
function display_logs_month(){
$data['logs_v'] = $this->load->view('logs_v_month');
echo json_encode($data);
}
Then I have another view which is the main view.
//logs.php
<div class="tab-content" id="logs_content">
<!--logs loaded via ajax call-->
</div>
<script type="text/javascript">
function view_month(group_id, start_end){
$.ajax({
type : "POST",
dataType: "json",
url : "<?=base_url()?>logs/view/display_logs_month",
data : {"group_id" : group_id},
success : function (data){
$("div#logs").remove();
$("#logs_content").append(data.logs_v);//logs_v
},
error : function(data){
console.log(data);
}
});
}
The logs.php view has an ajax call to load the logs_v_month.php view through the view.php controller.
My question is, how do I access JavaScript functions (which are also loaded via ajax) from the logs_v_month.php from the logs.php view?
I've tried searching for the answer on the web and most answers people give is using the eval() function but many discourages from using that.
Is there a way I can restructure or rearrange my JavaScript so it will be easy for me to access them in this kind of situation? I'm also taking into account Namespacing if this can help the problem.
I just started learning CI and HMVC so I'm fairly new to this.
Thanks!
You need to put the returned scripts into the DOM for the page.
when you get the script data back in the success method of your ajax call, you can create a script element with the code inside it and append it to the DOM, something like:
success:function(data){
var scriptElement = document.createElement('script');
scriptElement.setAttribute('type', 'text/javascript');
scriptElement.textContent = data;
var head = document.getElementsByTagName('head').item(0);
head.appendChild(scriptElement);
}
Now the functions will be accessible to all scripts.
I am using blueimp in my website. blueimp is included at the end of my page(index.php) like:
<?php include "index.html"; ?>
In my php page I am also using a jquery adapter which sends some variables from the same page and two variables from blueimp uploader via:
function save()
{
var variable1= document.getElementById('field1').value;
var variable2= CKEDITOR.instances.field2.getData();
variable2=encodeURIComponent(variable2);
var variable3= document.getElementById('field3').value;
var variables="variable1="+variable1+ "&variable2="+variable2+
"&variable3="+variable3;
jQuery('#mydiv').showLoading();
$.ajax({
type: "POST",
url: "some.php",
data: variables,
error: function(){
alert('Error while loading!');
},
success: function(data){
jQuery('#mydiv').hideLoading();
$('#mydiv').html(data);
}
});
}
Whenever I call this function the variables are not sent to some.php . However if I remove
<?php include "index.html"; ?>
from index.php page, the function save starts to work. I guess something from included blueimp page ,which is index.html, prevents the jquery adapter to function properly.
Is there any clue on this issue?
Thanks in advance.
It sounds like you are having some conflicts between the two libraries. jQuery has this option called noConflict() to allow you to sort of 'pause' jQuery for a bit and allow other libraries to work and then 'resume' normal jQuery functions.
I'm using Javascript and I need to call a PHP file to execute some stuff, but I'd like not to use window.open("filename.php"); because it usually opens a new tab or new window.
Sometime a PHP file need a few seconds to finish its job and I don't like to see an open tab or open window while it's working.
Thanks
AJAX is the solution, and with jQuery it's so simple.
Just add this line on your head section (to include jquery):
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
$.ajax({
type: "GET",
url: "yourPage.php"
}).done(function( data) {
alert( "Request is done" );
});
</script>
You can use ajax or dynamically create iframe.
If you use extjs library - here is example ajax request
Use Ajax! Here's a solution using JavaScript's jQuery library:
$.post('your_file.php', {parameter : some_value}, function(response){
// now you can use `response` came from your_file.php after execution
});
$.post(), $.get() are shorthand methods for $.ajax() of jQuery.
or simply use .load(),
('#test_div').load('your_file.php'); //load data from server into `#test_div`
will do job, if you want to just execute something on server-side.
$('#target').html(????????) or .ajax() ?
Need this to load a php page in the <div> with id target. How do I call that php page?
This is my problem it wasn't my setup it was trying to include the javascript variable obj.info:
function(obj){jQuery.ajax({'url':'/controller/\'+obj.info+\'','cache':false,'success':function(html){jQuery('#target').html(html)}})}
Whenever I try to work the variable obj.info it the function fails.
$('#target').load('url/to/php/script.php');
http://api.jquery.com/load/
$.ajax({
'url/to/php/script.php',
data: { 'varName': yourJsVariable },
success: function(response) {
// your php script returns HTML content
//
$('#element').html(response);
}
});
Check the page on .ajax() for more info: http://api.jquery.com/jQuery.ajax/
How do i make a href link call upon a PHP script without making the page refresh? Just in general? If someone could point me in the right direction it would be great. :)
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.js">
<script>
function trigger() {
$.ajax({
type: 'POST',
url: myfile.php,
data: data,
success: success
dataType: dataType
});
}
</script>
<body>
some link
</body>
For more examples and references go to http://api.jquery.com/category/ajax/ and http://api.jquery.com/jQuery.post/
Embed jQuery and use one of the AJAX commands:
If you want to load data, use $.load()
If you want to send a GET/POST request use $.get() or $.post() or the generic $.ajax() method.
jQuery is your friend! http://www.jquery.com
Specifically, what you're looking for is called AJAX, and it's going to be a fun journey for you. Good luck!
Reference:
http://api.jquery.com/category/ajax/