So I am trying to get the value from 3 checkboxes that will later be sent to a database, but for some reason they are not posting in PHP when im testing.
<table width="200" border="0">
<tr>
<td><img src="images/image1.jpg" ></td>
<td><img src="images/image2.jpg" ></td>
<td><img src="images/image3.jpg" ></td>
</tr>
<tr>
<td><input type="checkbox" id="checkbox65" class="css-checkbox med" name="avatar" value="image1"/>
<label for="checkbox65" class="css-label med elegant" /></label></td>
<td><input type="checkbox" id="checkbox66" class="css-checkbox med" name="avatar" value="image2"/>
<label for="checkbox66" class="css-label med elegant" /></label></td>
<td><input type="checkbox" id="checkbox67" class="css-checkbox med" name="avatar" value="image3"/>
<label for="checkbox67" class="css-label med elegant" ></label></td>
</tr>
</table><br>
and I am posting in another page:
<?php
if(isset($_POST['submit'])){$avatar=$_POST['avatar'];}
echo $avatar; ?>
but I am getting
Undefined index: avatar on line 14
I have form tags, everything is correct, that's how it's a different question, Everything else on the page posts correctly, except this one thing
I think you have to put a <form>
tag around your inputs. And in that tag you should specify (as action) the php file that'll reviece the data ;)
Take a look at that : http://www.html-form-guide.com/php-form/php-form-checkbox.html
Edit:
As per your original post, you need to treat your checkboxes as an array.
Sidenote: You should always do an edit underneath your original question and marked as an edit, in order not to overwrite the original.
I.e.: name="avatar[]"
adding brackets around each element bearing the same name attribute. You then need to use a foreach
loop, while using a conditional statement and placing the POST array for it and its variable inside that loop.
<form action="reg_test.php" method="post">
Username:<br>
<input type="text" name="username"><br>
Password:
<input type="password" name="password" ><br>
E-mail:
<input type="text" name="email" ><br>
Avatar:
<table width="200" border="0">
<tr>
<td><img src="images/avatars/image1.jpg" ></td>
<td><img src="images/avatars/image2.jpg" ></td>
<td><img src="images/avatars/image3.jpg" ></td>
</tr>
<tr>
<td><input type="checkbox" id="checkbox65" class="css-checkbox med" name="avatar[]" value="image1"/>
<label for="checkbox65" class="css-label med elegant" /></label></td>
<td><input type="checkbox" id="checkbox66" class="css-checkbox med" name="avatar[]" value="image2"/>
<label for="checkbox66" class="css-label med elegant" /></label></td>
<td><input type="checkbox" id="checkbox67" class="css-checkbox med" name="avatar[]" value="image3"/>
<label for="checkbox67" class="css-label med elegant" ></label></td>
</tr>
</table><br>
<input type="submit" value="Register" name="submit">
</form>
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
if(isset($_POST['submit'])){
$username=$_POST['username'];
$password=$_POST['password'];
$email=$_POST['email'];
if(isset($_POST['avatar'])){
$avatar=$_POST['avatar'];
foreach ($avatar as $avatars=>$value) {
echo "Avatars : ".$value."<br />";
}
echo $username.",".$password.",".$email."</br>";
}
} // brace for if(isset($_POST['submit']))
?>