treehouse : what would you like to learn today?
Web Design Web Development iOS Development

Input dynamically added, wont delete on submit unless refresh.

  • I'm working on a mini CMS for a racing team and here is my issue.

    When they log on, PHP will generate a <form> with a <fieldset> chunk for each post they have done.

      <fieldset class="old_posts">';
          <input type="checkbox" name="select_post[]" class="select_post" value="' . $id . '">';
          <div class="select_post_separator"><span class="first"></span><span></span><span class="last"></span></div>';
          <input type="text" name="old_post_title" class="old_post_title" value="' . $title . '">';
          <span class="old_post_edit" title="' . $id . '"></span>';
          <p class="old_post_date">' . $postMonth . '. ' . $postDay . '</p>';
      </fieldset>';
    

    Then, if they want to add a post, I upload the data to a MySQL database with JQuery AJAX in the background, and on success, I get the ID of that post, and insert it with .after in my form. The problem is when I do this, select the post and delete it, it doesn't see that new post as part of the form and won't delete it unless I refresh the page.

    Any idea how to fix this? Here is the code performed when creating a post and when deleting it.

    submit_post function:

    $.ajax({
        type: 'POST',
        url: 'submit_post.php',
        data: { postTitle : titleValue, postMessage : messageValue }
    }).success( function () {
            var date = new Date(),
            day = date.getDate(),
            months = new Array('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sept', 'Oct', 'Nov', 'Dec'),
            today = months[date.getMonth()] + '. ' + day,
            phpID = '',
            title = $('#post_title').val();
    
            $.ajax({
                type: 'POST',
                url: 'add_post_to_list.php',
                data: phpID
            }).success( function (data) {
                    phpID = data;
    
                    var post = '<div class="old_posts">' +
                        '<input type="checkbox" name="select_post" class="select_post" value="' + phpID + '">' +
                        '<div class="select_post_separator"><span class="first"></span><span></span><span class="last"></span></div>' +
                        '<input type="text" name="old_post_title" class="old_post_title" value="' + title +'">' +
                        '<span class="old_post_edit" title="' + phpID + '"></span>' +
                        '<p class="old_post_date">' + today + '</p>' +
                        '</div>';
    
                        $('.post_options').after(post);
                        togglePostForm(0);
                        notificationAlert(1);
                        });
                });
    

    delete_post function:

    $('#delete_post_button').click( function (e) {
        e.preventDefault();
    
        var postID = [];
            $(".select_post[name='select_post[]']:checked").each(function () {
                postID.push(parseInt($(this).val()));
            });
    
        $.ajax({
            type: 'POST',
            url: 'delete_post.php',
            data: { select_post : postID }
        }).success( function () {
                var posts = $('.old_posts'),
                       postArray = [];
    
                $(".select_post[name='select_post[]']:checked").each( function () {
                $(this).parent().css('opacity','0');
                $(this).parent().css('padding','0');
                $(this).parent().css('height','0');
                });
            });
    });