php:如何使用jquery和$ post []获取动态添加输入的值

时间:2014-12-15 16:44:28

标签: javascript php jquery mysql

请问我的代码有问题,我使用jquery动态添加输入,我想将此输入的值插入mysql数据库,但错误是 “注意:未定义的索引:年份......” “注意:未定义的索引:月份......”

我无法获得添加输入的值。有我的代码,我无法插入数据库:

这是我的表单http://i.stack.imgur.com/qpBN4.png

的图片
<!DOCTYPE html>
<html>
<head>
    <title>Site</title>
    <link href="style.css" rel="stylesheet" type="text/css" />
    <link href="menu.css" rel="stylesheet" type="text/css" />
    <script src="http://code.jquery.com/jquery-latest.js"></script>
<script type='text/javascript'>

//<![CDATA[
$(document).ready(function() {
var currentItem = 1;
$('#addnew').click(function(){
currentItem++;
$('#items').val(currentItem);
var strToAdd = '<tr><td>Year</td><td>:</td><td><select name="year'+currentItem+'" id="year'+currentItem+'" ><option value="2012">2012</option><option value="2011">2011</option></select></td><td>Month</td><td>:</td><td width="17%"><select name="month'+currentItem+'" id="month'+currentItem+'"><option value="1">January</option><option value="2">February</option><option value="3">March</option><option value="4">April</option><option value="5">May</option><option value="6">June</option><option value="7">July</option><option value="8">August</option><option value="9">September</option><option value="10">October</option><option value="11">November</option><option value="12">December</option></select></td><td width="7%">Week</td><td width="3%">:</td><td width="17%"><select name="week'+currentItem+'" id="week'+currentItem+'" ><option value="1">1</option><option value="2">2</option><option value="3">3</option><option value="4">4</option></select></td><td width="8%"></td><td colspan="2"></td></tr><tr><td>Actual</td><td>:</td><td width="17%"><input name="actual'+currentItem+'" id="actual'+currentItem+'" type="text" /></td><td width="7%">Max</td> <td width="3%">:</td><td><input name="max'+currentItem+'" id ="max'+currentItem+'"type="text" /></td><td>Target</td><td>:</td><td><input name="target'+currentItem+'" id="target'+currentItem+'" type="text" /></td></tr>';
  $('#data').append(strToAdd);

 });
 });

 //]]>
 </script>
</head>

    <body class="body">


<div class ="Contentform">
  <form method="POST" action="myform.php" >
<table class="dd" width="100%" id="data">
<tr>
<td>Year</td>
<td>:</td>
<td><select name="year1" id="year1" >
<option value="2012">2012</option>
<option value="2011">2011</option>
</select></td>
<td>Month</td>
<td>:</td>
<td width="17%"><select name="month1" id="month1">
  <option value="1">January</option>
  <option value="2">February</option>
  <option value="3">March</option>
  <option value="4">April</option>
  <option value="5">May</option>
  <option value="6">June</option>
  <option value="7">July</option>
  <option value="8">August</option>
  <option value="9">September</option>
  <option value="10">October</option>
  <option value="11">November</option>
  <option value="12">December</option>
</select></td>
<td width="7%">Week</td>
<td width="3%">:</td>
<td width="17%"><select name="week1" id="week1" >
  <option value="1">1</option>
  <option value="2">2</option>
  <option value="3">3</option>
  <option value="4">4</option>
</select></td>
<td width="8%">&nbsp;</td>
<td colspan="2">&nbsp;</td>
</tr>
<tr>
<td>Actual</td>
<td>:</td>
<td width="17%"><input name="actual1" id="actual1" type="text" /></td>
<td width="7%">Max</td>
<td width="3%">:</td>
<td><input name="max1" id="max1" type="text" /></td>
<td>Target</td>
<td>:</td>
<td><input name="target1" id="target1" type="text" /></td>
<td>
    <input type="button" name="addnew" id="addnew" value="addnew"/>
</td>

</tr>

</table>


                                    <input type="hidden" name="action" value="submit" />
                                    <input type="submit"  height="20" width="20" value="submit values"/>

            </form>
<?php
include 'DBConfig.php';

if(isset($_POST["action"]) and $_POST["action"]=="submit"){

$count=2;
 for( $i = 1; $i <= $count; $i++ )
{
    $year = $_POST['year'.$i];
    $month = $_POST['month'.$i];
    $week = $_POST['week'.$i];
    $actual = $_POST['actual'.$i];
    $max = $_POST['max'.$i];
    $target = $_POST['target'.$i];

    $que = "insert INTO table_name(id,year,month,week,actual,max,target) VALUES ('".$year."','".$month."','".$week."','".$actual."','".$max."','".$target."')";
    mysql_query($que);

}
?>

</div>
</body>
</html>

谢谢,

1 个答案:

答案 0 :(得分:0)

您的值不在表单中。这就是他们提交undefined的原因。

只需将表格包裹在桌子周围,如下所示:

<form method="POST" action="myform.php" >
   <table class="dd" width="100%" id="data">
      <!-- your table content here -->
   </table> 
   <input type="hidden" name="action" value="submit" />
   <input type="submit"  height="20" width="20" value="submit values"/>
</form>

编辑:

这一行应该在表格之前或之后,或者至少在td

<input type="submit" name="addnew" id="addnew" value="addnew"/>

也应该是type="button",而不是type="submit"。否则,它将提交您的表单,而不是添加新元素。

而且你错过了一个}关闭 if(isset()...)

