2014-11-11 7 views
0

이미지와 텍스트를 별도의 형식으로 업로드하면 잘 동작합니다. 하지만 내가 함께 추가 할 때 작동하지 않습니다. 내 양식 텍스트는 js로 업로드하고 이미지는 PHP 파일로 업로드합니다. 그리고 내 문제는 제 모양으로 생각합니다.이미지와 텍스트를 같은 형식으로 업로드

js와 함께 업로드하는 경우 아래에 추가되는 js 및 submit.php에서 변경됩니다.

여기에 함께

<form action="" method="post" id="cmntfrm" enctype="multipart/form-data"> 
<fieldset id="cmntfs"> 
<legend class="pyct"> 
What's your mind 
</legend> 
<input type="hidden" name="username" size="22" tabindex="1" id="author" value="'.$pname.'"/> 
<input type="hidden" name="email" size="22" tabindex="2" id="email" value="'.$email.'"/> 
<p><textarea name="comment" rows="10" tabindex="4" id="comment"></textarea></p> 


<div id="ajaxuploadfrm"> 
<form action="uploadpostimg.php" method="post" enctype="multipart/form-data"> 
<b>Select an image (Maximum 1mb)</b> 
<input type="file" name="url" id="url" /> 
</form> 
</div> 

<p><input type="submit" name="submit" value="Post comment" tabindex="5" id="submit"/></span></p> 
</fieldset>    
<input type="hidden" name="parent_id" id="parent_id" value="0" /> 
<input type="hidden" name="tutid2" id="tutid" value="'.$tutid2.'" /> 
</form> 

JS

$(document).ready(function(){ 
var inputAuthor = $("#author"); 
var inputComment = $("#comment"); 
var inputEmail = $("#email"); 
var inputUrl = $("#url"); 
var inputTutid = $("#tutid"); 
var inputparent_id = $("#parent_id"); 
var commentList = $(".content > comment"); 
var commentCountList = $("#updatecommentNum"); 
var error = $("#error"); 
error.fadeOut(); 

    function updateCommentbox(){ 
     var tutid = inputTutid.attr("value"); 
    //just for the fade effect 
    commentList.hide(); 
    //send the post to submit.php 
    $.ajax({ 
     type: "POST", url: "submit.php", data: "action=update&tutid="+ tutid, 
     complete: function(data){ 
      commentList.prepend(data.responseText); 
      commentList.fadeIn(2000); 
     } 
    }); 
} 

    function updateCommentnum(){ 
    var tutid = inputTutid.attr("value"); 
    //just for the fade effect 
    commentList.hide(); 
    //send the post to submit.php 
    $.ajax({ 
     type: "POST", url: "submit.php", data: "action=updatenum&tutid="+ tutid, 
     complete: function(data){ 
      commentCountList.html(data.responseText); 
      commentList.fadeIn(2000); 
     } 
    }); 
} 

    function error_message(){ 
    error.fadeIn(); 
    } 

    function checkForm(){ 
    if(inputAuthor.attr("value") && inputComment.attr("value") && inputEmail.attr("value")) 
     return true; 
    else 
     return false; 
     } 

    //on submit event 
$("#cmntfrm").submit(function(){ 
error.fadeOut(); 
if(checkForm()){ 
var author = inputAuthor.attr("value"); 
var url = inputUrl.attr("value"); 
var email = inputEmail.attr("value"); 
var comment = inputComment.attr("value"); 
var parent_id = inputparent_id.attr("value"); 
var tutid = inputTutid.attr("value"); 

     //we deactivate submit button while sending 
     $("#submit").attr({ disabled:true, value:"Sending..." }); 
     $("#submit").blur(); 
     //send the post to submit.php 
     $.ajax({ 
      type: "POST", url: "submit.php", data: "action=insert&author="+ author + "&url="+ url + "&email="+ email + "&comment="+ comment + "&parent_id="+ parent_id + "&tutid="+ tutid, 
      complete: function(data){ 
       error.fadeOut(); 
       commentList.prepend(data.responseText); 
       updateCommentbox(); 
       updateCommentnum(); 

       //reactivate the send button 
       $("#submit").attr({ disabled:false, value:"Submit Comment!" }); 
       $('#cmntfrm').each(function(){ 
    this.reset(); 
}); 
} 
     }); 
    } 
    else //alert("Please fill all fields!"); 
      error_message(); 
    //we prevent the refresh of the page after submitting the form 
    return false; 
}); 
}); 

Submit.php

