HELP with the isset function

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

vlio20
Forum Commoner
Posts: 26
Joined: Sat Jun 02, 2012 6:46 am

HELP with the isset function

Post by vlio20 »

Hi,
I have some problem with the isset function. when clicking delete nothing happens, even when i try to echo something.

here is the code:

Code: Select all


<?php
session_start(); // a veriable that sticks to a user

include("includes/connect.php");
include("includes/html_codes.php");

			
?>

<!DOCTYOE html>
<html lang="en">
<title>Messages</title>
<head>
		<link rel="stylesheet" href="css/main.css">
		<link rel="stylesheet" href="css/forms.css">
		<link rel="stylesheet" href="css/account.css">
</head>
<body>
	
	<div id="wrapper">
		<?php headerAndSearchCode(); ?>
		<aside id="main_aside"> <?php  accountLinks(0);  ?></aside>
			<div id="messagebar">
				<form>
					<input type="submit" name="db" class="groovybutton" value="Delete"/>
					<input type="submit" name="archive" class="groovybutton" value="Archive" />
				</form>
			</div>
			
			<aside id="main_aside">
				<?php 	$userid = $_SESSION['user_id'];
					$allmessages = mysql_query("SELECT * FROM messages WHERE receiver='$userid' ORDER BY date_sent DESC") or die(mysql_error());
					$rowCounter = mysql_num_rows($allmessages);
					echo "<div id=\"massagebox\">";
						if($rowCounter == 0){
							echo "
									<h2 class=\"txt\">There are no messages to show.</>
							";
						}else{
							echo"
								<table class=\"mesBox\" >
									<tr>
										<th class=\"select\">Check</th>
										<th class=\"from\">From</th>
										<th class=\"sub\">Subject</th>
										<th class=\"date\">Date received</th>
									</tr>
									<tr>
										<td colspan=\"4\"></br></td>
									</tr>
							";
							while($row = mysql_fetch_array($allmessages) or die(mysql_error())){
								$rowCounter--;
								$id = $row['id'];
								$status = $row['status'];
								$sender = $row['sender'];
								$tmp = mysql_query("SELECT username FROM users WHERE user_id='$sender' LIMIT 1") or die(mysql_error());
								$tmp2 = mysql_fetch_array($tmp) or die(mysql_error());
								$senderUsername = $tmp2['username'];
								$sub = $row['subject'];
								$date = $row['date_sent'];
								
								if($status == 'unread'){
									echo '
										<tr class="linkMesBoxUnread">
											<td><input type="checkbox" name="checked[]" value='."$id".'></td>
											<td><a href="read_message.php'.'?mid='.$id.'">'."$senderUsername".'</a></td>
											<td>'."$sub".'</td>
											<td>'."$date".'</td>
										</tr>
									';
								}else{
									echo '
										<tr class="linkMesBox">
											<td><input type="checkbox" name="checked[]" value='."$id".'></td>
											<td><a href="read_message.php'.'?mid='.$id.'">'."$senderUsername".'</a></td>
											<td>'."$sub".'</td>
											<td>'."$date".'</td>
										</tr>
									';
								}
								if($rowCounter != 0){
									echo '
										<tr>
											<td colspan="4"><hr class="division"></td>
										</tr>
									';
								}else{break;}
							}
							echo '</table>';
														
						}
					
					echo '</div>';
					
					$checked = $_POST['checked'];
					if(isset($_POST['db'])){
						echo "123";
						foreach($checked as $messageId){
							$result = mysql_query("DELETE FROM messages WHERE id='$messageId'") or die(mysql_error());
								if(!$result){
									echo "There is some problem with the server please make contact with as </br> Thank you,</br> HobeXchange";
								}
						}
					}	
				?>
			</aside>
		<?php footerCode();?>
	</div>
	
</body>

</html>









thanks
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: HELP with the isset function

Post by requinix »

You didn't put a method on that form so it will default to GET.

Code: Select all

<form method="post">
vlio20
Forum Commoner
Posts: 26
Joined: Sat Jun 02, 2012 6:46 am

Re: HELP with the isset function

