I'm currently developing a textarea that whatever pasted after the push of button, it will be added to the database. For now, let's leave the database function aside, let's just focus on the data POST event.
Here's I gone so far
jquery
<script>
$(function(){
$('#sw').click(function(){
var fld=$("#txt1").val().split('
');
$.each(fld, function(){
$.post('up.php',
{ 'ldta': this.split('\t') },
function(data) {
$('#out').html(data);
alert(data);
}
);
});
}
);
});
</script>
html
<body>
<form>
<textarea id="txt1" rows="5" cols="250"></textarea>
<input type="button" id="sw" value="Add">
</form>
<h2 id="out"></h2>
php (up.php)
$ldta=$_POST['ldta'];
$out='';
foreach($ldta as $line) {
if (!$line == "")
{
$out.=$line.',';
}
}
echo $out;
Why it seemed only 1 row was posted? I used
alert(data);
under the jquery script and I found out that all contents are displayed
I used $out.= to concatenate whatever is being passed but it seems the php only gets the last row..
Test data to try with (separated with tabs)
111 2015-02-11 20:49:00 0.00
222 2015-02-11 20:52:00 0.00
333 2015-02-11 19:25:00 0.00
Probably there's something missing on the code? Please let me know
In you current implementation you send one request per line, so for your test input you will send three independend requests. I suggest you, to build an object that will be parsed by you PHP code before sending it. Try something like that:
$(function () {
$('#sw').click(function () {
var fld = $("#txt1").val().split('
')
, data = [];
$.each(fld, function(index, element){
data.push({'ldta': element.split(' ')});
});
$.post('/echo/html/', {
html: JSON.stringify(data)
},
function (data) {
$('#out').append(data);
});
});
});