如何使用Ajax表单将文件保存到数据库?

时间:2018-11-14 07:53:50

标签: php jquery ajax

需要有关此表格的帮助人员,将数据保存到数据库中很好,我的问题是我无法将文件保存在数据库中,我不知道要更改什么,我需要添加var file还是添加{{1} }真的不知道该怎么办,而且我对如何使用ajax表单保存文件一无所知,而且我在jQuery / ajax中也不是很好。请帮忙。

<form id=''>

upload.php

<textarea class='message' cols ='30' rows ='5' id = 'message'></textarea>
<form method="post" enctype="multipart/form-data">
<label for="fileToUpload"><img src="img icon.png" title="Attach Image"> 
</label>
<input id="fileToUpload" name="fileToUpload" type="file" onchange="document.getElementById('file').value = this.value.split('\\').pop().split('/').pop()"/>
<input type="text" name="file" id="file" class="file">
<button class="submit"  id="submit" name="submit" >Comment</button>
</form>

<script>
    $('.submit').click(function(){
        var name = '<?php echo $row['name'];?>';
        var message = $('.message').val();
        if(message.length>1){
        $.ajax({
            url:'upload.php?id=<?php echo $id;?>',
            data:'name='+name+'&message='+message,
            type:'post',
            success:function()
            {
                $('.message').val('');
                listComments();
                alert('Post Message');
            }
        })
        }else{ 
            alert('PLEASE DO RIGHT A MESSAGE BEFORE SENDING'); 
            }
    })
</script>

0 个答案:

没有答案