Tell me more ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

One of our web applications is a page that takes in a serial number and various information is returned and displayed to the user. The serial is passed via AJAX, and based on the response, one of the following can happen -

  • An error message is shown
  • A new form replaces the previous form

Now, the way I am handling this is to use jQuery to destroy (using $.remove()) the table that displayed the initial serial form, then I'm appending another html table that contains another form. Right now I am including that additional form as part of the html source, and just setting it to display:none, then using jQuery to show it when appropriate. However, I don't like this approach because if someone views source on the page, they can see that table html code that is not being displayed.

My next thought would be to use AJAX to read in another HTML file, and append it that way. However, I am trying to keep down the number of files this project uses, and since most pages in our project will use AJAX, I could see a case where there are multiple files containing HTML snippets - and that feels sloppy to me.

What is the best way to handle a case where multiple html elements are being shown and removed with jQuery?

share|improve this question
1  
Why are you worried that the user might see all the tables? And why is it 'no way you're removing it' - is the data not saved yet until the last form? –  Spork Aug 16 at 21:31
 
I don't understand your reference to 'no way you're removing it'. But the idea is that a person only sees the subsequent forms after they have submitted a serial number that fits that requirement - i.e. serial is submitted first, if it fits the proper criteria another form needs to be shown to the user –  J. Robertson Aug 16 at 21:47

2 Answers

I've done something like this by sending the new HTML with the response in a JSON object. Something like this question.

echo '{"html": "<p>I\'m the markup</p>"}';

Then replacing the HTML like this:

$.ajax({
    url: "foo.php",
    type: "post",
    data: values,
    success: function(data){
        $("#myDiv").html(data.html);
    }
});
share|improve this answer

Yes this is right way , you can user to avoid IE problem

echo '<p>this is your html</p>'

Js code

$.ajax({
url: "ajax.php",
type: "post",
data: values,
success: function(data){
    $("#myDiv").html($.trim(data));
}})
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.