mrpatg
mrpatg

Reputation: 10117

Trouble submitting many variables in jquery ajax to php

This JS is giving me some problems. Specifically FF3.5 is saying there is Error: missing ; before statement and its pointing at the 'comsn=' + comsn section. I know enough js to get myself into trouble, but specifics, im not there yet. Any ideas?

<script type="text/javascript" >
$(function () {
    $(".submit").click(function () {
        var comsn = $("#comsn").val();
        var comrn = $("#comrn").val();
        var compic = $("#compic").val();
        var comment = $("#comment").val();
        var eventid = $("#eventid").val();
        var comuserid = $("#comuserid").val();
        var owner = $("#ownerid").val();
        var dataString = 'comsn=' + comsn '&comrn=' + comrn '&compic=' + compic '&comment=' + comment '&eventid=' + eventid '&comuserid=' + comuserid '&owner=' + owner;
            if (comment == '') {
                alert('Must Type Comment to Post Comment');
            }else{
                $("#flash").show();
                $("#flash").fadeIn(400).html('<img src="assets/uploading.gif" />Loading Comment...');
                $.ajax({
                    type: "POST",
                    url: "comments_post.php",
                    data: dataString,
                    cache: false,
                    success: function (html) {
                        $("ol#update").prepend(html);
                        $("ol#update li:last").fadeIn("slow");
                        $("#flash").hide();
                    }
                });
            }return false;
    });
});
</script>

Upvotes: 1

Views: 616

Answers (3)

nowk
nowk

Reputation: 33161

Your missing some +s

+ comrn '&compic='...

should be

+ comrn + '&compic='...

Upvotes: 1

Christian C. Salvad&#243;
Christian C. Salvad&#243;

Reputation: 827218

You have missing + signs in your concatenation:

var dataString = 'comsn=' + comsn + '&comrn=' + comrn +
                 '&compic=' + compic + '&comment=' + comment +
                 '&eventid=' + eventid + '&comuserid=' + comuserid +
                 '&owner=' + owner;

However if you want to get all your form element values in a string of data, you could use the Ajax/serialize method:

var dataString =  $("#formId").serialize();

Upvotes: 1

RageZ
RageZ

Reputation: 27313

var dataString = 'comsn=' + comsn + '&comrn=' + comrn + '&compic=' + compic + '&comment=' + comment + '&eventid=' + eventid '&comuserid=' + comuserid + '&owner=' + owner;

You've got a lot of missing +.

Also jquery have a method called serialize which do the same think you are doing by hand probably worth to give a try.

Upvotes: 1

Related Questions