Welcome Guest, Not a member yet? Register   Sign In
jquery with forms
#1

[eluser]A.M.F[/eluser]
hello

i tried to work with jquery and forms combining with CI but i couldn't get it work... i am still a newbie in jquery so i tried to searc for good toturials, but all i found was scripts...

what i want to do is to send a form through jquery and work with the data with CI.

thank u!
#2

[eluser]Phil Sturgeon[/eluser]
There are two ways to submit forms with jQuery, GET and POST. Dont bother with GET as it doesnt get along with CI very well, use POST. Shouldnt matter if you use a normal page request or use AJAX, POST will appear in CI the same.

If you REALLY need to use a URL to send your values, you can so it like this:

Code:
<scr1pt>
   $(function(){
        $('input#submit').click(function(){
            w1ndow.locat1on = '&lt;?=$goto_link;?&gt;' + $('select#cup').val();
        });
    });
</scr1pt>

<h3>Pick Cup</h3>

&lt;?=form_dropdown('cup', $cups, $default_cup, 'id="cup"');?&gt; &lt;input type="button" id="submit" value="Next"&gt;

Sorry if the variable names seem a bit random, this is a straight copy from a football site I was working on.
#3

[eluser]A.M.F[/eluser]
[quote author="thepyromaniac" date="1203105633"]There are two ways to submit forms with jQuery, GET and POST. Dont bother with GET as it doesnt get along with CI very well, use POST. Shouldnt matter if you use a normal page request or use AJAX, POST will appear in CI the same.

If you REALLY need to use a URL to send your values, you can so it like this:

Code:
<scr1pt>
   $(function(){
        $('input#submit').click(function(){
            w1ndow.locat1on = '&lt;?=$goto_link;?&gt;' + $('select#cup').val();
        });
    });
</scr1pt>

<h3>Pick Cup</h3>

&lt;?=form_dropdown('cup', $cups, $default_cup, 'id="cup"');?&gt; &lt;input type="button" id="submit" value="Next"&gt;

Sorry if the variable names seem a bit random, this is a straight copy from a football site I was working on.[/quote]

i see, and what if i want to use it through post? how can i do it with the jquery side?
#4

[eluser]Phil Sturgeon[/eluser]
Put the form inputs inside a <form tage - or use form_open() - then instead of setting [removed] you would send it via POST AJAX.

http://docs.jquery.com/Ajax/jQuery.post#...llbacktype
#5

[eluser]A.M.F[/eluser]
i have tried something like that in the JS:

Code:
$(document).ready(function() {
              
        $("#comment input[@type='submit']").click(function(e){
                                
            $.post("&lt;?=site_url_rel("show/comment/".$game_id)?&gt;", {comment: $(this).html()}, function(xml) {

                $("#comment").html(
                "התגובה התווספה בהצלחה"
                );
                        
                alert("Hello world!");
            });
                            
            return false;
        });
    });


and this if the show/comment function that i have:

Code:
$comment = $_REQUEST["content"];

$data = array('cmnt_itemid'     => $item_id,
          'cmnt_item'     => 'game',
          'cmnt_content'     => $comment,
          'cmnt_date'     => 'today'
        );
$this->db->insert('comments', $data);

i tried to work with this for hours but i couldn't get it work
#6

[eluser]Phil Sturgeon[/eluser]
You are sending it the POST key of "comment" in your Javascript, but in your PHP you are looking for "content". Also, try using $this->input->post('comment') instead of $_REQUEST as I think CI MIGHT kill that variable just like GET as its even more insecure.

You also are using $(this).html() which is incorrect, as you would be sending the inner HTML of the submit button they just clicked, which is clearly wrong. You want to send $("input#comment_box").text() or .html() instead.

There is no reason for your "e" parameter in the function call, and really you should do some checking in both JS and PHP to see if it worked. If the data is invalid then return a status header ( I use the following code to see if it worked or not, 404 is the wrong status but I dont know a better one).

Code:
function delete_payment($id = 0)
    {
        $this->load->helper('ajax');
        if(isAjax()):
            if($this->invoices_model->deleteInvoicePayment($id)):
                $this->output->set_header('HTTP/1.1 200 OK'); // Worked fine
                
            else:
                $this->output->set_header('HTTP/1.1 404');
                $this->output->set_output($this->lang->line('error_delete_payment'));
            endif;

        else:
            if($this->invoices_model->deleteInvoicePayment($id))
                            // Redirect to good or bad page message, whatever

        endif;
    }

Then you can use jQuery's second callback function, first is success, second is failure.
#7

[eluser]A.M.F[/eluser]
thanks for ur reply.
i modified my code and now the data is beeing transfered to the DB exept for the content of the field.

this is part of the js code:
Code:
$.post("&lt;?=site_url_rel("show/game/".$game_id)?&gt;", {content: $("input#content").html()}, function(xml) {

and this is part of my view:

Code:
&lt;?=form_open('show/comment/' . $id, $data);?&gt;
<br />&lt;textarea name="content" id="content" maxlength="300" class="big_txt"&gt;&lt;?=$this->obj->validation->content?&gt;&lt;/textarea&gt;


i can't put my finger on the problam, what do u think?
ps
i am looking for good example code of jQuery so i'll learn the syntax,

cheers!
#8

[eluser]Phil Sturgeon[/eluser]
What errors do you get? Post the controller code as this doesnt give us much to go on.










You cant pass $data inside the view, unless in your controller you defined $data['data'] = $data; or gave it some other value which is... silly.
#9

[eluser]A.M.F[/eluser]
[quote author="thepyromaniac" date="1203285037"]What errors do you get? Post the controller code as this doesnt give us much to go on.



You cant pass $data inside the view, unless in your controller you defined $data['data'] = $data; or gave it some other value which is... silly.[/quote]


the $data in my view is just an array named data with the form attributes.

and as for the controller code:

Code:
function comment($item_id)
{
    $comment = $this->input->post('content', TRUE);
    $data = array('cmnt_itemid'     => $item_id,
                  'cmnt_item'     => 'game',
                  'cmnt_content' => $comment,
                  'cmnt_date'     => 'toda1y'
    );
    $this->db->insert('comments', $data);
}

and i don't get any error, just in my DB the data that i get in the 'content' field is 'null'
#10

[eluser]Phil Sturgeon[/eluser]
Just play the debug game, echo it in the controller, print_r($_POST), keep going until you find where its showing as NULL then you have your answer.

The code you posted cant be all your controller, as you have the code &lt;?=$this->obj->validation->content?&gt; in your view, which would suggest you are validating this somewhere and I see no validation here.




Theme © iAndrew 2016 - Forum software by © MyBB