3

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

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
s.K.
  • 65
  • 1
  • 8
  • 1
    Where are your form tags? – Rizier123 May 15 '15 at 14:01
  • maybe you have no `
    ` tag at all, its just inputs and stuff, wrap the table with it
    – Kevin May 15 '15 at 14:01
  • I have edited my post and written in a comment, I have form tags, I have multiple things that post on the page correctly EXCEPT this – s.K. May 15 '15 at 14:07
  • **A:** - Checkboxes all bearing the same name attribute, require them to be treated as arrays; you're not doing that, in turn overwriting all other checkboxes chosen. You then need to loop over that array. – Funk Forty Niner May 15 '15 at 14:08
  • @Fred-ii- I dont really understand what you mean specifically, since I havent encountered this problem before, could you show me an example of a loop that fixes this? Arrays are not my strong side – s.K. May 15 '15 at 14:12
  • @s.K. Look at the code in the link that they gave you. I'm hoping they will improve their answer in order to provide a solution for you. However, that link does include code that will take arrays into account. `name="avatar[]"` for all. – Funk Forty Niner May 15 '15 at 14:14
  • http://stackoverflow.com/q/14543050/ and http://stackoverflow.com/q/14026361/ you'll find your answers there too. – Funk Forty Niner May 15 '15 at 14:19
  • @Fred-ii- using that array, when posting i get "You didn't select any avatars". Thanks for the help, I will try and fix it, if now I will switch to radio buttons – s.K. May 15 '15 at 14:19
  • You're welcome. I left you a few links above with Q&A's. Those will fix it, I am sure of it. If that fails (which I doubt), then yeah... radio buttons will be a fix, just not one for the intended method ;-) – Funk Forty Niner May 15 '15 at 14:21
  • Sidenote: I noticed your edit `$password=$_POST['password'];` I hope you're storing a hash and not a plain text method. – Funk Forty Niner May 15 '15 at 14:24
  • @Fred-ii- oh yeah I will, but it's a local project just for an assignment, nothing too public or big, but I will be using a hash. This is just for testing – s.K. May 15 '15 at 14:26
  • Great! Plus, you do have a closing `` tag right? It's not in your edit. I like dotting the `i`'s and crossing the `t`'s ;-) – Funk Forty Niner May 15 '15 at 14:26
  • @Fred-ii- yes, forgot to copy it :P – s.K. May 15 '15 at 14:27
  • @s.K. Sidenote: You should always do an edit underneath your original question and marked as an edit, in order not to overwrite the original as noted in the answer that I wrote up. People visiting the question will see `name="avatar[]"` in your newly posted edit and then ask themselves: *"why the answer, the brackets are there"*. Food for thought ;-) People might downvote their answer because of it. You should probably rollback to your original post just in case. – Funk Forty Niner May 15 '15 at 14:48
  • @s.K. I had to perform a rollback, just in case the answer receives a downvote based on your previous edit where you added the brackets for the checkboxes. In the future, please mark edits **as an edit** and placed underneath your original post, stating something like: *"this is what I have tried using some of the answers below, but did not work...."* type of thing. That's how things are done on Stack ;-) *Cheers* – Funk Forty Niner May 15 '15 at 14:55

1 Answers1

5

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']))

?>
Community
  • 1
  • 1
Guillaume Munsch
  • 1,233
  • 17
  • 37
  • I have done that, everything else on the page posts fine except the avatar – s.K. May 15 '15 at 14:01
  • thanks for overwriting my edit. something is not showing. use ticks – Funk Forty Niner May 15 '15 at 14:01
  • It was just right after your edit @Fred-ii-, I was watching as it happened. Just unfortunate timing.. – Rizky Fakkel May 15 '15 at 14:02
  • @RizkyFakkel probably lol well, I've edited again. If they overwrite it without using ticks to highlight special tags, I won't do it again. – Funk Forty Niner May 15 '15 at 14:04
  • 1
    Hmmmmmm, just to try, add '[]' at the end of your 'avatar' in the html, but not in the php, and tell me if it works – Guillaume Munsch May 15 '15 at 14:04
  • *"I think you have to put a `
    ` tag"* - True, and specifying a post method.
    – Funk Forty Niner May 15 '15 at 14:04
  • first of wrap all input codes into form tag than all checkbox name should be "avatar[]". – Suman Singh May 15 '15 at 14:05
  • @s.K. *"I have done that, everything else on the page posts fine except the avatar"* - Update your question/code then in order to show us what you're ***really*** using. If that is your full code, then it's obvious; missing `
    ` tags and a post method.
    – Funk Forty Niner May 15 '15 at 14:06
  • i did specifically say that its not my full code , but except the form tags, the rest is not important since it posts correctly – s.K. May 15 '15 at 14:11
  • @LeZohan68 Your comment about `[]` is correct as is the example link you included in your answer. I upvoted your answer, however, link-only answers are discouraged if and when that website/page no longer exists, will render your answer useless and may be flagged later on, should it ever happen. You should be using parts of the code in your answer, relevant to the question. – Funk Forty Niner May 15 '15 at 14:12
  • @Fred-ii- Notice: Undefined index: avatar in C:\xampp\htdocs\ovningar\meet\reg_test.php on line 14 Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\ovningar\meet\reg_test.php on line 20 – s.K. May 15 '15 at 14:34
  • @s.K. I find that rather impossible to believe. Did you highlight the entire code, rather than just replacing some parts? – Funk Forty Niner May 15 '15 at 14:35
  • @s.K. ah ok. That's because you've clicked on the submit without checking any boxes. Let me see what I can do about that. I will re-edit shortly. – Funk Forty Niner May 15 '15 at 14:36
  • 2
    @s.K. Ok, reload the answer. I had to put `$avatar=$_POST['avatar'];` inside another conditional statement `if(isset($_POST['avatar']))`. – Funk Forty Niner May 15 '15 at 14:40
  • 1
    @LeZohan68 You now have enough rep points to give comments under a question, which what you posted as your original answer http://stackoverflow.com/revisions/30261427/1 and an added link in another http://stackoverflow.com/revisions/30261427/3 which are considered as comments. In doing so for later answers, you stand at getting downvoted for it. A few people gave you a break here (including myself) and you were lucky. You may not be so lucky next time ;-) – Funk Forty Niner May 15 '15 at 15:01