StyleOnPC
StyleOnPC

Reputation: 39

PHP: Getting the value of a radio button and storing it into a database

I have never really used radio buttons before. So what I am trying to achieve is, using two radio buttons 'Public' and 'Private'. This is for the user's profile. My HTML code for the buttons is;

<div class="btn-group" data-toggle="buttons-radio">
    <button type="button" class="btn btn-primary" id="privacy" name="privacy"   value="0">Private</button>
    <button type="button" class="btn btn-primary" id="privacy" name="privacy"   value="1">Public</button>
</div>

As for the PHP, I'm not sure how to get the value and store it. Any help is appreciated!

Upvotes: 1

Views: 24085

Answers (5)

GolezTrol
GolezTrol

Reputation: 116100

Use a form.

<form method="get" action="yourscript.php">

  <input type="radio" id="private" name="privacy" value="0">
  <label for="private">Private</label>

  <input type="radio" id="public" name="privacy" value="1">
  <label for="public">Public</label>

  <input type="submit" value="Save">

</form>

Change your buttons to input elements, which, together with the form, provide a HTML native way to send data to a script without the need for Javascript (although you can use Javascript to enhance the usability later on).

Using the label tag, you can assign a text to a radiobutton. This allows this text to be clicked as well, and it also provides better support for screen readers, because they can actually see which text belongs to the radiobutton.

An id needs to be unique, so I changed it to private and public. The ids are also used to link the label to.

The name determines by which name the value is sent to your script.

In PHP, you can use the superglobal $_GET. $_GET['privacy'] will contain '0' or '1' depending on the choice made. If the method of the form was post, you could use the superglobal $_POST instead, or you can use $_REQUEST, which contains values from either, so in that case your script doesn't care whether the values were send in the url (get) or in the chunk of invisible post data that is sent along with the request.

Upvotes: 4

Chathuranga Tennakoon
Chathuranga Tennakoon

Reputation: 2179

i have assumes that your form is something as follows.

    <form name="sample" action="form_submit.php" method="POST">
    <div class="btn-group" data-toggle="buttons-radio">
    <input type="button" class="btn btn-primary" id="private" name="privacy"   value="0">Private</input>
    <label for="private">Private</label>
        <input type="button" class="btn btn-primary" id="public" name="privacy"   value="1">Public</input>
    <label for="public">Public</label>
    </div>
    <input type="submit" name ="submit" value ="Submit"/>
    </form>

you can use the following code inside the action (form_submit.php) to get the selected radio button value.

form_submit.php

<?php

$selected_radio = $_POST['privacy'];
print $selected_radio;

?>

make sure that you are not duplicating the IDs because they should be unique.

Upvotes: 0

zavg
zavg

Reputation: 11061

You don't have form and submit button.

HTML page:

<div class="btn-group" data-toggle="buttons-radio ">
  <form name="privacyForm" action="action.php" method="post">
    <button type="radio" class="btn btn-primary" name="privacy" value="0">Private</button>
    <button type="radio" class="btn btn-primary" name="privacy" value="1">Public</button>
    <input type="submit" value="Submit">
  </form>
</div>

action.php

<?php
  echo $_POST['privacy']; // should be 0 or 1
?>

Upvotes: 0

exussum
exussum

Reputation: 18550

assuming its posted on $_POST['privacy'] should contain either a 1 or a 0

also dont use an ID twice - they are supposed to be unique

Upvotes: 0

M Khalid Junaid
M Khalid Junaid

Reputation: 64466

change type to radio and donot use the same id for morethan one element id is supposed to be unique

Also when you post the form type=button values will not be passed with the form

<div class="btn-group" data-toggle="buttons-radio">
<input type="radio" class="btn btn-primary" name="privacy"   value="0"/>Private
<input type="radio" class="btn btn-primary" name="privacy"   value="1"/>Public
</div>

Upvotes: 0

Related Questions