0

i am building a php form with ajax and i am submitting the form variables to the same page where the form resides. my validating code looks like

if($valid){
    echo "<div id='retmsg'>Your message has been submitted successfully</div>";
}
else {
    echo "<div id='retmsg'>An error occured!</div>";
}

My ajax code

function form_submit() {
 $('#response').html('Loading...').fadeIn();
$.ajax({
    type: "POST",
url:  "<?php $_SERVER['PHP_SELF']; ?>",
data: "title=" + $('#title').val() + "&content=" + $('#contents').val(),
success: function(html){
var message = $("#retmsg").html(html);
alert(message);
  }

}); 
}

i need to alert the contents of div id "retmsg". But the alert shows me like "[Object Object]". Please help me...

4 Answers 4

1

Check

var message = $("#retmsg").value;
Sign up to request clarification or add additional context in comments.

1 Comment

Hi, thanks for the reply. It shows "undefined". For your information i am getting the all contents in a variable "html" and then i am trying to filter out contents of #retmsg
0

Try

 alert(message.html())

1 Comment

I suggest you to use firebug to test your client side code. I did alert($("#someExistingID").html("a").html()) in some pages and it shows me "a"... probably you are losing some error in your code.
0

take a look at jquery`s html(). if you want to retrieve the html content of a certain element you need to use html() function with no arguments.

so from this:

var message = $("#retmsg").html(html);

to this:

var message = $("#retmsg").html();    

The "[Object Object]" that the

$("#retmsg").html(html);

returns is actually a jquery object. You can find more on jquery chain

4 Comments

but i am submitting the form the same page and the page already has the html elements(whole page with form elements), html() will return all contents again. Hope you understand
what i want to say is that in your code, you alert the jquery object, to alert the the contents of div id "retmsg" as you said you need to do : alert($("#retmsg").html());
i think both ways are same because i get the same answer for also alert($("#retmsg").html())
alert($("#retmsg")[0].innerHTML);
0

Thanks for all gentle mens who shared my issue. As i mentioned in my description, as i submitted the form to the same page, the page loads again with all components i used such as jQuery library and js files i.e the whole document. That's the problem. I simply used if(post){ } else { form validation } for the whole html content and got it solved.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.