Greetings,
I need a help on how to insert data from textbox when the checkbox checked. In my code there are few checkboxes where one checkbox is with textfield. Its means when i checked the "other" checkbox it will allow user to type. But the the problem here now is its store the value as "other" in database. I want it same what user type in that textfield into database. And one more is when the data is added we need to send an email notification to admin to notify the addition. Can anyone help me on this problem? Here my code for it. Please help me. Thank you.

<?php 
$host="localhost"; // Host name 
$username="root"; // Mysql username 
$password=""; // Mysql password 
$db_name="pet"; // Database name 
$tbl_name="user"; // Table name 

// Connect to server and select database. 
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB"); 

// Get values from form 
if(isset($_POST['submit'])) {
$FullName=$_POST['FullName'];  
$MobileNumber=$_POST['MobileNumber'];  
$EmailAddress=$_POST['EmailAddress'];  
$Address=$_POST['Address'];  
$petathome= implode(',', $_POST['petathome']);

// Loop through the array of checked box values ... 

// Insert data into mysql 
$sql="INSERT INTO user(FullName, MobileNumber, EmailAddress, Address, petathome)VALUES('$FullName', '$MobileNumber', '$EmailAddress', '$Address', '$petathome')";  
$result=mysql_query($sql); 

// if successfully insert data into database, displays message "Successful". 
if($result){ 
echo "Successful"; 
echo "<BR>"; 
echo "<a href=''>Back to main page</a>"; 
} 

else { 
echo "ERROR"; 
} 
}
?> 

<?php 
// close connection 
mysql_close(); 
?>
<body>
<form action="pet.php" method="post">

Full Name: <input name="FullName" type="text" id="FullName" required="required" /><br />
<br />
Number: <input name="MobileNumber" type="text" id="MobileNumber" required="required" /><br />
<br />
Email Address: <input name="EmailAddress" type="text" id="EmailAddress" required="required" /><br /><br />
<br/>
Address: <input name="Address" type="text" id="Address" required="required" /><br />
<br />
Which animal do you want to keep?<br />
<script type="text/javascript">
var currentEnabled = null;
function enableElement(elem) {
if (currentEnabled) {
currentEnabled.disabled = true;
}
elem.disabled = false;
currentEnabled = elem;
}
</script>
<form action="pet.php">
<input type="checkbox" name="petathome[]" value="Dog"
onclick="enableElement(this.form.elements['inp1']);"> Dog:
<br>
<input type="checkbox" name="petathome[]" value="Cat"
onclick="enableElement(this.form.elements['inp2']);"> Cat
<br>
<input type="checkbox" name="petathome[]" value="Lion"
onclick="enableElement(this.form.elements['inp3']);"> Lion
<br>
<input type="checkbox" name="petathome[]" value="Tiger"
onclick="enableElement(this.form.elements['inp4']);"> Tiger
<br>
<input type="checkbox" name="petathome[]" value="Leopard"
onclick="enableElement(this.form.elements['inp5']);"> Leopard
<br>
<input type="checkbox" name="petathome[]" value="Others"
onclick="enableElement(this.form.elements['inp6']);">
Others: <input type="text" name="inp6" disabled="disabled">

<br>
<br />

<input type="submit" name="submit" value="Submit" />

</form>

</body>
</html>

And this one is my database.

-- phpMyAdmin SQL Dump
-- version 3.4.5
-- http://www.phpmyadmin.net
--
-- Host: localhost
-- Generation Time: Nov 26, 2014 at 04:38 AM
-- Server version: 5.5.16
-- PHP Version: 5.3.8

SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;

--
-- Database: `pet`
--

-- --------------------------------------------------------

--
-- Table structure for table `user`
--

CREATE TABLE IF NOT EXISTS `user` (
  `Id` int(11) NOT NULL AUTO_INCREMENT,
  `FullName` varchar(255) NOT NULL,
  `MobileNumber` varchar(255) NOT NULL,
  `EmailAddress` varchar(255) NOT NULL,
  `Address` varchar(255) NOT NULL,
  `petathome` varchar(255) NOT NULL,
  PRIMARY KEY (`Id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;

--
-- Dumping data for table `user`
--

INSERT INTO `user` (`Id`, `FullName`, `MobileNumber`, `EmailAddress`, `Address`, `petathome`) VALUES
(1, 'hashani', '738189332', 'hdhlsd', 'dhdljldd', ''),
(2, 'hssgsg', '', 'shdhhds', 'huksh', 'Dog,Others'),
(3, 'hass', '527812', 'hasbjs', 'dddsssa', 'Dog,Others'),
(4, 'jjjghkl', '66679097632', 'jghvhjhgg', 'lokgff', 'Dog,Others');

/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
Member Avatar for diafol

Ok these are 2 different questions. It would be best if you separated these requests. I will try to address the first as this is the premise described by your title.

You simply need to check for the checkbox name existing in the $_POST superglobal array and check asgainst a non-zero length string in the trimmed $_POST var for the "other" input box.

However, a bit confused. Your table structure is not normalized. Anyway.

<input type="checkbox" name="petathome[]" value="Others"
onclick="enableElement(this.form.elements['inp6']);">
Others: <input type="text" name="inp6" disabled="disabled">

Change this to... (note change of name for checkbox)

<input type="checkbox" name="others" value="Others"
onclick="enableElement(this.form.elements['inp6']);">
Others: <input type="text" name="inp6" disabled="disabled">

The php handling code...

$pets = (array) $_POST['petsathome']; //casting not strictly required here
if(isset($_POST['others']) && trim($_POST['inp6'])) $pets[] = $_POST['inp6'];

$petsathome = implode(", ", $pets);

Not tested - off top of head.

NB. As your data is not normalized, difficult to build an edit form if ever you need to do this.

Thank you for your reply, diafol. I managed to fix it. :)
But i need to send notification to the admin once the data inserted into database. Is there any solutions for this?

Member Avatar for diafol

THis is a separate issue and should be in its own thread as the title does not suggest that this topic is discussed here. Before you do that, please search Daniweb and other sites as there are loads of examples out there.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.