당신은 양식을 중첩 할 수 없습니다
<?php header('Content-Type: charset=utf-8'); ?> 
<?php 

     include("db.php"); 
     include_once("include/session.php"); 
     switch($_POST['action']){ 
     case "update": 
     echo updateComment($_POST['tutid']);   
     break; 
     case "updatenum": 
     echo updateNumComment($_POST['tutid']);  
     break; 
     case "insert": 
     date_default_timezone_set('Asia/Dhaka'); 
     echo insertComment($_POST['author'], $_POST['comment'], $_FILES['url']['name'], $_POST['email'], $_POST['tutid'], $_POST['parent_id'], $date = date("M j, y; g:i a")); 
     break; 
     } 

function updateNumComment($tutid) { 
//Detail here 
} 

function updateComment($tutid) { 
//Detail here 
} 


function insertComment($username, $description, $url, $email, $qazi_id, $parent_id, $date){ 
global $dbh; 

//Upload image script that not work here when i try together so i took it at separate file and then tried with above form 

    $output_dir = "comimage/"; 
    $allowedExts = array("jpg", "jpeg", "gif", "png","JPG"); 
    $extension = @end(explode(".", $_FILES["url"]["name"])); 
     if(isset($_FILES["url"]["name"])) 
     { 
      //Filter the file types , if you want. 
      if ((($_FILES["url"]["type"] == "image/gif") 
       || ($_FILES["url"]["type"] == "image/jpeg") 
       || ($_FILES["url"]["type"] == "image/JPG") 
       || ($_FILES["url"]["type"] == "image/png") 
       || ($_FILES["url"]["type"] == "image/pjpeg")) 
       && ($_FILES["url"]["size"] < 504800) 
       && in_array($extension, $allowedExts)) 
      { 
        if ($_FILES["url"]["error"] > 0) 
        { 
        echo "Return Code: " . $_FILES["url"]["error"] . "<br>"; 
        } 
       if (file_exists($output_dir. $_FILES["url"]["name"])) 
        { 
        unlink($output_dir. $_FILES["url"]["name"]); 
        } 
        else 
        { 
        $pic=$_FILES["url"]["name"]; 
        $conv=explode(".",$pic); 
        $ext=$conv['1'];  

        $user = $_SESSION['username']; 
        //move the uploaded file to uploads folder; 
         move_uploaded_file($_FILES["url"] ["tmp_name"],$output_dir.$user.".".$ext); 
        $pic=$output_dir.$user.".".$ext; 
        $u_imgurl=$user.".".$ext; 
        } 
      } 
      else{echo '<strong>Warning !</strong> File not Uploaded, Check image' ;} 
     } 
     //Submit main comment 
     if ($parent_id == 0){ 
     $username = mysqli_real_escape_string($dbh,$username); 
     $description = mysqli_real_escape_string($dbh,$description); 

     $sub = "Comment to"; 
     $query = "INSERT INTO comments_lite VALUES('','$qazi_id','0','$username','$email','$description','','$parent_id','$date')"; 
     mysqli_query($dbh,$query); 

     } else { 

     if ($parent_id >= 1){ 
        global $dbh;  
     $username = mysqli_real_escape_string($dbh,$username); 
     $description = mysqli_real_escape_string($dbh,$description); 

     $sub2 = "Reply to"; 
     $query = "INSERT INTO comments_reply VALUES('','$qazi_id','0','$username','$email','$description','','$parent_id','$date')"; 
     mysqli_query($dbh,$query); 

} 
} 
} 
?> 

답변

0

작동하지 그 내 양식 코드입니다. 그것을 피하고 아래 양식을 분리하십시오. 다른 양식의 데이터 인 경우 양식을 제출하는 동안이 양식에 숨겨진 필드를 작성하여 제출하십시오.

다른 제안 : 어쨌든 자바 스크립트로 작업하고 있으므로 보이지 않는 div에 업로드 양식을 아웃소싱하고 업로드 버튼을 클릭하거나 마우스를 올려 놓거나 form1의 마지막 입력란 등을 입력하여 팝업으로 만듭니다. 당신이 확인해야 JS의 코드를 넣을 수 있습니다 제출의 클릭에

3

JS의 변화가 .PHP 페이지에

$.post('phpapgename.php',data:jquerydata,function(){ 
}) 

파일 당신은 당신의 데이터를 전송하기 위해 쿼리를 넣을 수 있습니다.

+0

세부 사항을 알려주십시오. 내 js 파일은 위에 있습니다. 감사합니다 – jainul

+0

방금 ​​삭제 된 질문에 대한 답변을 찾고있는 경우 : http://codepen.io/anon/pen/zGZwmK – connexo

관련 문제