$_post Inside A Loop
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 l
Solution 1:
This is incorrect:
$sasa = $_POST["textbox".[$i].""];
^^^^
[$i]
defines a new array with a single integer in it, which you then concatenate into a string. Arrays used in a string context simply become the literal word Array
, which means you're effectively running this:
$sasa = $_POST["textboxArray"];
which doesn't exist in your form.
You want
$sasa = $_POST["textbox{$i}"];
instead. Note the {}
.
And note that your code is vulnerable to sql injection attacks.
Solution 2:
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" ... >
<inputtype="text" name="textbox[]"/>
<inputtype="text" name="textbox[]"/>
<inputtype="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 = $('<divclass="controls"></div>');
control.append("<inputclass='form-control'type='text'name='textbox[]'placeholder='textbox'/><ahref='#'class='remove_this btn btn-danger'>remove</a>");
control.appendTo('form');
control.find('.remove_this').click(function(){
control.remove();
});
Post a Comment for "$_post Inside A Loop"