Welcome Guest, Not a member yet? Register   Sign In
Cant reach controller from script
#1

[eluser]fiddle-sticks[/eluser]
Hi so this is my first post ever - please be kind.
And this is a little thing that I just cant get around - Im sure there's some simple explanation as to why..

So basically Im trying to send data from a text area to a function in one of my controllers asynchronously using jquery/json but it doesnt seem to reach the controller or do anything.

And Ive narrowed it down to the following bits of code that arent working (dont want to bog you down with masses of code here):
the button that is inside the form but which works fine for doing an alert etc.. (Note: ignore fullstop between on and click)
Code:
<button type='button' on.click='saveimage()'>Save Image</button>
the jquery code that works fine for getting the data and putting it into the img variable...
Code:
[removed]

    function saveimage() {
    
    var img    = "some random text";
    $.post("/template/saveimagetosession", { img:img }, NULL ,'json');
        
    }
    
[removed]
the Template controller function that works fine writing to data.txt if called on its own..
Code:
function saveimagetosession(){

        $this->load->helper('file');
        $this->session->set_userdata('imageurl', $this->input->post("img"));
        
        $img = $this->input->post("img");
            
        write('data.txt', $img);
    }
but somewhere between posting and the controller it falls apart..

Thanks in advance
any help is much appreciated
#2

[eluser]InsiteFX[/eluser]
Try this, Not test but I think you need to pass the base_url to you function.
Code:
$.post("&lt;?php echo base_url();?&gt;template/saveimagetosession", { img:img }, NULL ,'json');

InsiteFX
#3

[eluser]fiddle-sticks[/eluser]
okay I tried this
Code:
$.post("&lt;?php echo base_url();?&gt;" + "template/saveimagetosession", { img:img }, NULL ,'json');
        
    alert('&lt;?php echo base_url();?&gt;' + "template/saveimagetosession");
Which doesnt do anything.
But if I do just this then it prints out the right url fine..
Code:
alert("&lt;?php echo base_url();?&gt;" + "template/saveimagetosession");

So Im guessing:
Code:
$.post("&lt;?php echo base_url();?&gt;" + "template/saveimagetosession", { img:img }, NULL ,'json');
Is the guilty party in the whole thing
#4

[eluser]bubbafoley[/eluser]
are you hiding index.php from the urls with mod_rewrite? If not base_url() won't work. you also don't need the callback function or the datatype in the jquery post call since the controller is doing all of the work.

try this:
Code:
$.post("&lt;?php echo site_url('template/saveimagetosession') ?&gt;", { img:img });

If that doesn't work you need to debug your [removed] http://fixingthesejquery.com/#slide3
#5

[eluser]Rob @ iFetch[/eluser]
[quote author="fiddle-sticks" date="1299809008"]
Code:
<button type='button' on.click='saveimage()'>Save Image</button>
[/quote]
I'm sure it's probably just a copy/paste error, but is that 'on.click' in your code as well? Should it not be 'onclick' (without the period)?
#6

[eluser]fiddle-sticks[/eluser]
Thanks for the helps guys. But I couldnt get it in the end Im afraid and solved my problem by using plain php and multiple submit buttons. It didnt need to be done asynchronously in the first place.




Theme © iAndrew 2016 - Forum software by © MyBB