Welcome Guest, Not a member yet? Register   Sign In
I have problem with post form with jquery
#11

[eluser]jdfwarrior[/eluser]
Youve got me curious now. I'm gonna have to go set up an installation of 1.5.4 and see if I can get it to work haha.
#12

[eluser]jdfwarrior[/eluser]
I have it working with a local install of 1.5.4. Exact same code.. I think. Check code below. This is a base install of CodeIgniter. Nothing new added except the js file to make it run. I just modified the welcome_message.php view file and the welcome.php controller.

This is the temp.js I made for jQuery:
Code:
$(document).ready(function() {
    $("#btn").click(function() {
        alert("button clicked");
        $.post("welcome/test", { user: $("#user").val(), pass: $("#pass").val() }, function(data){
            alert("returned: " + data)
        });
    });
});

Welcome.php controller modified to add the "test" function that jQuery would be submitting to:
Code:
class Welcome extends Controller {

    function Welcome(){ parent::Controller(); }

    function index() { $this->load->view('welcome_message'); }

    function test() {
        $user = $this->input->post('user');
        $pass = $this->input->post('pass');
        echo "I got {$user} and {$pass}";
    }
}

And once again, this HTML added to the end of the welcome_message.php view
Code:
User<br />
&lt;input type="text" name="user" id="user" /&gt;&lt;br />
Password<br />
&lt;input type="password" name="pass" id="pass" /&gt;&lt;br />
&lt;input type="button" id="btn" name="btn" value="submit" /&gt;

It works. So if something is not working on your end, its something to do with your code because the functionality is there.
#13

[eluser]jdfwarrior[/eluser]
Did you go back and try this code again?
#14

[eluser]darkside9[/eluser]
Thank you jdfwarrior

I have test this example and it works fine in MY CI 1.5.4. with the welcome controller

The problem Which I am facing is passing post data to other controller method, and it is not working even with the simple form post.


&lt;form id="search_form" action="&lt;?php echo site_url()?&gt;search" method="post"&gt;
<div>
&lt;input type="text" align="bottom" class="search_box" name="query" id="query" value="" /&gt;
&lt;input type="image" align="top" src="/images/search-button.png" id="submit" value="Search" /&gt;
</div>
&lt;/form&gt;

[/code]


Code:
class Search extends Controller {
    function Search(){
        parent::Controller();
    }
    
    function index(){
        echo $this->input->post('query'); //but this did not echo any thing
    }
    
    
}


note the search form viewed by another controller main;
#15

[eluser]darkside9[/eluser]
Is there any restriction when passing form data to other controller's method?
#16

[eluser]jdfwarrior[/eluser]
I know that index is the default function, but I've never tried just posting to a controller name and seeing if it would default to index. Have you tried explicitly naming the function you want to post to? In your current case that would be index. If your just using the controller name because your looking for an easy url, maybe you could try setting up a route that would direct to the needed function. I don't know of any restrictions..

Also, if your using jQuery to perform your post, there is no need to wrap your inputs in a form tag. The form tag is used to bundled everything up when you use an html submit button. It finds all inputs within that form tag and submits those. Using jQuery you have specifically tell it what items to send, eliminating the need for the form tag.

I'll check out the thing with posting to the controller and seeing if it falls back to index when I get to work. Will post back soon.
#17

[eluser]darkside9[/eluser]
Thank you jdfwarrior for your effort

I have made the following:

I have create new view called myview.php note that I did not specify the form action:

Code:
&lt;form id="search_form" action="" method="post"&gt; &lt;!-- search_form --&gt;
    <div>
        &lt;input type="text" align="bottom" class="search_box" name="query" id="query" value="" /&gt;
        &lt;input type="image" align="top" src="/images/search-button.png" id="submit" value="Search" /&gt;
    </div>
&lt;/form&gt;

Code:
class Search extends Controller {
    function Search(){
        parent::Controller();
    }
    
    function index(){
        
        //
        
    }
    
    function mysearch(){
        $this->load->view('myview');
        echo $this->input->post('query');
    }
    
    
}


//in this case the form query input is passed to the post and I got the echo with the string which I put in the form.

note that when I added the form action as the following :
Code:
&lt;form id="search_form" action="/search/mysearch" method="post"&gt; &lt;!-- search_form --&gt;
    <div>
        &lt;input type="text" align="bottom" class="search_box" name="query" id="query" value="" /&gt;
        &lt;input type="image" align="top" src="/images/search-button.png" id="submit" value="Search" /&gt;
    </div>
&lt;/form&gt;

No data passed to the mysearch method, and nothing echo
#18

[eluser]jdfwarrior[/eluser]
Soo... you got it working?
#19

[eluser]darkside9[/eluser]
Yes but I don't know why it is not working in the second way?
#20

[eluser]pickupman[/eluser]
If your jQuery is in a js file try referencing the URI of your domain
Code:
$.post('http://www.mydomain.com/mycontroller/myfunction'

I am running jquery from within views ie.
/application/views/mycontroller/jquery

Then I can use
Code:
$.post('&lt;?php echo site_url('mycontroller/myfunction');?&gt;'




Theme © iAndrew 2016 - Forum software by © MyBB