Using HTML checkboxes with PHP
Posted: Mon Aug 22, 2005 12:15 pm
Hi guys,
My first time on here - be gentle!
I'm trying to code a Newsletter system, but I cannot grasp how to tell PHP if a checkbox has been selected or not.
Here is my code (you only need to look at the recipients bit):
Ok. That's it. I just want PHP to know which checkboxes were selected when the page is submitted (or one of the 3 buttons are used (at the moment I'm working on the Delete button)). I can see that $x would never equal selected, but the thing that troubles me is how to create a dynamic checkbox name/value/w/e and then refer to it when its been submitted without it looking at the true or fake value (I hope you can see where I'm coming from).
I don't get any errors or anything the page just acts like it's refreshing.
Any ideas would be great.
Thanks in Advance.
feyd | Please use
My first time on here - be gentle!
I'm trying to code a Newsletter system, but I cannot grasp how to tell PHP if a checkbox has been selected or not.
Here is my code (you only need to look at the recipients bit):
Code: Select all
<?php
//Include the HTML header
include ('includes/header.html');
//Connect to MySQL
require_once ('config.php');
//**HOME***
if ($_GET['page'] == "") {
//Print the content
echo '<b><u>Welcome to PHPJayMail, by Jay-Designs.co.uk</b></u><br><br>';
echo 'Please select a page from the list below:<br>';
echo '<a href="index.php?page=recipients">Edit/View Recipients</a><br>';
echo '<a href="index.php?page=sendmail">Send Mail</a>';
}
//*********
//***RECIPIENTS***
if ($_GET['page'] == "recipients") {
//If records need to be deleted...
if ($_POST['delete']) {
$query = "SELECT email FROM " . TBL_NAME;
$result = @mysql_query ($query);
$x = 1;
while ($row = mysql_fetch_array ($result, MYSQL_NUM)) {
if ($x == "selected") {
$query = "DELETE FROM " . TBL_NAME . " WHERE email = '" . $row['0'] . "'";
$result = @mysql_query ($query);
if ($result) {
header ("Location: http://" . URL . "/index.php?page=recipients");
$message = "You successfully deleted the record(s)!<br>";
} else {
header ("Location: http://" . URL . "/index.php?page=recipients");
$message = "Sorry, but there has been a technical difficulty.<br>";
}
}
$x++;
}
}
//Print the title
echo '<b><u>', $_GET['page'], '</u></b>';
//Print the message if there is one
if (isset($message)) {
echo $message;
}
//Fetch the mailing list
$query = "SELECT * FROM " . TBL_NAME;
$result = @mysql_query ($query);
//Initialise the $first and $x variables
$first = 1;
$x = 1;
//Print the mailing list
while ($row = mysql_fetch_array ($result, MYSQL_NUM)) {
if ($first) {
echo '<form action="index.php?page=recipients" method="post"><table border="1"><tr><td><b>Email</b></td><td><b>Name</b></td><td><b>Approved?</b></td><td><b>Select</b></td></tr>';
}
echo '<tr><td>' . $row['0'] . '</td><td>' . $row['1'] . '</td><td align="center">' . $row['2'] . '</td><td align="center"><input type="checkbox" name="box" value="' . $row['0'] . '"></tr>';
$first = 0;
$x++;
}
echo '<tr><td colspan="4" align="center">With selected: <input type="submit" name="delete" value="Delete"> <input type="submit" name="approve" value="Approve"> <input type="submit" name="unapprove" value="Unapprove"></td></tr></table>';
}
//*********
//Close the MySQL connection
mysql_close();
//Include the HTML footer
include ('includes/footer.html');
?>Ok. That's it. I just want PHP to know which checkboxes were selected when the page is submitted (or one of the 3 buttons are used (at the moment I'm working on the Delete button)). I can see that $x would never equal selected, but the thing that troubles me is how to create a dynamic checkbox name/value/w/e and then refer to it when its been submitted without it looking at the true or fake value (I hope you can see where I'm coming from).
I don't get any errors or anything the page just acts like it's refreshing.
Any ideas would be great.
Thanks in Advance.
feyd | Please use
Code: Select all
andCode: Select all
tags where approriate when posting code. Read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]