views:

58

answers:

1

I'm struggling with this and just can't seem to make it work.
Need to pass the current users date (cdate) variable to my controller and in spite my alert windows shows the correct value, that value never reaches the controller.
Here's the javascript code:

$(document).ready(function() {  
    $('#submit').click(function() {  
        var mydate=new Date()  
        var year=mydate.getYear()  
        if (year < 1000)  
            year+=1900  
        var day=mydate.getDay()  
        var month=mydate.getMonth()+1  
        var daym=mydate.getDate()  
        if (daym<10)  
            daym="0"+daym  
        if (month<10)  
            month="0"+month  
        var hours=mydate.getHours()  
        var minutes=mydate.getMinutes()  
        var seconds=mydate.getSeconds()  
        var cdate=+month+"/"+daym+"/"+year  
        $.post('user/available', {curdate: cdate});  
        alert(cdate);         
    });  
});    

Controller:

$curdate=$this->input->post('curdate');  

View:
echo form_open('user/available');
echo form_input('dateav','',$dateav);
input type="image" src="echo base_url();images/send.png" id="submit" alt="Submit button"
echo form_close();
Can anyone tell me what i'm doing wrong?
Thanks

A: 

I suggest to dump the whole request to stdout or something so you can see what URL is used and all the other parameters. Maybe there is a typo in the config.

[EDIT] The function should return false, otherwise Bad Things(TM) will happen (like the form will be posted twice and such).

If nothing happens but the alert showing up, then the post() call fails. Read the documentation and especially the part about error handling with .ajaxError()

Aaron Digulla
Hmmm, how can i do it? Sorry but i'm still a newbie :(. Thanks.
JEagle
Just as DrColossos says: Use Firebug. If you see the alert, then Firebug must see the request. Note that Firebug must be running (open it and reload the page) and enabled for this page (check the prefs)!
Aaron Digulla
Ok. What's happening right now is that i stopped seeing the alert window in chrome and firefox but i'm seeing in IE. Has anyone experience this? Using firebug i can's see the function being fired. How can i check it in IE?
JEagle
Check the JavaScript console for errors on Firefox. Rule: Make it work in FF (or Chrome) because they have the best developer support. Only when everything else works perfectly fine, try with IE.
Aaron Digulla
@ Aaron. Ok. i'm doing that right now. I don't know what's going on but when i insert return false; right after the alert() the function gets an OK in firebug but it doesn't pass it to the controller it just sits there. If i remove return false; then the controller works but i get an error in firebug. Can anyone help me on this one. I just don't know what more to do and this is what's missing to finish the project :(
JEagle
The function should return false, otherwise Bad Things(TM) will happen. See my edits for further instructions.
Aaron Digulla
Ok. Will read the docs. Can you see my updated code? Is the view code ok?
JEagle
Since i get an OK POST in firebug but nothing happens in the controller will .ajaxError() show something?
JEagle
Did a little progress. Analyzing firebug POST HTML e found that my input field is not being filled and validation says the field is required=empty....So can someone please see my view code and tell me if my id="submit" is properly set?
JEagle
Which input field isn't filled? Does that input field exist in the form? If not, what happens if you add it?
Aaron Digulla
Hi Aaron, here's what i'm trying to do. The form has one field where the user selects a date(field is datepicker, a calendar appears and he selects a date). After that he submits the form. Together with that form i want to send the value of cdate(current users date) which is in the javascript code i posted. Then in the controller i make the calculations based on the dates provided(user can be in Australia and the dates be different).
JEagle
Add an invisible field for `cdate`. It's possible that you can't post values for fields which don't exist in the form.
Aaron Digulla
Ok added echo form_hidden('curdate',$cdate); but got an undefined error on the variable $cdate. How can i fill that variable with cdate value?
JEagle
Ok I know i have to do a page reload in order to get that value. So now i'm trying to pass the value to the controller and then insert it back to the view.
JEagle
What happens now is that when i click the datepicker field, the javascript triggers and i get and OK POST and i get another page on top of the page i had. Instead of just adding the from_hidden field, it adds the whole page again.Anyone had this before?
JEagle
In that case, you must add a success handler to `post()`.
Aaron Digulla
Ok. Can you send to an example on how to do it? Also i'm using .load because if i use .post nothing happens.
JEagle
Unfortunately not since I'm not using Codeigniter. I suggest to follow an example in Codeigniter that is similar to your problem, make it work 100% as described in the docs and then start to modify it for your problem.
Aaron Digulla
Thanks Aaron, it's working now. Turns out i had to give full path to my ajax function on my controller. .load("user/ajax", data) didn't work so i had to create a var with the base_url() and then put like : .load(root+"user/ajax", data) .
JEagle