checkbox - Checkboxes not posting in PHP -
so trying value 3 checkboxes later sent database, reason 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 posting in page:
<?php if(isset($_post['submit'])){$avatar=$_post['avatar'];} echo $avatar; ?>
but getting
undefined index: avatar on line 14
i have form tags, correct, that's how it's different question, else on page posts correctly, except 1 thing
i think have put <form>
tag around inputs. , in tag should specify (as action) php file that'll reviece data ;)
take @ : http://www.html-form-guide.com/php-form/php-form-checkbox.html
edit:
as per your original post, need treat checkboxes array.
sidenote: should edit underneath original question , marked edit, in order not overwrite original.
i.e.: name="avatar[]"
adding brackets around each element bearing same name attribute. need use foreach
loop, while using conditional statement , placing post array , variable inside 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 $avatars=>$value) { echo "avatars : ".$value."<br />"; } echo $username.",".$password.",".$email."</br>"; } } // brace if(isset($_post['submit'])) ?>
- this example pulled answer on stack: https://stackoverflow.com/a/19734486/
Comments
Post a Comment