473,467 Members | 1,430 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

store data from either of two controls in single column of databese

2 New Member
I have select box in that two options like static value and custom value if user selects custom value a textbox will appear and if it is static value a combo box will appear so that from both option based on selection the value should store into database please help to code this problem correctly

Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1">
  7.  
  8. <title>Example of Bootstrap 3 Supported Form Controls</title>
  9. <script type="text/javascript" src="js/script.js"></script> 
  10.         <meta name="viewport" content="width=device-width, initial-scale=1">
  11.         <meta name="viewport" content="width=device-width, initial-scale=1">
  12.         <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
  13.         <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
  14.         <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
  15.         <link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/> 
  16.         <link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.1/css/select2.min.css" rel="stylesheet"/> 
  17.         <script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.1/js/select2.min.js"></script> 
  18.         <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
  19. <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">
  20. <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
  21. <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
  22. <style type="text/css">
  23.     h1{
  24.         margin: 30px 0;
  25.         padding: 0 200px 15px 0;
  26.         border-bottom: 1px solid #E5E5E5;
  27.     }
  28.     .bs-example{
  29.         margin: 20px;
  30.     }
  31. </style>
  32. </head>
  33. <body>
  34. <div class="bs-example">
  35.     <form class="form-horizontal"  role="form" method="post" action="dynform.php">
  36.         <div class="form-group">
  37.             <label class="control-label col-xs-3" for="firstName">Enter Field Name:</label>
  38.             <div class="col-xs-9">
  39.                <input id="new-state" type="text" placeholder="Field Name " class="form-control"/>
  40.                <br>
  41.  
  42.                 <button type="button" id="btn-add-state" class="btn-primary">ADD</button>
  43.             </div>
  44.         </div>
  45.         <div class="form-group">
  46.             <label class="control-label col-xs-3" for="lastName">Select Field Name:</label>
  47.             <div class="col-xs-9">
  48.                <select id="state"  type="text"  class="form-control" name="column" required="" >
  49.   <option value="">select</option>
  50.   <option value="col1">col1</option>
  51.   <option value="col2">col2</option>
  52.   </select>
  53.             </div>
  54.         </div>
  55.         <div class="form-group">
  56.             <label class="control-label col-xs-3" for="inputEmail">Enter Field type:</label>
  57.             <div class="col-xs-9">
  58.                 <select id="type1" class="form-control" type="text"  name="type" required="">
  59.                                 <option value="">select</option>
  60.                                 <option value="INT">INT</option>
  61.                                 <option value="BOOLEAN">BOOLEAN</option>
  62.                                 <option value="DATE">DATE</option>
  63.                                 <option value="BLOB">BLOB</option>
  64.                                 <option value="CHAR">CHAR</option>
  65.                                 <option value="VARCHAR">VARCHAR</option>
  66.                             </select>
  67.             </div>
  68.         </div>
  69.         <div class="form-group">
  70.             <label class="control-label col-xs-3" for="phoneNumber">How Do You Pass Values:</label>
  71.             <div class="col-xs-9">
  72.                 <select id="type2" class="form-control" type="text"  required="" onselect="select();">
  73.                         <option >select</option>
  74.                         <option   id="chkYes">Custom Values</option>
  75.                         <option   id="chkNo"> Static Values</option>
  76.                     </select>
  77.             </div>
  78.         </div>
  79.         <div class="form-group">
  80.  
  81.         <div class="col-xs-6">
  82.         <span>
  83.         <div id="dPassport" style="display: none">
  84.              <label class="control-label col-xs-3" >Static Values:</label>
  85.              <div class="col-xs-9">
  86.             <select id="value" class="form-control" type="text"   name="value1" required="" align="left">
  87.                 <option value="">select</option>
  88.                 <option value="state">State</option>
  89.                 <option value="District">District</option>
  90.                 <option value="Gender">Gender</option>
  91.                 <option value="B-group">B-group</option>
  92.             </select>
  93.             </div>
  94.             </div>
  95.  
  96.         <div id="dvPassport" style="display: none">
  97.             <label class="control-label col-xs-3" >Custom Values:</label>
  98.             <div class="col-xs-9">
  99.             <input id="value" type="text" placeholder=" Enter Value " style="width:100%" name="value2" required="" class="form-control"/>
  100.             </div>
  101.         </div>
  102.         <span>
  103.         </div>
  104.          </div>
  105.  
  106.         <br>
  107.         <div class="form-group">
  108.             <div class="col-xs-offset-3 col-xs-9">
  109.                 <input type="submit" value="SUBMIT" class="btn btn-success"  name="submit" />
  110.                <input id="remove" type="button" value="Done"  onClick="addRow('dataTable')">
  111.             </div>
  112.         </div>
  113.     </form>
  114. </div>
  115. <script type="text/javascript">
  116.     $(document).ready(function () {
  117.         function select() {
  118.             if ($('#type2').find(":selected").attr('id') == 'chkYes') {
  119.                 $("#dvPassport").show();
  120.                 $("#dPassport").hide();
  121.             } else {
  122.                 $("#dPassport").show();
  123.                 $("#dvPassport").hide();
  124.             }
  125.         }
  126.         //this makes the select change call the function select():
  127.         // anytime the user change the selection the function select() is called and works as expected
  128.         $('#type2').on('change', function () {
  129.             select();
  130.         });
  131.  
  132.         $("#state").select2({
  133.             tags: true
  134.         });
  135.  
  136.         $("#btn-add-state").on("click", function () {
  137.             var newStateVal = $("#new-state").val();
  138.             // Set the value, creating a new option if necessary
  139.             if ($("#state").find("option[value='" + newStateVal + "']").length) {
  140.                 $("#state").val(newStateVal).trigger("change");
  141.             } else {
  142.                 // Create the DOM option that is pre-selected by default
  143.                 var newState = new Option(newStateVal, newStateVal, true, true);
  144.                 // Append it to the select
  145.                 $("#state").append(newState).trigger('change');
  146.             }
  147.         });
  148.         $("#type").select2({
  149.             tags: true
  150.         });
  151.  
  152.  
  153.     });//end jQuery
  154.     </script>
  155. </body>
  156. </html>  
  157. <?php
  158. $link = mysqli_connect("localhost","root","")  or die("failed to connect to server !!");
  159. mysqli_select_db($link,"idcard");
  160. if(isset($_REQUEST['submit']))
  161. {
  162. $errorMessage = "";
  163. $column=$_POST['column'];
  164. $type=$_POST['type'];
  165. $value= $_POST['value1'] or $_POST['value2'];
  166.  
  167.  
  168. // Validation will be added here
  169.  
  170. if ($errorMessage != "" ) {
  171. echo "<p class='message'>" .$errorMessage. "</p>" ;
  172. }
  173. else{
  174. //Inserting record in table using INSERT query
  175. $ins="INSERT INTO `idcard`.`combo1`
  176. (`column`, `type`, `value`) VALUES ('$column', '$type', 
  177. '$value')";
  178. $test=mysqli_query($link,$ins) or die(mysqli_error($link));
  179. if($test)
  180. echo "<script type='text/javascript'>alert('submitted successfully!')</script>";
  181. else
  182.  echo "<script type='text/javascript'>alert('failed!')</script>";
  183. }
  184. echo "<script type='text/javascript'>window.location.href='zzz.php'</script>";
  185. exit();
  186. }
  187. $sql = "SELECT * FROM combo1 group by id ";
  188. $result = mysqli_query($link, $sql);
  189. if ($result && mysqli_num_rows($result) > 0) {
  190.  
  191.       # code...
  192.       // while($row = $result->fetch_assoc()) {
  193.  echo "<table class='table-hover table borderless table-condensed ';style='background-color: red'>";
  194.  echo "<thead>";
  195.             echo "<tr  align=center>";
  196.                 echo "<th >Column</th>";
  197.                 echo "<th >Type</th>";
  198.                 echo "<th >Value</th>";
  199.                 echo "<th >Action</th>";
  200.             echo "</tr>";
  201.            echo"</thead>";
  202.        while($row = mysqli_fetch_array($result)){
  203.         $id=$row['id'];
  204.        echo "<tbody>";
  205.             echo "<tr >";
  206.                 echo "<td >" . $row['column'] . "</td>";
  207.                 echo "<td >" . $row['type'] . "</td>";
  208.                 echo "<td >" . $row['value'] . "</td>";
  209.                 echo "<td><a href='edit.php?id=" . $row['id'] ."' class='btn btn-success'>Edit</a>
  210.                 <a href='delete.php?id= $id'  data-toggle='modal'  class='btn btn-danger' >Delete </a>
  211.  
  212.                 </td>";
  213.                echo "</tr>";
  214.                 echo "</tbody>";
  215.               } 
  216.  
  217.            echo "</table>";
  218.  
  219.  
  220.     }
  221.  
  222. mysqli_free_result($result);
  223.  
  224. ?>
  225.  
