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

[eluser]darkside9[/eluser]
Hi Guys,
I am trying to post form elements to controller but the nothing posted to the controller.
Here is my code

$(document).ready(function() {
$('#btn').click(function(){

var comment_str = $('#comment_text').attr('value');
var key = $('#content_key').attr('value');

$.post('/main/addComment', {comment:comment_str, content_key:key }, function() {

$('#comment_text').val("");
$(".msg_body").hide();

});

});
});

and I checked the comment_str and key they have values whiched entered in the form.

When I am trying to trace it using fire bug
I got an error that mean the comment:comment_str, content_key:key not passed to the controller /main/addComment



any advice
#2

[eluser]jdfwarrior[/eluser]
I've done this before and I can't remember off the top of my head, but I'm wanting to say that you should post to "main/addComment", leaving off the / in the front. I could be wrong though... I'll set something up and test it if you'd like
#3

[eluser]darkside9[/eluser]
I have tried what you suggest but it is not working,
When I put "/main/addComment" I did not put the site url because i have VHost and it is needed to add / but if I add the site url before it it is sure i must remove the /.
#4

[eluser]darkside9[/eluser]
@jdfwarrior
if you have any example to how I can post form data to controller, you will help me.
Thanks for the reply.
#5

[eluser]jdfwarrior[/eluser]
That didn't matter anyway... This is what I just used...

jQuery
Code:
$(document).ready(function() {

    $("#submitform").click(function() {

         $.post("home/testFunction", { user: $("#username").val(), mail: $("#usermail").val() }, function(data) {
            alert("posted");
            alert("returned:" + data)
        });

    });

});

inside the template html
Code:
User<br />
            &lt;input type="text" name="username" id="username" /&gt;&lt;br />

            Email<br />
            &lt;input type="text" name="usermail" id="usermail" /&gt;&lt;br />

            &lt;input type="button" id="submitform" name="submitform" value="submit" /&gt;

function being submitted to..
Code:
function testFunction() {

        $username = $this->input->post('user');
        $email = $this->input->post('mail');

        echo "I got {$username} and {$email}";

    }


This code worked for me.
Let me know if this works...
#6

[eluser]darkside9[/eluser]
@jdfwarrior
It is not working with me.
Question is this code need specific version of CI, My CI Version is 1.5.4, I am adding new features to old site which built in CI 1.5.4

Note. When I remove the / from '/main/addComment' the url which requested is http://mydomain/en/main/addComment/main/addComment

I guess when removing / this part of the url /main/addComment url will duplicated.

Thanks jdfwarrior for fast reply
#7

[eluser]jdfwarrior[/eluser]
I don't know much about version 1.5.4. I would think posting would still work though as that's something pretty important.

In the function being posted to, echo something simple (doesnt have to be the passed values). Just echo your name or something and see if you can get that to return to the jQuery.

Basically.. keep the jQuery post command the way it is except make the callback do nothing but alert the response.

Code:
$(document).ready(function() {
    $('#btn').click(function(){

  var comment_str = $('#comment_text').attr('value');
  var key   = $('#content_key').attr('value');
    
    $.post('/main/addComment', {comment:comment_str, content_key:key },
       function(data) {
          alert("response: " + data);
       });
        
  });
});

That will let you see whether or not it is actually posting to the function and whether or not the function is returning anything back to jQuery. Then branch out from there. See if it will show you the info your passing to it, and so on.
#8

[eluser]darkside9[/eluser]
mmmmmmm

In the main/addComment

I took the values from the $_POST as the following:

$key = $_POST["content_key"];
$comment = $_POST["comment"];

instead of using $this->input->post(...)

I got the following:

...

<h4>A PHP Error was encountered</h4>

<p>Severity: Notice</p>
<p>Message: Undefined index: comment</p>
<p>Filename: controllers/main.php</p>
<p>Line Number: ...</p>



<h4>A PHP Error was encountered</h4>

<p>Severity: Notice</p>
<p>Message: Undefined index: content_key</p>
<p>Filename: controllers/main.php</p>
<p>Line Number: ...</p>

...
#9

[eluser]asylmottaket[/eluser]
http://malsup.com/jquery/form/

Makes life easy!
#10

[eluser]darkside9[/eluser]
@jdfwarrior

I made the same functionality in a fresh copy of CI 1.7.1
And it is working fine.

I guess the problem when using jquery post with CI 1.5.4

I will try to find some way to post form in jquery or ajax with CI 1.5.4

Thanks jdfwarrior for your efforts




Theme © iAndrew 2016 - Forum software by © MyBB