views:

63

answers:

3

Hello,

I do struggle with it as hell. Can someone tell me what is wrong with that code.

In a word: CI + jquery form submit. I try to add inline row.

  • java part

        var pair = $('#pair').attr('value');
        var entry = $('#entry').attr('value');
        var exit = $('#exit').attr('value');
        var buysell = $('#buysell').attr('value');
        var pl = $('#pl').attr('value');
    var dataString = 'pair='+ pair +'&entry='+ entry +'&exit='+ exit +'&buysell='+ buysell +'&pl='+ pl;
      $("form#submit").submit(function() {
            $.ajax({ 
                type: "POST",
                url: "<?php echo base_url();?>journal/add",
                data: dataString,
                success: function(){
                        $("#message").text("Added").fadeOut(4000, function() {
                            $(this).css('display','block').text("");
                        });
                }
            });
    });
    
  • controller part (works if not using java, and yet it's too simple not to work)

        $data['pair'] = htmlspecialchars(trim($_POST['pair']));
        $data['entry'] = htmlspecialchars(trim($_POST['entry']));
        $data['exit'] = htmlspecialchars(trim($_POST['exit']));
        $data['buysell'] = stripslashes(trim($_POST['buysell']));
        // Insert_data
        $this->journal_model->add_trade($data);
    

If someone will be able to help, please do... :(

Cheers,

/Jacek

+1  A: 

the one think that jumps out at me is the php echo statement. is this in a php file? if not, that's your problem.

GSto
The OP is using CodeIginter, and I assume this is in a view file loaded via a controller. If not, this is your problem.
Rocket
This is a build in Code Igniter statement which gets the url configured.
Jacek Dominiak
I know how to use CodeIgniter, I know what `base_url();` does. Is this code in a view file, or in a .js file?
Rocket
It's in a view file.
Jacek Dominiak
Jacek Dominiak
I found out that once I fill the form ... it does not submits ... empty one submits the data hardcoded ... :)
Jacek Dominiak
I figured out the problem. The dataString may not be correct if any fields are empty. See my answer below.
Rocket
A: 

Try using $('form').serialize() instead of manually creating data string. If any of the fields are empty, your manually created dataString may not be correct. For example, if the buysell field was empty, the dataString would be pair=data&entry=data2&exit=data2&buysell=&pl=data3, as you can see buysell is empty, and this dataString is not valid.

$("form#submit").submit(function() {
    $.ajax({ 
        type: "POST",
        url: "<?php echo base_url();?>journal/add",
        data: $('form').serialize(),
        success: function(){
                $("#message").text("Added").fadeOut(4000, function() {
                    $(this).css('display','block').text("");
                });
        }
    });
});

This can also be written using the $.post() shorthand.

$.post('<?php echo base_url();?>journal/add', $('form').serialize(), function(){
    $("#message").text("Added").fadeOut(4000, function() {
        $(this).css('display','block').text("");
    });
});
Rocket
A: 

After some woking out... I have it working in IE (wow :O ) but not in Safair/FF/Chrome :S

Function looks like that now:

    $("form#add_trade_form").submit(function() {
        dataString = $("form#add_trade_form").serialize();
        $.ajax({
            type: "POST",
            url: "<?php echo base_url();?>journal/add",
            data: dataString,
            cache: false,
            lsuccess: function() {
                $("#message").html("Saved... ").fadeOut(4000, function() { 
                    $(this).css('display', 'block').text("");
                });
            }

        });

    });

Any ideas?

PS. Thanks Rocky ... it moved me forward...

Jacek Dominiak
Jacek - Please do not post additional questions as an answer. It is better to update your original question (using the "Edit" button) so that other users coming to the site don't confuse answers and questions.
JasCav
Thanks for the tip Jason !!
Jacek Dominiak