views:

39

answers:

3

Hey guys

Heres a simple jquery and html script to simulate the facebook wall.. But it isnt working...cud someone help me out?

<script type="text/javascript" src="jquery.js"></script>


<script type="text/javascript">
$(document).ready(function(){
$("form#submit_wall").submit(function() {

var message_box = $('#message_box').attr('value');

$.ajax({
type: "POST",

data:"message_box="+ message_box,
success: function(){
$("ul#wall").prepend("<li style="display: none;">"+message_box+"</li>");
$("ul#wall li:first").fadeIn();
}
});
return false;
});
});
</script>



</head>
<body>

<form id="submit_wall">
<label for="message_box">Type your experience :</label>
<input type="text" id="message_box" />
<button type="submit">Post it!</button>
</form>

<ul id="wall">
</ul>
+1  A: 

You're not using the correct variable name. You've named the variable message_wall but are attempting to use it as message_box. You might also need to supply the url for the AJAX post. It will use the page's url if not specified, but typically you'd pull it from the form's action as it is likely different. I would also use val() to get the value from the input.

 $('form#submit_wall').submit( function() {
     var url = $(this).attr('action');
     var message_box = $('#message_box').val();
     $.ajax({
         url: url,
         type: 'POST',
         data: 'message_box=' + message_box,
         success: function() { ... }
     });
     return false;
 });
tvanfosson
Whoops! Thats was a mistake.. but it still isnt working!
Ram Bhat
@Ram - Your quote marks are off too - see my answer.
Peter Ajtai
A: 

Hello,

message_box is undefined, you initialize message_wall ...

Arnaud F.
First post was edited, before it was : `var message_wall = ... ` ;)
Arnaud F.
+1  A: 

Your quotes are off.... also don't forget to set the URL you are sending the request to!
( $.ajax({ url: ... , type: ... ).


Problem:

$("ul#wall").prepend("<li style="display: none;">"+message_box+"</li>");

You need to pay attention to which quotes will be rendered as part of the string and which quotes denote the string boundaries.


Solution:

$("ul#wall").prepend('<li style="display: none;">' + message_box + '</li>');

Look at the difference in the syntax highlighting of these 2 code snippets.


Try it out with this jsFiddle

Peter Ajtai
Hey.. Thanks for that :) The code is working perfectly in jsfiddle.. but not working here.. Im running WAMP on my localhost...
Ram Bhat
@Ram - Are you sure your jQuery source link is correct ( `jquery.js` not `/jquery.js` or `/jquery/jquery.js` or something else )? So is jQuery loading properly?
Peter Ajtai
yes..ive tested it both with minified nd normal... still nt working :(
Ram Bhat
@Ram - To be clear, if you try a simple `$(function() { alert("jQuery is working!"; });` you do get the alert? ---------------- I assume your HTML is correct, unlike in your posted example (you don't have an open `html` or `head` tag and you don't have a close `body`, or `html` tags?
Peter Ajtai
Hey! I get the alert when so jquery is working fine.. also yes.. the html posted here is incomplete.. What else cud it be! THis is driving me nuts!
Ram Bhat
@Ram - Strange - Do you use Firebug? Try debugging with that... Can you give a link to the page?
Peter Ajtai
Hey Peter! Thanks so much for all your help! Caught the error with chrome developer tools.. There was a non unicode character in the script!
Ram Bhat
@Ram - You're welcome ;) Glad you got it figured out.
Peter Ajtai