troubleshooting Question

data is not inserting into database properly from two form fields(both having same name )into single column

Avatar of shruti A
shruti A asked on
PHPBootstrapJavaScript
10 Comments4 Solutions148 ViewsLast Modified:
<html>
<head>
	<meta charset="utf-8">
		<meta name="viewport" content="width=device-width, initial-scale=1">
		<meta name="viewport" content="width=device-width, initial-scale=1">
		<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
	<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script> 
		<!-- <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script> -->
		<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet"/> 
		<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.1/css/select2.min.css" rel="stylesheet"/> 
		<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.1/js/select2.min.js"></script> 
</head>
	<body>
	<form class="form-horizontal"  role="form" method="post" action="">
		<input id="new-state" type="text" placeholder="Field Name " class="form-control" />
		 <div class="form-group">
            <label class="control-label col-xs-3" for="phoneNumber">How Do You Pass Values:</label>
            <div class="col-xs-9">
                <select id="type2" class="form-control" type="text"  required="" onselect="select();">
                        <option >select</option>
                        <option   id="chkYes">Custom Values</option>
                        <option   id="chkNo"> Static Values</option>
                    </select>
            </div>
        </div>
        <div class="form-group">

        <div class="col-xs-6">
        <span>
        <div id="dPassport" style="display: none">
             <label class="control-label col-xs-3" >Static Values:</label>
             <div class="col-xs-9">
            <select id="vvv" class="form-control" type="text"  name="value"  align="left">
                <option >select</option>
                <option value="state">State</option>
                <option value="District">District</option>
                <option value="Gender">Gender</option>
                <option value="B-group">B-group</option>
            </select>
            </div>
            </div>
           
        <div id="dvPassport" style="display: none">
            <label class="control-label col-xs-3" >Custom Values:</label>
            <div class="col-xs-9">
            <input id="ggg" type="text" placeholder=" Enter Value " name="value" style="width:100%" />
            </div>
        </div>
        <span>
        </div>
         </div>
       
        <br>
        <div class="form-group">
            <div class="col-xs-offset-3 col-xs-9">
                <input type="submit" value="SUBMIT" class="btn btn-success"  name="submit"  onclick="msg()" />
                <input type="reset" class="btn btn-default" value="Reset">
  </form>
	</body>
	<script>
	var x,y;
function msg(){
//var v=document.getElementsByName("value2");
//x=document.getElementById("ggg").value;
if(document.getElementById("ggg").value!=""){
 //alert("Your name is:"+v[0].value);
 //alert("Your name is:"+x);
value=document.getElementById("ggg").value;
alert("Your name is:"+value);
}
else
{
 value=document.getElementById("vvv").value;
alert("You are selected:"+value);
}
}
</script>
	<script type="text/javascript">


			//x="ok";

	$(document).ready(function () {
		function select() {
			
			if ($('#type2').find(":selected").attr('id') == 'chkYes') {
				$("#dvPassport").show();
				$("#dPassport").hide();
			} else {
				$("#dPassport").show();
				$("#dvPassport").hide();
			}
		}
		//this makes the select change call the function select():
		// anytime the user change the selection the function select() is called and works as expected
		$('#type2').on('change', function () {
			select();
		});

		$("#state").select2({
			tags: true
		});

		$("#btn-add-state").on("click", function () {
			var newStateVal = $("#new-state").val();
			// Set the value, creating a new option if necessary
			if ($("#state").find("option[value='" + newStateVal + "']").length) {
				$("#state").val(newStateVal).trigger("change");
			} else {
				// Create the DOM option that is pre-selected by default
				var newState = new Option(newStateVal, newStateVal, true, true);
				// Append it to the select
				$("#state").append(newState).trigger('change');
			}
		});
		$("#type").select2({
			tags: true
		});
//x=document.getElementById('value2').value;


	});//end jQuery
	</script>
	

	
</html>
<?php
$link = mysqli_connect("localhost","root","")  or die("failed to connect to server !!");
mysqli_select_db($link,"idcard");
if(isset($_REQUEST['submit']))
{
$errorMessage = "";

$value=$_POST['value'];

 
// Validation will be added here
 
if ($errorMessage != "" ) {
echo "<p class='message'>" .$errorMessage. "</p>" ;
}
if( isset($_POST['value'])) {
//Inserting record in table using INSERT query
$ins="INSERT INTO `idcard`.`select`
( `value`) VALUES ('$value')";
$test=mysqli_query($link,$ins) or die(mysqli_error($link));
if($test)
echo "<script type='text/javascript'>alert('submitted successfully!')</script>";
else
 echo "<script type='text/javascript'>alert('failed!')</script>";
}
echo "<script type='text/javascript'>window.location.href='ex.php'</script>";
exit();
}
SOLUTION
Join our community to see this answer!
Unlock 4 Answers and 10 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 4 Answers and 10 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros