0v3k Shi3ld3r 0v3k Shi3ld3r - 4 months ago 12
Ajax Question

Correct Formatting Code Defining

I would like to add the following code

data: {
name: info,
recptid: '<?php echo $_GET["id"]; ?>',
name: info,
q:'<?php echo $q; ?>',
name: info,
me: '<?php echo $me; ?>'
},


To my existing code below

$(function() {
$(".submit_button").click(function() {
var textcontent = $("#content").val();
var dataString = 'content='+ textcontent;

if (textcontent == '') {
alert("Enter some text..");
$("#content").focus();
} else {
$("#flash").show();
$("#flash").fadeIn(400).html('<span class="load">Loading..</span>');
$.ajax({
type: "POST",
url: "actions/action_comment.php",
// i want to add my code here but i get an error when I do
data: dataString,
cache: true,
success: function(html) {
$("#show").after(html);
document.getElementById('content').value = '';
$("#flash").hide();
$("#content").focus();
}
});
}
return false;
});
});


How can I add my code with the formatting correctly inserted my error is below when I add it code

enter image description here

Thanks to anyone who helps or inputs advice

Answer

First, JS objects should be defined as data = {} not as data: {} assuming that data is already declared (var data = {} otherwise).

Second, the issue that info is being interpreted as a variable here, except you don't seem to have an existing info variable already. So replacing info with hard-coded information such as "123" or defining info before this array should fix your issue, like this:

var info = "123"; // or even info = 123;
data= {
    name: info, 
    recptid: '<?php echo $_GET["id"]; ?>', 
    name: info,
    q:'<?php echo $q; ?>',
    name: info, 
    me: '<?php echo $me; ?>'
};

Edit: As mentioned in the comments, name is defined more than once. Keys/properties have to be unique in JS objects.