如何在数据库中创建和存储表单值

时间:2016-10-04 10:45:09

标签: php html mysql database forms

我创建了带有选择选项的表单。根据选择我显示和隐藏表单值,我不知道将其保存在数据库中。在我的代码中有选择选项我再次显示表单字段。例如,如果我选择选项是3我将显示表单字段3次。

我想将数据存储在后端(数据库)中。如何存储数据库。

我的代码



$(document).ready(function() {
  $('#hidden-div').hide();
  $("#select_btn").change(function() {
    toggleFields();
  });

});

function toggleFields() {
  var selectVal = $("#select_btn").val();
  if (selectVal <= 5) {
    $hiddenHtml = $('#hidden-div').clone().html();
    $("#refer").html('');
    for (var i = 0; i < selectVal; i++) {
      $("#refer").append($hiddenHtml);
    }
  }
}
$( "form" ).submit(function( event ) {
  console.log( $( this ).serializeArray() );
  event.preventDefault();
});
&#13;
<html>
<head>
<title> Demo </title>
<meta name="robots" content="noindex, nofollow" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<form name="referer" method="post" action="">
  <p>Name:
    <input type="text" name="referer_name" />
  </p>
  <p>Mobile:
    <input type="text" name="referer_mobile" />
  </p>
  <p>Email:
    <input type="text" name="referer_email" />
  </p>
  <p>No of Referrer:
    <select id="select_btn" >
      <option value="0">--Select--</option>
      <option value="1">1</option>
      <option value="2">2</option>
      <option value="3">3</option>
      <option value="4">4</option>
      <option value="5">5</option>
    </select>
    <div id="hidden-div">
 <div id="text">Referral Details</div>
      <p>Name:
        <input type="text" name="name[]" />
      </p>
      <p>Mobile:
        <input type="text" name="mobile[]" />
      </p>
      <p>Email:
        <input type="text" name="email[]" />
      </p>
      
    </div>
    <div id="refer">

    </div>
    <p align="center">
      <input type="submit" value="Submit" />
    </p>
</form>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

&#13;
&#13;
<html>
<head>
<title> Demo </title>
<meta name="robots" content="noindex, nofollow" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<form id ="refer-form" name="refer-form" action="validate.php"  method="post" >
 
  <p>No of Referrer:
    <select id="select_btn" >
      <option value="0">--Select--</option>
      <option value="1">1</option>
      <option value="2">2</option>
      <option value="3">3</option>
      <option value="4">4</option>
      <option value="5">5</option>
    </select>
    <div id="hidden-div">
 <div id="text">Referrer</div>
      <p>Name:
        <input type="text" name="name[]" />
      </p>
      <p>Mobile:
        <input type="text" name="mobile[]" />
      </p>
      <p>Email:
        <input type="text" name="email[]" />
      </p>
      
    </div>
    <div id="refer">

    </div>
    <p align="center">
      <input type="submit" value="Submit" />
    </p>
</form>
<script type="text/javascript">
$(document).ready(function() {
  $('#hidden-div').hide();
  $("#select_btn").change(function() {
    toggleFields();
  });

});

function toggleFields() {
  var selectVal = $("#select_btn").val();
  if (selectVal <= 5) {
    $hiddenHtml = $('#hidden-div').clone().html();
    $("#refer").html('');
    for (var i = 0; i < selectVal; i++) {
      $("#refer").append($hiddenHtml);
    }
  }
}
/*$( "form" ).submit(function( event ) {
  console.log( $( this ).serializeArray() );
  event.preventDefault();
});*/

</script>


</head>
  <?php
for($i = 0; $i < count($name); ++$i) {
$link = mysqli_connect("localhost", "root", "", "test");
if($link === false){
	    die("ERROR: Could not connect. " . mysqli_connect_error());
	}
	// Attempt insert query execution
	$sql = "INSERT INTO test1 (name,email,mobile) VALUES ('$name[0]','$email[$i]', '$mobile[$i]')";
	echo "$sql";
	if(mysqli_query($link, $sql)){
	    echo "Records added successfully.";
	} else{
	    echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
	} 

	// Close connection
	mysqli_close($link);
	}
?>
&#13;
&#13;
&#13;

这里我附上了工作代码以供参考