我有一个可以发布姓名的ajax代码还有一个可以发布图片的ajax代码我需要把这两个功能结合起来才能得到一个既可以发布姓名又可以发布图片的函数
下面用于发布图像的代码
<script>
$(document).ready(function(){
$("#but_upload").click(function(){
var fd = new FormData();
var files = $('#file')[0].files;
// Check file selected or not
if(files.length > 0 ){
fd.append('file',files[0]);
$.ajax({
url: 'upload.php',
type: 'post',
data: fd,
contentType: false,
processData: false,
success: function(response){
if(response != 0){
$("#img").attr("src",response);
$(".preview img").show(); // Display image element
}else{
alert('file not uploaded');
}
},
});
}else{
alert("Please select a file.");
}
});
});
</script>
及以下代码用于发布名称
<script type="text/javascript">
function clickButton(){
var name=document.getElementById('name').value;
$.ajax({
type:"post",
url:"upload.php",
data:
{
'name' :name
},
cache:false,
success: function (html)
{
alert('Data Send');
$('#msg').html(html);
}
});
return false;
}
</script>
我如何结合以上代码,以便只使用一个url "upload.php"这意味着upload.php将在数据库中插入名称并在文件夹中保存图像,同时单击保存按钮,这就是为什么我需要组合代码
请任何人帮助我
实际上是将它们组合在一起。
您可以使用第一个函数并执行以下操作:
var fd = new FormData();
var files = $('#file')[0].files;
fd.append('name', $("#name").val();
就是这样。在另一边(后端)你只需要输入这个名字:
$name = $_POST['name'];