Post by vlio20 »

thanks!!! you helped me alot
vlio20
Forum Commoner
Posts: 26
Joined: Sat Jun 02, 2012 6:46 am

Re: HELP with the isset function

Post by vlio20 »

now one more problem that I have with the multiple checkbox handling.
in line:

Code: Select all

if($status == 'unread'){
									echo '
										<tr class="linkMesBoxUnread">
											<td><input type="checkbox" name="tags[]" value='."$id".'></td>
											<td><a href="read_message.php'.'?mid='.$id.'">'."$senderUsername".'</a></td>
											<td>'."$sub".'</td>
											<td>'."$date".'</td>
										</tr>
									';
								}else{
									echo '
										<tr class="linkMesBox">
											<td><input type="checkbox" name="tags[]" value='."$id".'></td>
											<td><a href="read_message.php'.'?mid='.$id.'">'."$senderUsername".'</a></td>
											<td>'."$sub".'</td>
											<td>'."$date".'</td>
										</tr>
									';
								}


and the action is:

Code: Select all

$checked = $_POST['tags'];
					if(isset($_POST['db'])){
						foreach($checked as $messageId){
							$result = mysql_query("DELETE FROM messages WHERE id='$messageId'") or die(mysql_error());
								if(!$result){
									echo "There is some problem with the server please make contact with as </br> Thank you,</br> HobeXchange";
								}
						}
					}

I get this error/warning:

Warning: Invalid argument supplied for foreach() in /home/vlio20/public_html/admin/messages.php on line 100
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: HELP with the isset function

Post by requinix »

What does

Code: Select all

var_dump($checked);
show? And did you check any of the checkboxes?
vlio20
Forum Commoner
Posts: 26
Joined: Sat Jun 02, 2012 6:46 am

Re: HELP with the isset function

Post by vlio20 »

It shows: NULL in any case (checked and unchecked)
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: HELP with the isset function

Post by requinix »

Then either (a) you're still not using POST or (b) the checkboxes aren't actually named "tags[]".
vlio20
Forum Commoner
Posts: 26
Joined: Sat Jun 02, 2012 6:46 am

Re: HELP with the isset function

Post by vlio20 »

here is the code after all the corrections:

Code: Select all

<?php
session_start(); // a veriable that sticks to a user

include("includes/connect.php");
include("includes/html_codes.php");

			
?>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html lang="en">
<title>Messages</title>
<head>
		<link rel="stylesheet" href="css/main.css">
		<link rel="stylesheet" href="css/forms.css">
		<link rel="stylesheet" href="css/account.css">
