我想通过 jquery 的 $post()
发送多个 base64 字符串。字符串的数量并不总是相同的。我怎样才能做到这一点并在 php 中获取它?
将所有字符串放在一个数组中并将它们添加到 $post()
中是一个不错的选择吗?
var items = [
"data:image/png;base64,i.....",
"data:image/png;base64,i....",
"data:image/png;base64,i...."
] //the number od these strings varies on each post
$.post("../send.php",
{
for(i=0;i<21;i++){
'item'+i: items[i]
}
},
)
PHP:
if($_POST['item1']){
$item1 = $_POST['item1'];
}
最佳答案
我会按照以下步骤进行:
1) 使用包含所有这些 base64 字符串的输入字段创建一个表单。 (表单及其所有字段都可以隐藏在html中) 2)在我的表单中,所有输入文本字段都可以具有与本例相同的名称
<input type="text" name="text1[]">
2) 当我需要添加一个新字符串时,我将使用 jQuery.append() 在该表单中添加一个输入字段 3) 在我的 jquery 帖子中,我将数据设置为
$.post('../send.php',$('#myFormId').serialize(),function(){
// what i want to do with the response
})
4) 在我的 php 页面中我可以很容易地循环
foreach($_POST['item1'] as $item){
// do what you want with data
}
就是这样!
关于javascript - 向php发送多个base64,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31802085/