Reputation: 49384
I have a form with a textarea.
The textarea has multiple words separate by a comma, see below...
<form method="post" action="send.php">
<textarea>data1, data2, data3</textarea>
<input type="submit" value="submit" />
</form>
What I need to do is to create some kind of loop where the form gets submitted multiple times...
Each time is will send a different value from the textarea.
For example:
Start:
1. data1 .. Submit
2. data2 .. Submit
3. data3 .. Submit
End.
Is there a way to do this in php or php and javascript?
Upvotes: 0
Views: 589
Reputation: 3272
Is there a reason why you want to submit multiple time?
The easy way to do this is to sumbit the entire textbox value and then have php spilt it up using "," as a delimiter and trim off the whitespace
<form method="post" action="send.php">
<textarea name="textboxdata">data1, data2, data3</textarea>
<input type="submit" value="submit" />
</form>
<?php
$textboxarray = array_map('trim',explode(",",$POST['textboxdata']));
?>
OUTPUT
Array
(
[0] => data1
[1] => data2
[2] => data3
)
Upvotes: -1
Reputation: 3138
Sure. get the reference of the textarea (can also be done by id if you wish).
var text = document.getElementsByTagName('textarea')[0].innerHTML
var separated = text.splt(',');
for(var a = 0 ; a < separated.length ; a++){
//SENDREQUEST IS A FUNCTION YoU HAVE TO BUILD.
sendRequest(separated[a].trim());
}
Upvotes: 1
Reputation: 77966
Pseudo code :
$('#my-form').on('submit', function(e) {
var $this = $(this),
data = $this.find('textarea').text().split(',');
data.forEach(function(item, index, array) {
$.ajax({
url : $this.attr('action'),
data : item,
type : $this.attr('method')
});
});
});
Upvotes: 1