您的$_POST['extreme']$_POST['type']必须未定义,因为这些名称没有输入。

完整代码:

<!DOCTYPE html>
<html>
    <head>
        <title>Site</title>
        <link href="style.css" rel="stylesheet" type="text/css" />
        <link href="menu.css" rel="stylesheet" type="text/css" />
        <script src="http://code.jquery.com/jquery-latest.js"></script>
        <script type='text/javascript'>

            $(document).ready(function() {
                var currentItem = 1;
                $('#addnew').click(function() {
                    currentItem++;
                    $('#items').val(currentItem);
                    var strToAdd = '<tr><td>Year</td><td>:</td><td><select name="year' + currentItem + '" id="year' + currentItem + '" ><option value="2012">2012</option><option value="2011">2011</option></select></td><td>Month</td><td>:</td><td width="17%"><select name="month' + currentItem + '" id="month' + currentItem + '"><option value="1">January</option><option value="2">February</option><option value="3">March</option><option value="4">April</option><option value="5">May</option><option value="6">June</option><option value="7">July</option><option value="8">August</option><option value="9">September</option><option value="10">October</option><option value="11">November</option><option value="12">December</option></select></td><td width="7%">Week</td><td width="3%">:</td><td width="17%"><select name="week' + currentItem + '" id="week' + currentItem + '" ><option value="1">1</option><option value="2">2</option><option value="3">3</option><option value="4">4</option></select></td><td width="8%"></td><td colspan="2"></td></tr><tr><td>Actual</td><td>:</td><td width="17%"><input name="actual' + currentItem + '" id="actual' + currentItem + '" type="text" /></td><td width="7%">Max</td> <td width="3%">:</td><td><input name="max' + currentItem + '" id ="max' + currentItem + '"type="text" /></td><td>Target</td><td>:</td><td><input name="target' + currentItem + '" id="target' + currentItem + '" type="text" /></td></tr>';
                    $('#data').append(strToAdd);

                });
            });
        </script>
    </head>

    <body class="body">


        <div class ="Contentform">
            <form method="POST" action="test.php" >
                <input type="button" name="addnew" id="addnew" value="addnew"/>
                <table class="dd" width="100%" id="data">
                    <tr>
                        <td>Year</td>
                        <td>:</td>
                        <td><select name="year1" id="year1" >
                                <option value="2012">2012</option>
                                <option value="2011">2011</option>
                            </select></td>
                        <td>Month</td>
                        <td>:</td>
                        <td width="17%"><select name="month1" id="month1">
                                <option value="1">January</option>
                                <option value="2">February</option>
                                <option value="3">March</option>
                                <option value="4">April</option>
                                <option value="5">May</option>
                                <option value="6">June</option>
                                <option value="7">July</option>
                                <option value="8">August</option>
                                <option value="9">September</option>
                                <option value="10">October</option>
                                <option value="11">November</option>
                                <option value="12">December</option>
                            </select></td>
                        <td width="7%">Week</td>
                        <td width="3%">:</td>
                        <td width="17%"><select name="week1" id="week1" >
                                <option value="1">1</option>
                                <option value="2">2</option>
                                <option value="3">3</option>
                                <option value="4">4</option>
                            </select></td>
                        <td width="8%">&nbsp;</td>
                        <td colspan="2">&nbsp;</td>
                    </tr>
                    <tr>
                        <td>Actual</td>
                        <td>:</td>
                        <td width="17%"><input name="actual1" id="actual1" type="text" /></td>
                        <td width="7%">Max</td>
                        <td width="3%">:</td>
                        <td><input name="max1" id="max1" type="text" /></td>
                        <td>Target</td>
                        <td>:</td>
                        <td><input name="target1" id="target1" type="text" /></td>
                    </tr>

                </table>


                <input type="hidden" name="action" value="submit" />
                <input type="submit"  height="20" width="20" value="submit values" />

            </form>
            <?php
            include 'DBConfig.php';

            if (isset($_POST["action"]) && $_POST["action"] == "submit") {

                $count = 2;
                for ($i = 1; $i <= $count; $i++) {
                    $year = $_POST['year' . $i];
                    $month = $_POST['month' . $i];
                    $week = $_POST['week' . $i];
                    $actual = $_POST['actual' . $i];
                    $max = $_POST['max' . $i];
                    $target = $_POST['target' . $i];
                    $extreme = $_POST['extreme' . $i];
                    $que = "insert INTO table_name(id,year,month,week,actual,max,target) VALUES ('" . $_POST['type'] . "','" . $year . "','" . $month . "','" . $week . "','" . $actual . "','" . $max . "','" . $target . "')";
                    mysql_query($que);
                }
            }
            ?>
        </div>
    </body>
</html>