使用jquery php使用相同形式但不同输入字段的多文件上载

时间:2016-10-21 05:19:55

标签: javascript php jquery html

我在堆栈溢出时搜索了这个东西,我无法找到我的问题的相关答案。请查看我用来获取数据并将其丢弃到php文件并将文件上传到相应文件夹并将其链接存储到数据库的代码。但我无法存储数据。它显示"无法上传数据。"

Jquery文件:

<form  method="post" role="form" enctype="multipart/form-data"  action="javascript:;">
                    <input type="text" class="form-control" placeholder="Page Name" id="pagename" name="pagename" style="max-width:25%; display:inline" />
                    <input type="text" class="form-control" placeholder="Nav Name" id="nav" name="nav" style="max-width:25%; display:inline" />
                    <input type="text" class="form-control" placeholder="Menu Name" id="menuname" name="menuname" style="max-width:25%; display:inline" />
                    <input type="file" name="logo" id="logo" title="Menu Logo" class="form-control"  style="max-width:20%; display:inline"/>
                    <input type="text" class="form-control"  placeholder="Summery Part.. Please make it crisp" id="content" name="content" style="max-width:30%; display:inline" />
                        <script>
                                var editor = CKEDITOR.replace( "content", {
                                uiColor: "#ffffff",
                                filebrowserBrowseUrl : "../ckeditor-ckfinder-integration/ckfinder/ckfinder.html",
                                filebrowserImageBrowseUrl : "../ckeditor-ckfinder-integration/ckfinder/ckfinder.html?type=Images",
                                filebrowserFlashBrowseUrl : "../ckeditor-ckfinder-integration/ckfinder/ckfinder.html?type=Flash",
                                filebrowserUploadUrl : "../ckeditor-ckfinder-integration/ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Files",
                                filebrowserImageUploadUrl : "../ckeditor-ckfinder-integration/ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Images",
                                filebrowserFlashUploadUrl : "../ckeditor-ckfinder-integration/ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Flash"
                            });
                            CKFinder.setupCKEditor( editor, "../" );
                        </script>
                    <input type="file" name="file" id="file" class="form-control"  style="max-width:25%; display:inline"/>
                    <input type="submit" id="modalMenu" name="modalMenu" class="btn btn-success" align="right" value="+" />
                    </form>
某页上的

表格

$pageName=$_POST['page'];
     if($pageName=='modalMenu'){

$pagename=$_POST['pagename'];
$menuname=$_POST['menuname'];
$nav=$_POST['nav'];
$content=$_POST['content'];

$error=$_FILES['file']['error'];
    if($error!=0)
    {
    echo "Error with File data Upload. File not uploaded, it seems the file is not selected or there is some error with the file or the server seems busy, try later.
    We will redirect you back to the dashboard. Please wait..";
    }
    else
    {
    $fname=$_FILES['file']['name'];
    $ftype=$_FILES['file']['type'];
    $fsize=$_FILES['file']['size'];
    $ftname=$_FILES['file']['tmp_name'];
    $target="../modalMenu/$fname";

    $ans=move_uploaded_file($ftname,$target);
    if($ans)
    {
                        $error=$_FILES['logo']['error'];
                            if($error!=0)
                            {
                            echo "Error with Image Data Upload. File not uploaded, it seems the file is not selected or there is some error with the file or the server seems busy, try later.
                            We will redirect you back to the dashboard. Please wait..";
                            }
                            else
                            {
                            $logo_name=$_FILES['logo']['name'];
                            $logo_type=$_FILES['logo']['type'];
                            $logo_size=$_FILES['logo']['size'];
                            $logo_tname=$_FILES['logo']['tmp_name'];
                            $target2="../modalMenu/$fname";

                            $ans=move_uploaded_file($ftname,$target2);
                            if($ans)
                            {

                            //save info to database
                            $con=mysql_connect("localhost","root","");
                            mysql_select_db("rcg_db",$con) or die (mysql_error());
                            $target=addslashes($target);

                                $query="INSERT INTO `modalmenu`(`pagename`, `nav`, `menuname`, `menulogo`, `content`, `readmore`) VALUES ('$pagename','$nav','$menuname','$target' ,'$content','$target2');";
                                $n=mysql_query($query);

                                if($n==1)
                                {
                                    echo "File upload successful! Data is added. Please wait while page reloads";

                                }
                                else
                                {
                                echo "File not uploaded, server seems busy, try later. We will redirect you back to the dashboard. Please wait..";

                                }
                            }
                            }
    }
    else
    {
    echo "File not uploaded, server seems busy, try later. We will redirect you back to the dashboard. Please wait.";

    }

    }

}

insert.php文件

        Multipart multipart = new MimeMultipart("mixed"); 
        BodyPart messageBodyPart = buildHtmlTextPart(); //html is read and added to the mail body part
        BodyPart calendarPart = buildCalendarPart();
        BodyPart signatureImagePart = buildSignatureImagePart(); //image is read and added as a content part of html.

        multipart.addBodyPart(messageBodyPart);
        multipart.addBodyPart(calendarPart);
        multipart.addBodyPart(signatureImagePart);

提前谢谢你。如果我没有使用好的编码方式,请建议并解决问题。

2 个答案:

答案 0 :(得分:0)

根据我的理解,您希望将ajax发送到PHP,但PHP无法保存它?

我给你举个例子

的jQuery

$(#modalMenu).on('click',function(){
    var fname = $("#fname").val();
    var lname = $("#lname").val();
    var age = $("#age").val();

    $.ajax({
        url: 'insert.php', //point to server-side PHP script
        dataType: 'json',
        type: "POST",
        data:{
            "fname" : fname,
            "lname" : lname,
            "age" : age,
            "save" : true
        },
        success: function(yes){
            if(yes.success == true){
                window.location.reload(true);
            }
        }
    });
});

PHP

$fname = $_POST['fname'];
$lname = $_POST['lname'];
$age = $_POST['age'];

$sql = "INSERT INTO beneficiary (fname, lname, age)
    VALUES ('$fname','$lname','$age')";
$result = mysql_query($sql);

echo json_encode(array(
    "success" => true,
));

答案 1 :(得分:0)

参考以下链接,我认为这对您有用 http://www.codexworld.com/upload-multiple-images-using-jquery-ajax-php/