2

我是 php 和 mysql 的菜鸟,所以我希望你们能帮助我。我有一个带有按钮的表单,用户可以在其中动态添加一组输入字段。我正在使用 append 生成字段。我正在阅读有关它的教程,所以我理解了那部分。这是我基于附加脚本的站点,链接在这里。我的问题是,如果我有一组字段,而不仅仅是一个像该网站那样的输入字段作为示例显示,我该如何构造 sql 查询的 foreach 循环?这是我的追加的样子(我只显示 4 个字段,但每组追加总共有 16 个字段。)我想将发布数据上传到 sql 数据库。我一直在搜索该站点,但我只看到单个输入字段的 foreach 循环示例,这与我拥有的集合不同。

非常感谢,我希望有人能给我一个关于如何继续进行的想法。

<script type="text/javascript">
    var count = 0;
    $(function(){
    $('p#add_field').click(function(){
    count += 1;
    $('#row-fluid').append(

        '<div class="span12" style="border-bottom:1px lightgray solid; padding:10px 0px;">'
            +'<div class="row-fluid"> '                         

                +'<div class="span1">'
                +'<label>&nbsp;</label>'
                +'<label class="radio"><input type="radio" id="process_where' + count + '" name="process_where[]' + '" value="Online" onClick="checkProcessType(this.value)" >Online</label>'   
                +'<label class="radio"><input type="radio" id="process_where' + count + '" name="process_where[]' + '" value="In-store" onClick="checkProcessType(this.value)" >In-store</label>'                       +'</div>'

                +'<div class="span1">'
                +'<label>&nbsp;</label>'
                +'<label class="radio"><input type="radio" id="status' + count + '" name="status[]' + '" value="Actual" onClick="checkStatus(this.value)" >Actual</label>   '
                +'<label class="radio"><input type="radio" id="status' + count + '" name="status[]' + '" value="Ghost" onClick="checkStatus(this.value)" >Ghost</label>'
                +'</div>'

                +'<div class="span1">'
                +'<label>Type</label>'
                +'<select id="upg' + count + '" name="upg[]' + '" onChange="checkExp(this.value)" class="input-small" >'
                +' <option value="" selected="&nbsp;" >&nbsp;</option>'
                +'<option value="Exp" >Exp</option>'
                +'<option value="Post" >Post</option>'
                +'<option value="Upgrade" >Upg</option>'
                +'<option value="Retail" >Retail</option>'
                +'</select>'
                +'</div>'

                +'<div id="upg' + count + '" class="span2">'
                +'<label>Full Date</label>'
                +'<input name="upg_date[]' + '" id="upg_date' + count + '" type="text" id="upg_date" placeholder="YYYY-MM-DD" class="input-small" />'
                +'<img id="jscalendar' + count + '" type="image" src="img/calendar.png" style="border-width:0px; width:16px; height:16px; background-color:#FFF; margin-bottom:-7px;" onclick="createPopCalendar3()">'
                +'</div>'
4

5 回答 5

1

并像这样更改您的 php 提交页面:

if(is_array($_POST[counter]))
{
$count=count($_POST[counter]);

for($i=0;$i<=$count;$i++)
{


$process_where=$_POST["process_where".$i.""];
$status=$_POST["status".$i.""];
$upg=$_POST["upg".$i.""];
$upg_date=$_POST["upg_date".i.""];

if($process_where !="")
{
//Query Run Here
  $query="insert into tablename (process_where,status,upg,upg_date) values ('".$process_where."','".$status."','".$upg."','".$upg_date."')";
 mysql_query($query) or die(mysql_error());
}




}

}
于 2012-11-02T08:16:41.107 回答
0

将您的 javascript 更改为:

<script type="text/javascript">
    var count = 0;
    $(function(){
    $('p#add_field').click(function(){
    count += 1;
    $('#row-fluid').append(

        '<div class="span12" style="border-bottom:1px lightgray solid; padding:10px 0px;">'
            +'<div class="row-fluid"> '                         

                +'<div class="span1">'
                +'<label>&nbsp;</label>'
                +'<label class="radio"><input type="radio" id="process_where' + count + '" name="process_where' + count +'" value="Online" onClick="checkProcessType(this.value)" >Online</label>'   
                +'<label class="radio"><input type="radio" id="process_where' + count + '" name="process_where' + count +'" value="In-store" onClick="checkProcessType(this.value)" >In-store</label>'                       +'</div>'

                +'<div class="span1">'
                +'<label>&nbsp;</label>'
                +'<label class="radio"><input type="radio" id="status' + count + '" name="status' + count +'" value="Actual" onClick="checkStatus(this.value)" >Actual</label>   '
                +'<label class="radio"><input type="radio" id="status' + count + '" name="status' + count +'" value="Ghost" onClick="checkStatus(this.value)" >Ghost</label>'
                +'</div>'

                +'<div class="span1">'
                +'<label>Type</label>'
                +'<select id="upg' + count + '" name="upg' + count +'" onChange="checkExp(this.value)" class="input-small" >'
                +' <option value="" selected="&nbsp;" >&nbsp;</option>'
                +'<option value="Exp" >Exp</option>'
                +'<option value="Post" >Post</option>'
                +'<option value="Upgrade" >Upg</option>'
                +'<option value="Retail" >Retail</option>'
                +'</select>'
                +'</div>'

                +'<div id="upg' + count + '" class="span2">'
                +'<label>Full Date</label>'
                +'<input name="upg_date' + count +'" id="upg_date' + count + '" type="text" id="upg_date" placeholder="YYYY-MM-DD" class="input-small" />'
                +'<img id="jscalendar' + count + '" type="image" src="img/calendar.png" style="border-width:0px; width:16px; height:16px; background-color:#FFF; margin-bottom:-7px;" onclick="createPopCalendar3()">'
                +'<input type="hidden" name="counter[]" value="1"></div>'

并像这样更改您的 php 提交页面:

if(is_array($_POST[counter]))
{


foreach($_POST[counter] as $key=>$val)
{


$process_where=$_POST["process_where".$key.""];
$status=$_POST["status".$key.""];
$upg=$_POST["upg".$key.""];
$upg_date=$_POST["upg_date".$key.""];

if($process_where !="")
{
//Query Run Here
  $query="insert into tablename (process_where,status,upg,upg_date) values ('".$process_where."','".$status."','".$upg."','".$upg_date."')";
 mysql_query($query) or die(mysql_error());
}




}

}
于 2012-11-02T06:36:14.633 回答
0

您可以这样做。在提交表单时检查此代码:

if(is_array($_POST[process_where]))
{


foreach($_POST[process_where] as $key=>$val)
{

$process_where=$_POST[process_where][$key];
$status=$_POST[status][$key];
$upg=$_POST[upg][$key];
$upg_date=$_POST[upg_date][$key];

//Query Run Here
  $query="insert into tablename (process_where,status,upg,upg_date) values ('".$process_where."','".$status."','".$upg."','".$upg_date."')";
 mysql_query($query) or die(mysql_error());




}

}
于 2012-11-01T11:28:31.227 回答
0

为了简化它,如果我这样做,什么都不会打印。

if(is_array($_POST[counter]))
{
$count=count($_POST[counter]);
echo $count;

    for($i=0;$i<=$count;$i++)
{   

    echo "The number is " . $i . "<br />";

}

}

但如果我这样做, $count 打印得很好。

if(is_array($_POST[counter]))
{
$count=count($_POST[counter]);
echo $count;

}

于 2012-11-02T15:39:17.457 回答
0

提交后请上传修改后的版本

if(is_array($_POST[counter]))
{
$count=count($_POST[counter]);

for($i=0;$i<=$count;$i++)
{


$process_where=$_POST["process_where".$i.""];
$status=$_POST["status".$i.""];
$upg=$_POST["upg".$i.""];
$upg_date=$_POST["upg_date".i.""];

if($process_where !="")
{
//Query Run Here
  $query="insert into tablename (process_where,status,upg,upg_date) values ('".$process_where."','".$status."','".$upg."','".$upg_date."')";
 mysql_query($query) or die(mysql_error());
}




}

}
于 2012-11-02T16:02:01.630 回答