javascript - 以同样的形式上传图片和文字

标签 javascript php jquery forms

当我通过单独的形式上传图像和文本时,效果很好。但当我加在一起时它不起作用。 我的表单文本通过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;
});
});

提交.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);

}
}
}
?>

最佳答案

点击提交后,您可以将代码放入js中,您必须在js文件中进行更改

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

在 .php 页面中,您可以输入查询来提交数据。

关于javascript - 以同样的形式上传图片和文字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26862190/

相关文章:

javascript//这里发生了什么?

javascript - 本地存储用于保存 fork 和页面数据直到最终提交

c# - 如何从 window.external.Notify ("someText")的事件处理程序(即 WebBrowser.ScriptNotify 事件)获取结果返回给 JS?

php - 表在数据库中不存在,但确实存在

php - 如何通过JQuery/Ajax请求返回最新的Mysql条目

javascript - PHP Echo SUM session 值和表单选择

php - 运行 Laravel 应用程序时在此服务器上找不到请求的 URL

javascript - css bootstrap slider 菜单修复

jquery - 如何使用jquery按特定文本选择按钮

javascript - 元素 JQuery 的祖 parent 的第一个 child