Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用jQuery和PHP动态添加输入字段并提交到数据库_Php_Mysql - Fatal编程技术网

使用jQuery和PHP动态添加输入字段并提交到数据库

使用jQuery和PHP动态添加输入字段并提交到数据库,php,mysql,Php,Mysql,我想为foreach函数发布多个值,因为我有多个动态文本框,那么我应该如何在数据库中发送这些值?? 如何为dat编写foreach函数。。 下面的代码显示2个多文本框,但未在数据库中提交值 <html> <head> <title></title> <script type="text/javascript" src="jquery-1.9.1.js"></script> <script type="text/java

我想为foreach函数发布多个值,因为我有多个动态文本框,那么我应该如何在数据库中发送这些值?? 如何为dat编写foreach函数。。 下面的代码显示2个多文本框,但未在数据库中提交值

<html>
<head>
<title></title>
<script type="text/javascript" src="jquery-1.9.1.js"></script>
<script type="text/javascript">
var counter = 0;
$(function(){
 $('p#add_field').click(function(){
 counter += 1;
 $('#container').append(
 '<strong>Hobby No. ' + counter + '</strong><br />'
 + '<input id="field_' + counter + '" name="dynfields[]' + '" type="text" /><br />'

+'<strong>HolidayReason ' + counter + '</strong>&nbsp;'
 + '<input id="holidayreason_' + counter + '" name="holireason[]' + '" type="text" />'
  );

 });
});
</script>

<body>

<?php
if (isset($_POST['submit_val'])) {
if (($_POST['dynfields'])&& ($_POST['holireason'])) {
//$aaa=array($_POST['dynfields']);
foreach ($_POST['dynfields'] as $key=>$value) 
{
$values = mysql_real_escape_string($value);
//$holireasons = mysql_real_escape_string($holireason);

$query = mysql_query("INSERT INTO my_hobbies (hobbies) VALUES ('$values')" );


}
}

echo "<i><h2><strong>" . count($_POST['dynfields']) . "</strong> Hobbies Added</h2></i>";

 mysql_close();
}
?>
<?php if (!isset($_POST['submit_val'])) { ?>
 <h1>Add your Hobbies</h1>
 <form method="post" action="">

 <div id="container">
 <p id="add_field"><a href="#"><span>Click To Add Hobbies</span></a></p>
 </div>

 <input type="submit" name="submit_val" value="Submit" />
 </form>
<?php } ?>

</body>
</html>

var计数器=0;
$(函数(){
$('p#添加_字段')。单击(函数(){
计数器+=1;
$(“#容器”)。追加(
“爱好编号”+计数器+”
” +“
” +“度假理由”+柜台+”“ + '' ); }); }); 试试这个

       <html>
            <head>
            <title></title>
           <script src="//code.jquery.com/jquery-1.10.2.js"></script>
            <script type="text/javascript">
            var counter = 0;
            $(function(){
             $('p#add_field').click(function(){
             counter += 1;
             $('#container').append(
             '<strong>Hobby No. ' + counter + '</strong><br />'
             + '<input id="field_' + counter + '" name="dynfields[]' + '" type="text" /><br />'

            +'<strong>HolidayReason ' + counter + '</strong>&nbsp;'
             + '<input id="holidayreason_' + counter + '" name="holireason[]' + '" type="text" />'
              );

             });
            });
            </script>

            <body>

            <?php
            if (isset($_POST['submit_val'])) {
            if (($_POST['dynfields'])&& ($_POST['holireason'])) {
            $no = count($_POST['dynfields']);
           for ($i=0; $i <$no ; $i++) { 
            echo $_POST['dynfields'][$i]."<br>";
            echo $_POST['holireason'][$i]."<br>";
        $abc = mysql_real_escape_string($_POST['dynfields'][$i]);
        $xyz = mysql_real_escape_string($_POST['holireason'][$i]);
       $sql =  "INSERT INTO my_hobbies (hobbies,Holidayreason) VALUES ('$abc','$xyz')";
mysql_query($sql);
        }

            }

            echo "<i><h2><strong>" . count($_POST['dynfields']) . "</strong> Hobbies Added</h2></i>";

             mysql_close();
            }
            ?>
            <?php if (!isset($_POST['submit_val'])) { ?>
             <h1>Add your Hobbies</h1>
             <form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">

             <div id="container">
             <p id="add_field"><a href="#"><span>Click To Add Hobbies</span></a></p>
             </div>

             <input type="submit" name="submit_val" value="Submit" />
             </form>
            <?php } ?>

            </body>
            </html>

var计数器=0;
$(函数(){
$('p#添加_字段')。单击(函数(){
计数器+=1;
$(“#容器”)。追加(
“爱好编号”+计数器+”
” +“
” +“度假理由”+柜台+”“ + '' ); }); });
使用数组_组合,如下所示

foreach(array_combine($_POST['dynfields'] , $_POST['holireason']) as $dyn => $holi) {

   $abc = mysql_real_escape_string($dyn);
   $xyz = mysql_real_escape_string($holi);

   $sql =  mysql_query ("INSERT INTO my_hobbies (hobbies,Holidayreason) VALUES ('".$abc."','".$xyz."')");

}

100%工作正常。

您是否尝试在foreach循环中打印
$values
?否我想在db中添加值作为单行您可以参考:或检查此小提琴:从打印语句中删除注释检查其打印或不打印值不知道如何发送两个值$\u POST['dynfields']和$\u POST['holireason']在foreach Function By print的数据库中,它只给出一个文本框值您的实际要求是什么您想在两个文本框值的一行中插入两列数据Dya我想在两个文本框值的两行中插入数据bt清楚您在做什么以及为什么它适合用户提问每个循环计数,$dyn和$holi take$\u POST['dynfields']值和$_POST['holireason']值,直到循环计数器显示足够。他说。。“否,我想将db中的值作为单行添加”该代码块根据数组的大小将db中的值作为单行插入。