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
Post
by vlio20 » Sat Jun 02, 2012 7:05 am
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
requinix
Spammer :|
Posts: 6617 Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA
Post
by requinix » Sat Jun 02, 2012 4:44 pm
You didn't put a method on that form so it will default to GET.
vlio20
Forum Commoner
Posts: 26 Joined: Sat Jun 02, 2012 6:46 am
Post
by vlio20 » Sun Jun 03, 2012 1:23 am
thanks!!! you helped me alot
vlio20
Forum Commoner
Posts: 26 Joined: Sat Jun 02, 2012 6:46 am
Post
by vlio20 » Sun Jun 03, 2012 2:58 am
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
requinix
Spammer :|
Posts: 6617 Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA
Post
by requinix » Sun Jun 03, 2012 4:14 am
What does
show? And did you check any of the checkboxes?
vlio20
Forum Commoner
Posts: 26 Joined: Sat Jun 02, 2012 6:46 am
Post
by vlio20 » Sun Jun 03, 2012 4:22 am
It shows: NULL in any case (checked and unchecked)
requinix
Spammer :|
Posts: 6617 Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA
Post
by requinix » Sun Jun 03, 2012 4:27 am
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
Post
by vlio20 » Sun Jun 03, 2012 4:31 am
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>
requinix
Spammer :|
Posts: 6617 Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA
Post
by requinix » Sun Jun 03, 2012 4:45 am
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
Post
by vlio20 » Sun Jun 03, 2012 4:56 am
here is what i see:
before licking delete:
[/img]
after:
[/img]
requinix
Spammer :|
Posts: 6617 Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA
Post
by requinix » Sun Jun 03, 2012 5:09 am
(anywhere) then click both checkboxes and submit. What's the output?
vlio20
Forum Commoner
Posts: 26 Joined: Sat Jun 02, 2012 6:46 am
Post
by vlio20 » Sun Jun 03, 2012 5:12 am
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
requinix
Spammer :|
Posts: 6617 Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA
Post
by requinix » Sun Jun 03, 2012 5:48 am
...Ah.
The checkboxes are outside the <form>.
vlio20
Forum Commoner
Posts: 26 Joined: Sat Jun 02, 2012 6:46 am
Post
by vlio20 » Sun Jun 03, 2012 5:51 am
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
Post
by vlio20 » Sun Jun 03, 2012 5:56 am
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!!!!