Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi, I have an embarrassing problem, I don't handle so well jquery , and I'm trying to make a button or two, that should toggle off and on some forms, but I don't manage to specify which exact form , and that's why when I push 'edit' a edit form will appear for all the comments not just for one , and I've tried to fix it(by inserting a different ID for forms) , but I stumble upon the code , If someone could help me it would be nice , if not sorry for being off-topic...
Here is the code
functions.php :
PHP
function get_comments($file_id) {
		include 'database.php';

		$result = mysqli_query($connect, "SELECT * FROM `comments` WHERE `file_id`='$file_id' AND `is_child`=FALSE ORDER BY `date` DESC");
		$row_cnt = mysqli_num_rows($result);

		echo '<h1>Comments ('.$row_cnt.')</h1>';
		echo '<div class="comment">';
			new_comment();
		echo '</div>';

		foreach($result as $item) {
			$date = new dateTime($item['date']);
			$date = date_format($date, 'M j, Y | H:i:s');
			$auth = $item['author'];
			$par_code = $item['com_code'];
// WHERE `par_code`='$par_code' AND `is_child`=TRUE    add_comment($item['author'], $item['com_code']);
			$chi_result = mysqli_query($connect, "SELECT * FROM `comments` WHERE `par_code`='$par_code' AND `is_child`=TRUE");
			$chi_cnt = mysqli_num_rows($chi_result);

			echo '<div class="comment" name="'.$item['com_code'].'">'
					.'<span class="author">'.$auth.'</span><br />'
					.$item['comment'].'<br />'
					.'<span class="date">Posted: '.$date.'</span><br />';

					if($chi_cnt == 0) {
						echo '<span class="replies">No replies</span>'
							.'<span class="replies"> Reply</span>'
							.add_comment($item['author'], $item['com_code']);;
					} else {
						echo '<span class="replies">[+] '.$chi_cnt.' replies</span>'
							.'<span class="replies" Reply</span>';
							add_comment($item['author'], $item['com_code']);
						echo '<div name="children" id="children">';
						foreach($chi_result as $com) {
							$chi_date = new dateTime($com['date']);
							$chi_date = date_format($chi_date, 'M j, Y | H:i:s');

							echo '<div class="child" name="'.$com['com_code'].'">'
									.'<span class="author">'.$com['author'].'</span><br />'
									.$com['comment'].'<br />'
									.'<span class="date">Posted: '.$chi_date.'</span><br />'
								.'</div>'
								.delete_comment()
								.edit_comment();
						}
						echo '</div>';
					}
				echo '</div>';
		}
		mysqli_close($connect);
	}

	function add_comment($reply, $code) {
		echo '<button class="button1">Reply</button><button class="button2">Edit</button><button class="button3">Delete</button>
		<form id="'.$code.'" action="reply.php" method="post" enctpye="" name="new_comment">'//
				.'<input type="hidden" name="par_code" value="'.$code.'" />'
				.'<textarea class="text_cmt" name="text_cmt" placeholder="Reply to '.$reply.'"></textarea><br />'
				.'<input type="submit" value="Reply" />'
			.'</form>';
	}

	function new_comment() {
		echo '<form action="new.php" method="post" enctpye="" name="new_comment">'
				.'<textarea class="text_cmt" name="text_cmt" placeholder="Post a new comment"></textarea><br />'
				.'<input type="submit" value="Post" />'
			.'</form>';
	}

	function delete_comment() {
		echo '<form action="delete.php" method="post" enctpye="" id="delete">'
				.'<input name="password" type="text" placeholder="Password to delete" id="password">'
				.'<input type="submit" value="Delete" />'
			.'</form>';
	}
	function edit_comment() {
		echo '<form action="edit.php" method="post" enctpye="" id="edit">'
				.'<textarea class="text_cmt" name="text_cmt"></textarea><br />'
				.'<input name="password" type="text" placeholder="Password to edit" id="password"><input type="submit" value="Edit" />'
			.'</form>';
	}

	function generateRandomString($length = 10) {
		$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
		$characterLength = strlen($characters);
		$randomString = '';

		for($i = 0; $i < $length; $i++) {
			$randomString .= $characters[rand(0, $characterLength - 1)];
		}
		return $randomString;
	}
	function checkString($com_code) {
		include 'database.php';

		$rand = generateRandomString();
		$result = mysqli_query($connect, "SELECT * FROM `comments` WHERE `com_code`='$com_code'");
		$row_cnt = mysqli_num_rows($result);

		if($row_cnt != 0) {
			return $rand;
		} else {
			checkString($rand);
		}
	}

index.php :
PHP
<?php
	require_once 'database.php';
	require_once 'functions.php';
?>
<!doctype html>
<html>
	<head>
		<title>YouTube Comment Test</title>
		<link rel="stylesheet" href="styles.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
	</head>
	<body>
		<?php
			get_comments('1234');
		?>
<script> $('.button2').click(function() {$('form#edit').toggle();});</script>
<script> $('.button3').click(function() {$('form#delete').toggle();});</script>

	</body>
</html>

and this http://jsfiddle.net/46ww7v5L/1/[^] is the result from the two file , you can see there that when I click one button , a edit/delete form will be displayed for all comments , I want just for a specific one...
Posted

1 solution

One problem could be that you are not making sure your .button2 and .button3 are already rendered on the page before you try to assign any jQuery functionality.

Items should exist there before you try to assign functionality.

As a first approach, I'd suggest you use a single script tag, include language="javascript" in that tag, and put those 2 functions within the next code block:

JavaScript
<script language="javascript">

$(document).ready(
   function() {

       $('.button2').click(function() {$('form#edit').toggle();});

       $('.button3').click(function() {$('form#delete').toggle();});

   }
);

</script>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900