</head>
<body>
	
	<div id="wrapper">
		<?php headerAndSearchCode(); ?>
		<aside id="main_aside"> <?php  accountLinks(0);  ?></aside>
			<div id="messagebar">
				<form method="post"">
					<input type="submit" name="db" class="groovybutton" value="Delete"/>
					<input type="submit" name="archive" class="groovybutton" value="Archive" />
				</form>
			</div>
			
			<aside id="main_aside">
				<?php 	$userid = $_SESSION['user_id'];
					$allmessages = mysql_query("SELECT * FROM messages WHERE receiver='$userid' ORDER BY date_sent DESC") or die(mysql_error());
					$rowCounter = mysql_num_rows($allmessages);
					echo "<div id=\"massagebox\">";
						if($rowCounter == 0){
							echo "
									<h2 class=\"txt\">There are no messages to show.</>
							";
						}else{
							echo"
								<table class=\"mesBox\" >
									<tr>
										<th class=\"select\">Check</th>
										<th class=\"from\">From</th>
										<th class=\"sub\">Subject</th>
										<th class=\"date\">Date received</th>
									</tr>
									<tr>
										<td colspan=\"4\"></br></td>
									</tr>
							";
							while($row = mysql_fetch_array($allmessages) or die(mysql_error())){
								$rowCounter--;
								$id = $row['id'];
								$status = $row['status'];
								$sender = $row['sender'];
								$tmp = mysql_query("SELECT username FROM users WHERE user_id='$sender' LIMIT 1") or die(mysql_error());
								$tmp2 = mysql_fetch_array($tmp) or die(mysql_error());
								$senderUsername = $tmp2['username'];
								$sub = $row['subject'];
								$date = $row['date_sent'];
								
								if($status == 'unread'){
									echo '
										<tr class="linkMesBoxUnread">
											<td><input type="checkbox" name="tags[]" value='."$id".'></td>
											<td><a href="read_message.php'.'?mid='.$id.'">'."$senderUsername".'</a></td>
											<td>'."$sub".'</td>
											<td>'."$date".'</td>
										</tr>
									';
								}else{
									echo '
										<tr class="linkMesBox">
											<td><input type="checkbox" name="tags[]" value='."$id".'></td>
											<td><a href="read_message.php'.'?mid='.$id.'">'."$senderUsername".'</a></td>
											<td>'."$sub".'</td>
											<td>'."$date".'</td>
										</tr>
									';
								}
								if($rowCounter != 0){
									echo '
										<tr>
											<td colspan="4"><hr class="division"></td>
										</tr>
									';
								}else{break;}
							}
							echo '</table>';
														
						}
					
					echo '</div>';
					
					if(isset($_POST['db'])){
						$checked = $_POST['tags'];
						var_dump($checked);
						foreach($checked as $messageId){
							$result = mysql_query("DELETE FROM messages WHERE id='$messageId'") or die(mysql_error());
								if(!$result){
									echo "There is some problem with the server please make contact with as </br> Thank you,</br> HobeXchange";
								}
						}
					}	
				?>
			</aside>
		<?php footerCode();?>
	</div>
	
</body>

</html>


User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: HELP with the isset function

Post by requinix »

There are rows to check, right? It doesn't tell you "there are no messages to show", right?
Because I'm pretty sure it does tell you that, and I base my conclusion on another bug which should prevent the deleting code from even running in the first place.
vlio20
Forum Commoner
Posts: 26
Joined: Sat Jun 02, 2012 6:46 am

Re: HELP with the isset function

Post by vlio20 »

here is what i see:
before licking delete:
Image
[/img]

after:
Image
[/img]
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: HELP with the isset function

Post by requinix »

Code: Select all

var_dump($_POST);
(anywhere) then click both checkboxes and submit. What's the output?
vlio20
Forum Commoner
Posts: 26
Joined: Sat Jun 02, 2012 6:46 am

Re: HELP with the isset function

Post by vlio20 »

getting this message:
array(1) { ["db"]=> string(6) "Delete" } NULL
Warning: Invalid argument supplied for foreach() in /home/vlio20/public_html/admin/messages.php on line 101
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: HELP with the isset function

Post by requinix »

...Ah.

The checkboxes are outside the <form>.
vlio20
Forum Commoner
Posts: 26
Joined: Sat Jun 02, 2012 6:46 am

Re: HELP with the isset function

Post by vlio20 »

yes i checked them, what do you mean by "What's the HTML source "?
vlio20
Forum Commoner
Posts: 26
Joined: Sat Jun 02, 2012 6:46 am

Re: HELP with the isset function

Post by vlio20 »

Changed it to:

Code: Select all

if($status == 'unread'){
									echo '
										<tr class="linkMesBoxUnread">
											<td><form method="post"><input type="checkbox" name="tags[]" value='."$id".'></form></td>
											<td><a href="read_message.php'.'?mid='.$id.'">'."$senderUsername".'</a></td>
											<td>'."$sub".'</td>
											<td>'."$date".'</td>
										</tr>
									';
								}else{
									echo '
										<tr class="linkMesBox">
											<td><form method="post"><input type="checkbox" name="tags[]" value='."$id".'></form></td>
											<td><a href="read_message.php'.'?mid='.$id.'">'."$senderUsername".'</a></td>
											<td>'."$sub".'</td>
											<td>'."$date".'</td>
										</tr>
									';
								}
and still getting this:
array(1) { ["db"]=> string(6) "Delete" } NULL
Warning: Invalid argument supplied for foreach() in /home/vlio20/public_html/admin/messages.php on line 101

frustrating!!!!
Post Reply