Welcome Guest, Not a member yet? Register   Sign In
jquery is not redirecting to controller?
#1

[eluser]solid9[/eluser]
Hi guys

I have these jQuery codes below,
Code:
$(document).ready(function(){
  $("#submit").click(
    function(){
        var swapid=$("#swapid").val();
        var name=$("#name").val();
        var email=$("#email").val();
        var comment=$("#comment").val();

        $.ajax({
        type: "POST",
        url: "http://barterswapping.com/main/submit/",
        dataType: "json",
        data: "swapid="+swapid+"&name;="+name+"&email;="+email+"&comment;="+comment,
        cache:false,
        success:
          function(data){
            $("#form_message").html(data.message).css({'background-color' : data.bg_color}).fadeIn('slow');
          }
        
        });
      return false;
    });
});

and in my <b>controller</b>, I have this method
Code:
function submit()
{
  $this->data['header_message'] = 'SMILE :)';
}

Just wondering why is it every time I press the submit button
in the view the page don't redirect to <b>main</b> controller.

What might be the reason for this?
#2

[eluser]CroNiX[/eluser]
Is your "#submit" element an actual submit input type form element? If so, you need to prevent the default behavior of the submit button.
Code:
$(document).ready(function(){
  $("#submit").click(
    function(e){ //pass the event
      e.preventDefault();  //prevent the submit buttons default behavior of sending the form
      //...
#3

[eluser]solid9[/eluser]
@CroNiX

I already modified the codes, see below,
Code:
$(document).ready(function(){
  $("#submit").click(
function(e){ //pass the event
      e.preventDefault();    
        var swapid=$("#swapid").val();
        var name=$("#name").val();
        var email=$("#email").val();
        var comment=$("#comment").val();
      
        $.ajax({
        type: "POST",
        url: "http://barterswapping.com/main/submit/",
        dataType: "json",
        data: "swapid="+swapid+"&name;="+name+"&email;="+email+"&comment;="+comment,
        cache:false,
        success:
          function(data){
            $("#form_message").html(data.message).css({'background-color' : data.bg_color}).fadeIn('slow');
          }
        
        });
      return false;
    });
});


Is my changes correct CroNix?

By the way thanks for your reply.
#4

[eluser]InsiteFX[/eluser]
Try this:
Code:
url: &lt;?php echo base_url();?&gt;+"main/submit",

Also in your submit method you should check for an ajax call:
Code:
if ($this->input->is_ajax_request())
{
    // do ajax stuff
}
else
{
    // else do normal stuff
}
#5

[eluser]solid9[/eluser]
@insideFX

I already did your suggestion.

But the page keep refreshing every time I press the submit button.
and nothing happened, below is the view

view
[code]
<div id="addCommentContainer">
&lt;form name="addCommentForm" id ="addCommentForm" method="post"&gt;
<div>
&lt;input type="hidden" name="swapid" value="&lt;?php echo $swapid ?&gt;"&gt;

<label for="name">Name &nbsp;&nbsp;&nbsp;&nbsp;</label>
&lt;input type="text" name="name" id="name" /&gt;&lt;br>

<label for="email">Email &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</label>
&lt;input type="text" name="email" id="email" /&gt;&lt;br>

<label for="body">Comment </label>
&lt;textarea name="comment" id="comment" cols="20" rows="5"&gt;&lt;/textarea><br>

&lt;input type="submit" name="offer_submit" id="offer_submit" value="Submit" /&gt;
</div>
&lt;/form&gt;
<br><br>
</div>
[code]

By the way I change the name and id of the button.
I also change the jQuery codes to match the name/id of the button.


#6

[eluser]InsiteFX[/eluser]
I may be wrong here, but I think you need the action statement in your form.
Also you have a space between the id and =
Code:
&lt;form name="addCommentForm" id="addCommentForm" action="where you want it to go!" method="post"&gt;

Here is an article on it but it's for ExpressionEngine.
jQuery and ExpressionEngine Form Processing
#7

[eluser]CroNiX[/eluser]
Probably because your submit button has an id of "offer_submit". Check your jquery, you are targeting #submit, which doesn't exist.
#8

[eluser]ojcarga[/eluser]
Some things to test with:

- Check the JQuery library inclusion
Code:
&lt;script...
is working as ot should.
- Make sure the selector is which it should be
Code:
$("#submit")
- Use
Code:
.submit(
instead of
Code:
.click(
- Why are you adding ";" to
Code:
"&name;="+name+"&email;
, I think you dont need it.
- Change the url: to the way @InsiteFX said.

If not, post the actual code, controller and view, both.
#9

[eluser]InsiteFX[/eluser]
Here is another way to get the base_url into jQuery:
Code:
// Replace the $ in script tags with s
<$cript type="text/javascript" charset="utf-8">
    //&lt;![CDATA[
        var base_url = "&lt;?php echo base_url(); ?&gt;";
        var site_url = "&lt;?php echo site_url(); ?&gt;";
    // ]]>
</$cript>

Now you can use either base_url+"" or site_url+""




Theme © iAndrew 2016 - Forum software by © MyBB