Jump to content

if statement disabling my jquery function/button


jason360

Recommended Posts

Hey Guys,

I am stuck on this problem after hours of searching and trial and error. Any help would be greatly appreciated, as I am completely out of ideas. Thank you.

I have a form with a jquery button function, that shows the post live once it is clicked.  The code has been working perfect until I added an 'if statement' on the include file used for displaying/echoing previous posts.  Now, with the if statement, the button no longer works functions displaying the post live through the jquery (note: the button is still posting to the mysql database). Simply put the if statement is disabling the jquery functionality of the submit button.

 

The structrure of my files:

 

index.php (with the form that includes JS script for the button functionality and an include comment.class.php).

 

This is the form and echo script from the index.php page:

<?php if(loggedin()){echo '
            <div id="addCommentContainer">
            <form id="addCommentForm" method="post" action="">
                <div>
                    <img src="http://www.mywebsite.com/images/avatar.jpg" class="avatar" />
                    <p class="commentAdd">Add your comment or review:</p>
                    <!-- <input type="text" name="link" id="link"  /> -->
                    <textarea name="body" id="body"  type="message" ></textarea>
                    <input type="image" id="submit" value="" src="http://www.mywebsite.com/images/commentsubmit.png" class="commentSubmit" />
                    <input type="hidden" name="name" id="name" value="'.$_SESSION['first'].'"/>
                    <input type="hidden" name="user_id" id="user_id" value="'.$_SESSION['user_id'].'" />
                </div>
            </form>
        </div>';} ?>    


        <?php
        /*
        /   Output the comments one by one:
        */
        foreach($comments as $c){

            echo $c->markup();
        }

        ?>

This is the if statement that is causing the problems:

if(loggedin() && $d['user_id'] == $_SESSION['user_id']){ $deletebutton = '<a href="#" id="'.$d['id'].'" class="delete">x</a>';}; 

This is the portion of the include file that holds this problematic if statement:

class Comment
{

private $data = array();

public function __construct($row)
{
    /*
    /   The constructor
    */

    $this->data = $row;
}

public function markup()
{
    /*
    /   This method outputs the XHTML markup of the comment
    */

    // Setting up an alias, so we don't have to write $this->data every time:
    $d = &$this->data;


    // Converting the time to a UNIX timestamp:
    $d['dt'] = strtotime($d['dt']);

  


    if(loggedin() && $d['user_id'] == $_SESSION['user_id']){ $deletebutton = '<a href="#" id="'.$d['id'].'" class="delete">x</a>';}; 


    return '

        <div id="comment">
            <div class="avatar">
                <a href="http://www.mywebsite.com/users/view.php?pid='.$d['user_id'].'" alt"User Link" title="User Link"><img src="http://www.mywebsite.com/users/avatar/'.$d['user_id'].'.jpg" /></a>
            </div>

            <div class="name"><a href="http://www.mywebsite.com/users/view.php?pid='.$d['user_id'].'" alt"User Link" title="User Link">'.$d['name'].'</a></div>
            <div class="date" title="Added at '.date('H:i \o\n d M Y',$d['dt']).'">'.date('d M Y',$d['dt']).'</div>
            <p class="commentText">'.$d['body'].'</p>
            '.$deletebutton.'
        </div>
    ';
}

This is the JS that performs the form submit button functionality:

 $(document).ready(function(){
/* The following code is executed once the DOM is loaded */

/* This flag will prevent multiple comment submits: */
var working = false;

/* Listening for the submit event of the form: */
$('#addCommentForm').submit(function(e){

    e.preventDefault();
    if(working) return false;

    working = true;
    $('#submit').val('Working..');
    $('span.error').remove();

    /* Sending the form fileds to submit.php: */
    $.post('submit.php',$(this).serialize(),function(msg){

        working = false;
        $('#submit').val('Submit');

        if(msg.status){

            /* 
            /   If the insert was successful, add the comment
            /   below the last one on the page with a slideDown effect
            /*/

            $(msg.html).hide().insertBefore('#addCommentContainer').slideDown();
            $('#body').val('');
        }
        else {

            /*
            /   If there were errors, loop through the
            /   msg.errors object and display them on the page 
            /*/

            $.each(msg.errors,function(k,v){
                $('label[for='+k+']').append('<span class="error">'+v+'</span>');
            });
        }
    },'json');

});
});

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.