Corey
Corey

Reputation: 327

PHP: how to select a radio button selection and submit it to a database (myphpadmin)

I'm making a contacts page in which you can add contacts to a book, view them, and viewthem in different groupings, etc, etc...

I have a group of radio buttons called "group" and I want to know if i'm using the right code to take the radio button selection and submit it to my "myPhpAdmin" table. I have attempted it as bellow:

<?php
 session_start();session_destroy();
 session_start();

if( $_GET["newFname"] && $_GET["newLname"] && $_GET["newPhone"] && $_GET["newEmail"] && $_GET["newAddress"          ] && $_GET["group"] value (or $_POST) )
    {
    if($_GET["regpass1"]==$_GET["regpass2"])
        {
        $servername="**********";
        $username="********";
        $pass="********";
        $conn=  mysql_connect($servername,$username,$pass)or die(mysql_error());
        mysql_select_db("******",$conn);
        $sql="insert into Contacts (newFname,newLname,newPhone,newAddress,group)                        values('$_GET[newFname]','$_GET[newLname]','$_GET[newPhone]','$_GET[newEmail]','$_GET[newAddress]','$_GET[group] value (or $_POST)')";
        $result=mysql_query($sql,$conn) or die(mysql_error());      
        print "Contact has been added";

        } else print "ERROR: Passwords Don't Match...";

    }else print"ERROR: Invaild Input Data...";

?>

Upvotes: 0

Views: 653

Answers (4)

Tarun Gupta
Tarun Gupta

Reputation: 6403

The simple way is to use JavaScript.

<form id="yourform" method="POST" action="">
     <input id="variable_a" type=text name="variable_a" wrap="virtual" value="<?php echo $_POST["variable_a"]; ?>" size=3 maxlength=10 onClick = "document.getElementById('yourform').submit();">
</form>

OR in PHP:

$v1 = 'unchecked';
$v2 = 'unchecked';

if ( isset( $_POST['Submit'] ) ) {
    $selected_radio = $_POST['variable_a'];
    if ( $selected_radio == 'v1' ) {
       $v1 = 'checked';
    } else if ( $selected_radio == 'v2') {
       $v2 = 'checked';
    }
}


V-1 <Input type = 'Radio' Name ='variable_a' value= '1'
<?PHP echo $v1; ?>

V-2 <Input type = 'Radio' Name ='variable_a' value= '2'
<?PHP echo $v2; ?>

Upvotes: 1

MIIB
MIIB

Reputation: 1849

You can use $_REQUEST that includes both $_GET and $_POST for a variable that has been set whether it is sent by post method or get method. And you should check with javascript which radio is checked.

Upvotes: 0

somesh
somesh

Reputation: 599

use $_POST and in a group of radio button give similar name and id to those and get the values $groupVal = $_POST['Name OR ID']; you will get value no need of implode.

Upvotes: 0

Edwin Alex
Edwin Alex

Reputation: 5108

You should not use $_GET to get all the form values. Use $_POST instead.

If you are passing radio button as an array, you can access like this,

$groupVal = $_POST['group']; // It will pass your selected options as an array.

$group = implode(",", $groupVal); // This will convert your array to string with ',' seperator

Now you can insert $group into your Database.

Upvotes: 0

Related Questions