附加单选按钮

时间:2016-08-15 16:54:06

标签: javascript php jquery html mysqli

如何使用jquery或javascript附加单选按钮以保存到数据库?

我添加了输入类型,如text,textarea,checkbox,select with options。

我的代码是

 <!DOCTYPE html>
<html lang="en">
<head>
<title>
apnd
</title>
<script src="jquery.min.js" type="text/javascript"></script>
<script>
$(document).ready(function(){
var htmldata='<div>Name<input type="text" name="cname[]">Mobile Number<input type="text"name="mob[]">Gender<input type="radio" name="gender[]" value="M">Male<input type="radio" name="gender[]" value="F">FemaleAddress<textarea name="address[]"></textarea><input type="button" class="remove_field" value="remove"></div>';
$('.add_field').click(function(){
$('.wrapper').append(htmldata); 
});
$('.wrapper').on('click','.remove_field',function(){
$(this).parent('div').remove(); 
});
});
</script>
</head>
<body>
<div class="container">
<form method="post" action="">
<div class="wrapper">
<div>
    Name<input type="text" name="cname[]">
    Mobile Number<input type="text" name="mob[]">
    Gender<input type="radio" name="gender[]" value="M">Male
          <input type="radio" name="gender[]" value="F">Female
    Address<textarea name="address[]"></textarea>
    <input type="button" class="add_field" value="Add">
</div>
</div>
<input type="submit" name="sub" value="Save">
</form>
</body>
</html>

使用php我想将值保存到数据库。

$name=sc_sql_injection($_POST['cname']);
        $mobile=sc_sql_injection($_POST['mob']);
        $gender=sc_sql_injection($_POST['gender']);
        $address=sc_sql_injection($_POST['address']);
        $count=count($_POST['cname']);
        for($i=0;$i<$count;$i++)
        {
            $sql="insert into t2(name,mobile,gender,address) values('$name[$i]','$mobile[$i]','$gender[$i]','$address[$i]')";
            sc_exec_sql($sql,$con);
   }

更新 enter image description here

1 个答案:

答案 0 :(得分:0)

附加单选按钮

<!DOCTYPE html>
    <html lang="en">
    <head>
    <title>
    apnd
    </title>
    <script src="jquery.min.js" type="text/javascript"></script>
    <script>
    $(document).ready(function(){
            var i=1;
    $('.add_field').click(function(){
    var htmldata='<div>Name<input type="text" name="cname[]">Mobile Number<input type="text"name="mob[]"> Gender<input type="radio" name="gender['+i+']" value="M">Male<input type="radio" name="gender['+i+']" value="F">Female Address<textarea name="address[]"></textarea><input type="button" class="remove_field" value="remove"></div>';
            i++;
    $('.wrapper').append(htmldata); 
    });
    $('.wrapper').on('click','.remove_field',function(){
    $(this).parent('div').remove(); 
    i--;
    });
    });
    </script>
    </head>
    <body>
    <div class="container">
    <form method="post" action="">
    <div class="wrapper">
    <div>
        Name<input type="text" name="cname[]">
        Mobile Number<input type="text" name="mob[]">
        Gender<input type="radio" name="gender[0]" value="M">Male
              <input type="radio" name="gender[0]" value="F">Female
        Address<textarea name="address[]"></textarea>
        <input type="button" class="add_field" value="Add">
    </div>
    </div>
    <input type="submit" name="sub" value="Save">
    </form>
    </body>
    </html>

&#13;
&#13;
$(document).ready(function(){
		var i=1;
$('.add_field').click(function(){
var htmldata='<div>Name<input type="text" name="cname[]">Mobile Number<input type="text"name="mob[]"> Gender<input type="radio" name="gender['+i+']" value="M">Male<input type="radio" name="gender['+i+']" value="F">Female Address<textarea name="address[]"></textarea><input type="button" class="remove_field" value="remove"></div>';
		i++;
$('.wrapper').append(htmldata);	
});
$('.wrapper').on('click','.remove_field',function(){
$(this).parent('div').remove();	
i--;
});
});
&#13;
<html>
 <head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
   </head>
<body>
<div class="container">
<form method="post" action="">
<div class="wrapper">
<div>
	Name<input type="text" name="cname[]">
	Mobile Number<input type="text" name="mob[]">
	Gender<input type="radio" name="gender[0]" value="M">Male
	      <input type="radio" name="gender[0]" value="F">Female
	Address<textarea name="address[]"></textarea>
	<input type="button" class="add_field" value="Add">
</div>
</div>
<input type="submit" name="sub" value="Save">
</form>
</body>
  </html>
&#13;
&#13;
&#13;