如何通过jquery post上传php mysql文件?

时间:2013-12-02 19:34:18

标签: php jquery mysql

我有一个问题与上传文件+ php + mysql + json + jquery帖子:( 我不知道这个:(。我需要你的帮助。

我有这段代码:

PHP代码:

include ('conection.php');
$arr = json_decode($_POST['send'],true);
if ($_POST['send']>NULL){
mysql_query("INSERT INTO liciteiro.documentos(doc_id,doc_descricao,doc_url,doc_validade,fkclient_id) VALUES ('".$arr[0]['doc_id']."','".$arr[0]['doc_descricao']."','".$arr[0]['doc_url']."','".$arr[0]['doc_validade']."','".$arr[0]['fkclient_id']."')")or die(mysql_error());
}
else {
    die(mysql_error());
}

JQuery的

$(function(){
    $("#send").click(function() {
    var json = $.parseJSON($("#json").val());
    var url2 = $("#url2").val();
            $.post(url2,{data:json},function(data){
        $('#status').html(data);
    });
    return false;
        });
        });

HTML

  <br />
  <br />
  <div id="geralform">
<div id="form1" class="send" onsubmit="return send();">
<div class="input-prepend input-append">
<span class="add-on"> URL:</span><input type="text" class="input-large"name="url2" id="url2" /></p>
</div>
<br />
<div class="input-prepend input-append"><span class="add-on">JSON:</span><textarea name="json" id="json" cols="45" rows="5" class="input-xlarge"></textarea><br />
<div class="input-prepend input-append" id="fileupload"><input name="file" id="file" type="file" class="input-xlarge"/></div>
<p><button type="submit" name="send" value="send" class="btn" id="send" />Send</button></div>
<p></p>
</div>
</div><br />

<hr />
<div class="input-prepend input-append"><span class="add-on">Retorno:</span>
<textarea name="status" id="status" class="input-xxlarge" cols="100" rows="4"></textarea>
</p>
</div>
</div>

我有两个问题,我需要在数据库和文件上传文本apper中注册数据,如果我输入textfield URL:include_documentos.php没有这个区域不会apper。

请帮帮我。

我需要你的帮助:(

对不起我的英语不好,我来自巴西,这里没有任何好的英语学校:x。

2 个答案:

答案 0 :(得分:0)

首先,else-block中的die(mysql_error());并不真正有意义,因为if-condition与mysql无关。

其次,你必须在sql-query中转义变量部分,至少如果它是一个可公开访问的项目。

现在回答你的问题,...你不能只通过javascript发送文件。您将需要支持HTML5的浏览器。 这可以帮助您:How can I upload files asynchronously?

答案 1 :(得分:0)

到目前为止,我找到的最佳解决方案是Jquery Form Plugin

希望这有帮助。