循环内$ _post [英] $_post inside a loop

查看:97
本文介绍了循环内$ _post的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有5个文本框,名称为textbox1,textbox2 ... textbox5。现在我想要的是,而不是将它们的每个值分别放在一个变量中,我想在循环中完成它,我在这里有我的代码,但是在输入提交错误后,比如


数组到字符串的转换...


未定义的索引:textboxArray in ...


。请查看错在哪里与我的代码。

$ $ p $ if(isset($ _ POST ['submit'])){
for($ i = 0; $ i< 5; $ i ++){
$ sasa = $ _POST [textbox。[$ i]。];
$ sql =插入sasa(sasa)值('$ sasa');
$ q = $ conn-> query($ sql);
}
}


解决方案

问题是这一行: $ sasa = $ _POST [textbox。[$ i]。] ;



你应该这样做:

  if(isset $ _BOST ['submit'])){

if(is_array($ _ POST [textbox])){
foreach($ _ POST [textbox] as $ sasa){
//这是不安全的,使用预处理语句来代替
$ sql =插入sasa(sasa)值('$ sasa');
$ q = $ conn-> query($ sql);



$ b code
$ b

这允许你可以写下你的表格:

 < form method =post...> 

< input type =textname =textbox []/>
< input type =textname =textbox []/>
< input type =textname =textbox []/>

< button type =submit>提交< / button>

< / form>

在回答您的评论时,您可以如何使用jQuery来添加/删除输入内容:

  var control = $('< div class =controls>< / div>'); 
control.append(< input class ='form-control'type ='text'name ='textbox []'placeholder ='textbox'/>< a href ='#'class =' remove_this btn btn-danger'>删除< / a>);
control.appendTo('form');

control.find('。remove_this')。click(function(){
control.remove();
});


i have 5 textboxes with the name of textbox1,textbox2 ... textbox5. now what i want is that instead of putting each of their value in a variable individually I want to do it in a loop, i have my code here but after i enter submit errors like

Array to string conversion in ...

and

Undefined index: textboxArray in...

.please see what's wrong with my code.

if(isset($_POST['submit'])){
    for($i=0; $i<5; $i++){
        $sasa = $_POST["textbox".[$i].""];  
        $sql="INSERT into sasa (sasa) values('$sasa')";
        $q=$conn->query($sql);
    }   
}

解决方案

The problem is this line: $sasa = $_POST["textbox".[$i].""];

You should do it as follows:

if(isset($_POST['submit'])){

    if(is_array($_POST["textbox"])){
        foreach($_POST["textbox"] as $sasa){
            //This is unsafe, use prepared statements instead
            $sql="INSERT into sasa (sasa) values('$sasa')";
            $q=$conn->query($sql);
        }
    }

}

This allows you to write your form like:

<form method="post" ... >

    <input type="text" name="textbox[]"/>
    <input type="text" name="textbox[]"/>
    <input type="text" name="textbox[]"/>

    <button type="submit">Submit</button>

</form>

In answer to your comment, this is how you could add/remove inputs dinamically using jQuery:

var control = $('<div class="controls"></div>');
control.append("<input class='form-control' type='text' name='textbox[]' placeholder='textbox'/><a href='#' class='remove_this btn btn-danger'>remove</a>");
control.appendTo('form');

control.find('.remove_this').click(function(){
    control.remove();
});

这篇关于循环内$ _post的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