Send value "0" or "1" from the checkbox to the database

Asked

Viewed 647 times

0

I have the table "Cd_comments" in the mysql database, with the column "comment_id" "user_id" "text" and "anonymity" I want to make a system for a website, where the user will comment, and if he has marked the checkbox "send anonymously?" it sends the value "1" to the column "anonimo" of the table "comments" and if nothing is marked it sends the value "0"

I am using the following input for the checkbox

                <input type="hidden" name="anony" id="caixa-disabled" value="0" <?php echo ($cd['comentarios']['anononimo'] == 0) ? 'checked': '';?>>
                 <input type="checkbox" name="anony" id="caixa-enabled" value="1" <?php echo ($cd['comentarios']['anononimo'] == 1) ? 'checked': '';?>>

But mark or not mark, no value is sent to the database.

I don’t use pure html, so I can do the logical and visual part all together, since it’s a simple form, on a site that only has one page. Should I use echo? Post? Something needs to be done in jquery to make it work or only PHP can solve the problem.

Sorry, I study programming very little time (less than 1 month to be more exact) and I’m lost

  • 2

    Friend, post the code of how you are receiving this data and entering in the bank.

  • Should I edit my question? Because that’s exactly what it’s about: How to get the value of the checkbox entered into the database.

  • By default unmarked checkboxes are not sent by the form... just check if the variable exists and the value is 1, if there is simply grave false in the database

  • I really appreciate your help, but this is exactly what I have no idea how to do. I don’t know if this is done with PHP, with jquery, or with q, I was webdesign and only worked with HTML, without database. Today I study programming and I’m learning about databases, but I still don’t know how to take the data entered in a form or something like that to a database, and what to use for that. PHP? Jquery? Both? The logical part of the programming "if, Else, if Else" is blz, but do Posts, Gets, Selects, which may be my Balance heel in this less than 1 month of programming

  • 1

    If it’s just a checkbox, it’s enough: $marcado = isset($_POST['name']); "name" is the name you put in the checkbox. If there are several, generate the name of each one with the ID together

  • Thanks Bacco, now I have placed myself better knowing that the "name" is the name of the checkbox. I just have a question, the marked $I will use in the checkbox input in html?

  • You can use for example <input type="checkbox" value="1" name="anonimo"<?=$is_anon?' checked':''?>> so if $is_anon is true the string will be included checked (note that you have to leave 1 blank space at the beginning checked) - If it is false will be worth the '' empty string.

  • Ai you get your checkboxes like this, for example $is_anon = isset($_POST['anonimo']); $is_news = isset($_POST['newsletter']); and so on, giving different names to each one, so that you know which is which (I used different names in the PHP variable and in the "name" of HTML for you to see which is which) - remembering that is still $_POST if it’s <form method="post", otherwise it is $_GET

  • To save in DB can use something like this: $campo_anon=$is_anon?'1':'0'; thus $campo_anon will be 1 or 0. (the same applies if you want sim and não, just replace 1 and 0)

Show 4 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.