Welcome Guest, Not a member yet? Register   Sign In
url in an ajax application
#1

Hello friends,

I am designing an application in which I want to pull data from a php file using javascript technique, namely ajax. In CI am not sure how to access my controller this way.

Consider this as my view:

<html>
<head>
<title>An Ajax example</title>
<script language = "javascript">
var XMLHttpRequestObject = false;
if (window.XMLHttpRequest) {
XMLHttpRequestObject = new XMLHttpRequest();
} else if (window.ActiveXObject) {
XMLHttpRequestObject = new
ActiveXObject("Microsoft.XMLHTTP");
}
function getData(dataSource, divID)
{
if(XMLHttpRequestObject) {
var obj = document.getElementById(divID);
XMLHttpRequestObject.open("GET", dataSource);
XMLHttpRequestObject.onreadystatechange = function()
{
if (XMLHttpRequestObject.readyState == 4 &&
XMLHttpRequestObject.status == 200) {
obj.innerHTML = XMLHttpRequestObject.responseText;
}
}
XMLHttpRequestObject.send(null);
}
}
</script>
</head>
<body>
<H1>An Ajax example</H1>
<form>
<input type = "button" value = "Fetch the message"
onclick = "getData('data.php', 'targetDiv')">
</form>
<div id="targetDiv">
<p>The fetched message will appear here.</p>
</div>
</body>
</html>

Thanks for you suggestions
Reply
#2

(This post was last modified: 08-30-2016, 12:16 PM by InsiteFX.)

PHP Code:
HTML
<head
    
<script>
        var 
baseUrl "<?php echo base_url(); ?>";
    </
script>
</
head 

PHP Code:
JS
    
// Save
    
$("#save").click(function () {

        $(
"#load").show();

        var 
dataString = {
            
data: $("#nestable-output").val(),
        };

        $.
ajax({
            
type    "POST",
            
url     baseUrl "menu/saveMnu",
            
data    dataString,
            
cache   false,
            
success : function (data) {
                $(
"#load").hide();
                
alert('Data has been saved');
            }, 
error: function (xhrstatuserror) {
                
alert(error);
            },
        });
    }); 
baseUrl + "controller/method";
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

(This post was last modified: 08-30-2016, 12:24 PM by PaulD.)

That example looks like it is from ajax for dummies. http://documents.mx/technology/ajax-for-...-only.html

A raw ajax call is actually quite complicated, which is why so many frameworks and libraries have popped up to make the whole job easier.

For instance in jquery, you would do something like this:
PHP Code:
$.get('/mycontroller/mymethod', function(theResponse){
 
   $('#results).html(theResponse)

This will get information from mysite/mycontroller/mymethod just like a normal url request. It will get the response and put it into whatever has the id of 'results'.

And your controller would just do this:
PHP Code:
<?php defined('BASEPATH') OR exit('No direct script access allowed');

class 
Mycontroller extends CI_Controller {

 public function 
mymethod()
 {
 echo 
'Hello World';
 }


However, in the example of jquery, there is much more you can do with the library: https://api.jquery.com/jQuery.get/

Now you could still load a view as a string (use the third option in the load view and set it to true) for keeping html in views, and you can send data in the url segments or use a post instead of a get. Then you can post an array to the controller, validate it in the usual way, do some database calls, use a view to create a html snippet and return that. The level of complexity you add is up to you.

As for your example you posted, you need to give it the url you are sending the request to. You controller just acts in the normal way apart from echoing the results rather than sending them as full page output.

I hope that helps,

Paul.
Reply
#4

A javascript file can not execute php code only a file with the php extension can execute php code.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#5

?

No of course it can't. Not sure where you thought I was saying that but if what I said is not clear then I apologize for any confusion.
Reply
#6

(This post was last modified: 08-31-2016, 04:19 PM by InsiteFX.)

No not at you PaulD, Just stating it so anyone else will not try to do it.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply




Theme © iAndrew 2016 - Forum software by © MyBB