Jump to content

Problem with checkbox array


loozah

Recommended Posts

Hi guys, well I've tried everything and checked out as many posts and tutorials as I can but still I can't make this work

 

What I'm trying to do is... after a user has submitted the input (I.E. a comment) it is stored in the db table with "status" set to "0" or pending approval, the data stored is "id" "email_address" "comment" "status" the "id" is auto increment.  The admin can then view a list of "pending approval" records each with a check box ( name  comment[] value $_row['id'] ) the admin can then check the boxes next to the comments he/she wants to approve and on submit the table row "status" will be updated to "1" or approved and the user will be sent an email indicating the comment has been approved

 

What's happening is... everything seems fine except the email is sent to the entire list even if the check box is not checked I have tried a bunch of things but the code below is my latest attempt and it still send email to all...

 

 

<?php

//check the form was submitted

if (isset($_POST['submit']))

{

//check which check boxes checked

if (isset($_POST['comment']))

{

$chkbx = $_POST['comment'];

if ($chkbx)

{

foreach($chkbx as $id)

 

//get email addresses

 

$get_email = mysql_query("SELECT * FROM user_input WHERE id='$id'") or die(mysql_error());

while($row = mysql_fetch_assoc($get_email))

$email    = $row['email'];

}

 

//update db and send email

mysql_query("UPDATE user_input SET status ='1', sendmail ='1' WHERE id ='$aid'") or die(mysql_error());

 

  $to      = "$email";

  $from    = "[email protected]";

  $subject = "Input Approved";

  $message = "Your input has been approved . \"\r\n\" . $email";

  $headers = "From: $from";

  mail( $to, $subject, $message, $headers );

 

}}

?>

 

any help would be greatly appreciated and thanks in advance.

Link to comment
https://forums.phpfreaks.com/topic/202450-problem-with-checkbox-array/
Share on other sites

Thank for the reply... here is the code for the form it's is all on the same page... I have been making some changes and "By George! I think I've got it" well this code seems to work... not saying it's elegant but...  I'd still be interested in any ideas on improving it... I'll will be using "echo <<<HTML" rather than echoing each element separately.  If there are no suggestions I'll mark this as solved in a day or so.

 

<?php require_once('assets/myconfig.php'); ?>

<?php

echo "<div style=\"width:800px; float:left;\">";

echo "<form id=\"frm2\" name=\"frm2\" method=\"POST\" action=\"self.php\">";

 

$get = mysql_query("SELECT * FROM user_inputs WHERE status = '0'");

while($row = mysql_fetch_assoc($get))

{

$id        = $row['id'];

$comment  = $row['comment'];

$email    = $row['email'];

$status    = $row['status'];

 

echo "<div style=\"width:10%; float:left;\">";

echo "<input type=\"checkbox\" name=\"check[]\" value=\"$id\">";

echo "</div>";

echo "<div style=\"width:30%; float:left;\">";

echo $comment;

echo "</div>";

echo "<div style=\"width:30%; float:left;\">";

echo $email;

echo "</div>";

echo "<div style=\"width:30%; float:left;\">";

echo $status;

echo "</div>";

echo "<br />";

}

echo "<br />";

echo "<input type=\"submit\" name=\"submit\" id=\"submit\" value=\"Approve\">";

echo "</form>";

echo "</div>";

 

if (isset($_POST['submit']))

{

$chk = $_POST['check'];

foreach($chk as $id)

{

$getemail = mysql_query("SELECT * FROM user_inputs WHERE id='$id'") or die(mysql_error());

while($row = mysql_fetch_assoc($getemail))

{

$com      = $row['comment'];

$email    = $row['email'];

}

mysql_query("UPDATE user_inputs SET status ='1' WHERE id ='$id'") or die(mysql_error());

 

//udate and send email

  $to      = "$email";

  $from    = "[email protected]";

  $subject = "Input Approved";

  $message = "Your input has been approved\r\n{$com}\r\n{$email}";

  $headers = "From: $from";

  mail( $to, $subject, $message, $headers );

}}

?>

"... I'll will be using "echo <<<HTML" rather ..."

 

this is called Heredoc syntax

 

Consider just closing your PHP when writing significant chunks of HTML instead of, well, echo'ing every line out.  Even over the Heredoc syntax.

 

for example:

 

<?php
//... some PHP code
//... some more PHP code
?>
<div style="width:10%; float:left;">
<input type="checkbox" name="check[]" value="<?php echo $id; ?>"> <!-- NOTE: PHP variable here; -->
</div>
<!-- MORE HTML; -->
<?php
//... more PHP code
?>

 

Also, refrain from using double-quotes whenever possible as PHP will process (try anyways) anything within double-quotes, and this can add up to be some significant, and unnecessary, strain on your server. Use single-quotes in stead:

 

echo "<div style=\"width:30%; float:left;\">";

 

becomes...

 

echo '<div style="width:30%; float:left;">';

 

With that, you can now lose the escaping backslash \ and your code suddenly becomes cleaner.  Now, of course, PHP doesn't parse anything within single-quotes, so this is where concatenation comes into play (the joining of strings).

 

Just remember, large chunks of HTML you're best to to just close PHP and re-open after HTML is complete, and eliminate double-quotes from general parsing practices.

 

NOTE: double-quotes do have their place and are handy and efficient coding at times, so they are not to be discarded completely by any means .. I'm just saying to get into the habit of using single-quotes IMHO.  For example, when using carriage returns/newlines, etc., they must be wrapped by double-quotes only as they will not parse within single-quotes:

 

echo "Something something something\r\n"; //works

echo 'Something something something\r\n'; //will not produce a return or newline

 

Hope this all helps.

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.