Apr 3 '17 #1
0 1052

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Lars Behrens | last post by:
Hi there! For a web project I need a little expert help. I don't have written much code yet, just been fiddling around a bit, testing and planning. The web site will have a submission page for...
3
by: Forconi Boris via .NET 247 | last post by:
Hi, I'm working on a project in witch I have to list data(products that can be selected with a certain quantity) from anXML document, with key words entered by the user. A Table isdynamically created...
5
by: yma | last post by:
Hello, I tried to display a column in MS Access 2000 nwind.mdb using 3 data controls. But I got "It is already opened exclusively by another user, or you need permission to view its data." I...
0
by: sags | last post by:
Hi , Iam a new uesr of this group. I have a small doubt . Iam using the code mentioned below to retrive the data from tab delimited text file ., but Iam getting all the columns of the text file...
2
by: Anonieko | last post by:
Hello ASPNET guru's, What is a clean way to go around the problem of displaying a GridView templated column where data can contain Single Quote ( ' )? I maybe too naive, but this is of course...
3
by: itarunachalam | last post by:
Hi friends, I have a table structure like this in SQL sever 2000. i want a stored procedure to transfer the data from "tblSurvey" table to "tblRespondent" table. Table - tblSurvey...
4
by: luciegiles | last post by:
Hi, I wish to transpose data in an Access table currently in a single column over many rows to many columns over single row: Current format: CHI DateIssued 123456 01/01/10...
1
by: haripadadas | last post by:
I have 6 checkbox in my entryform.php. I am supposed to check more than 1 checkbox at a time. How to store multiple value in single column of database? Latter I also retrieve value from database...
1
by: parameswari | last post by:
Hi Friends, I have a problem in creating a table with a column that has two foreign Key relationship . Let me explain in detail. Say ,I have three tables. First table A which...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.