Jump to content

insert the checkbox value from php form to mysql


rahul19dj

Recommended Posts

I have this registration form which has 4 check boxes. I would like to report the selection ( 1 or more ) and insert their value as 1 in my database under the preferred selection. The below is my html code

 

<tr>

          <td>SMS Centers *</td>

          <td colspan="2"><table width="210" border="0" class="greennbold">

            <tr>

              <td width="22" align="center"><input name="checkbox[]" type="checkbox" class="tickbox" id="mumbai" value="1"></td>

              <td width="188">MUMBAI</td>

            </tr>

            <tr>

              <td align="center"><input name="checkbox[]" type="checkbox" class="tickbox" id="pune" value="1"></td>

              <td>PUNE</td>

            </tr>

            <tr>

              <td align="center"><input name="checkbox[]" type="checkbox" class="tickbox" id="banglore" value="1"></td>

              <td>BANGALORE</td>

            </tr>

            <tr>

              <td align="center"><input name="checkbox[]" type="checkbox" class="tickbox" id="mysore" value="1"></td>

              <td>MYSORE (August)</td>

            </tr>

          </table></td>

          </tr>

        <tr>

 

Below will be my php code.

 

<?php

  include ('database_connection.php');

  $mumbai=isset($_POST['checkbox']) ? 1 : 0;

$pune=isset($_POST['checkbox']) ? 1 : 0;

$banglore=isset($_POST['checkbox']) ? 1 : 0;

$mysore=isset($_POST['checkbox']) ? 1 : 0;

 

  if (isset($_POST['formsubmitted'])) {

  $error = array();//Declare An Array to store any error message 

  if (empty($_POST['mobileno'])) {//if no name has been supplied

    $error[] = 'Please Enter a Mobile Number ';//add to array "error"

  } else {

    $mobileno = $_POST['mobileno'];//else assign it a variable

  }

  if (empty($_POST['fname'])) {//if no name has been supplied

    $error[] = 'Please Enter a First name ';//add to array "error"

  } else {

    $fname = $_POST['fname'];//else assign it a variable

  }

if (empty($_POST['lname'])) {//if no name has been supplied

    $error[] = 'Please Enter a Last name ';//add to array "error"

  } else {

    $lname = $_POST['lname'];//else assign it a variable

  }

  if (empty($_POST['email'])) {

    $error[] = 'Please Enter your Email ';

  } else {

 

 

    if (preg_match("/^([a-zA-Z0-9])+([a-zA-Z0-9\._-])*@([a-zA-Z0-9_-])+([a-zA- Z0-9\._-]+)+$/", $_POST['email'])) {

      //regular expression for email validation

        $email = $_POST['email'];

    } else {

        $error[] = 'Your EMail Address is invalid  ';

    }

 

 

    }

 

 

  if (empty($_POST['passwd1'])) {

    $error[] = 'Please Enter Your Password ';

  } else {

    $passwd1 = $_POST['passwd1'];

  }

  if (empty($_POST['passwd2'])) {

    $error[] = 'Please Verify Your Password ';

  } else {

    $passwd2 = $_POST['passwd2'];

  }

 

  if (empty($error)) //send to Database if there's no error '

 

  { //If everything's OK...

 

    // Make sure the mobile no is available:

    $query_verify_mobileno = "SELECT * FROM userdtls WHERE mobileno = '$mobileno'";

    $result_verify_mobileno = mysqli_query($dbc, $query_verify_mobileno);

    if (!$result_verify_mobileno)

    {//if the Query Failed ,similar to if($result_verify_mobileno==false)

        echo ' Database Error Occured ';

    }

 

    if (mysqli_num_rows($result_verify_mobileno) == 0) { // IF no previous user is using this number .

 

 

        // Create a unique  activation code:

      //$activation = md5(uniqid(rand(), true));

 

 

        $query_insert_user = "INSERT INTO userdtls ( `mobileno`, `pass`, `fname`,    `lname`, `email`, `MUM`, `PUN`, `BNG`, `MYS` ) VALUES ( '".$mobileno."', '".$passwd1."', '".$fname."', '".$lname."', '".$email."', '".$mumbai."', '".$pune."', '".$banglore."', '".$mysore."'  )";

 

 

        $result_insert_user = mysqli_query($dbc, $query_insert_user);

        if (!$result_insert_user) {

            echo 'Query Failed ';

        }

 

        if (mysqli_affected_rows($dbc) == 1) { //If the Insert Query was successfull.

  // Finish the page:

            echo '<div class="success">Thank you for registering! </div>';

 

 

        } else { // If it did not run OK.

            echo '<div class="errormsgbox">You could not be registered due to a system  error. We apologize for any inconvenience.</div>';

        }

 

    } else { // The mobile number is not available.

        echo '<div class="errormsgbox" >That mobile number has already been registered.  </div>';

    }

 

  } else {//If the "error" array contains error msg , display them

 

 

 

  echo '<div class="errormsgbox"> <ol>';

    foreach ($error as $key => $values) {

 

        echo '  <li>'.$values.'</li>';

 

 

 

    }

    echo '</ol></div>';

 

  }

 

  mysqli_close($dbc);//Close the DB Connection

 

  } // End of the main Submit conditional.

 

Answered in your other post.

http://forums.phpfreaks.com/index.php?topic=363369.msg1719607#msg1719607

 

if (isset($_POST['checkbox'])){
$mumbai=(in_array("mumbai",$_POST['checkbox']) ? 1 : 0);
$pune=(in_array("pune",$_POST['checkbox']) ? 1 : 0);
$banglore=(in_array("banglore",$_POST['checkbox']) ? 1 : 0);
$mysore=(in_array(1,$_POST['checkbox']) ? 1 : 0);	
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.