使用AJAX从不同的HTML文件POST到PHP?

时间:2015-06-15 08:22:31

标签: javascript php jquery html ajax

我有3个HTML文件,用于将图片上传到特定文件夹和子文件夹。我面临的问题是,当我从第1和第2个HTML页面发布POST时,我可以将图像移动到特定的文件夹和子文件夹,但是一旦我从第3个HTML发布文件,它就会给我一个成功警报但是图片未上传。以下是代码。

PHP:

 if(isset($_POST))
{
    session_start();
    //The target location for the image to be stored.
    $_SESSION['str'];
    $_SESSION['str1'];
    $_SESSION['str2'];
    //To rename the file
    $_SESSION['img'];
    $_SESSION['img1'];
    $_SESSION['img2'];

     //1st IF statement
     if(!empty($_FILES["info_file"]["name"])){

     move_uploaded_file($_FILES['info_file']['tmp_name'], $_SESSION['str'].$_SESSION['img']);


    }
    else 
        {
        //2nd IF statement
        if(!empty($_FILES["info_file1"]["name"])){
            move_uploaded_file($_FILES['info_file1']['tmp_name'], $_SESSION['str1'].$_SESSION['img1']);
        }
        else
        {
            if(!empty($_FILES["info_file2"]["name"]))
            {
            move_uploaded_file($_FILES['info_file2']['tmp_name'], $_SESSION['str2'].$_SESSION['img2']);
            }
        }
        }
}

HTMLS:

第一

<script>

function readURL(input) {

        if (input.files && input.files[0]) {
        var reader = new FileReader();

        reader.onload = function (e) {
            $('#blah')
                .attr('src', e.target.result)
                .width(300)
                .height(340);
        };

        reader.readAsDataURL(input.files[0]);
    }
   uploadfunc();

  }
</script>
<script> 
function uploadfunc(){  
     if(!navigator.onLine){ 
        alert("你已離線,請再試一次");
        return false;   
    }else{
     var formData = new FormData();
     var info_file_data = $('#imageid').prop('files')[0];
     console.log(info_file_data);
      formData.append('info_file', info_file_data);
      // for 2nd and 3rd HTML onlt this 'info_file' changed to 'info_file1' and 'info_file2' respectively.
     $.ajax({
          url: 'login.php', // point to server-side PHP script 
          dataType: 'text',  // what to expect back from the PHP script, if anything
          cache: false,
          contentType: false,
          processData: false,
          data: formData,                         
          type: 'post',
          success: function(php_script_response){
            alert ("上載成功/ Upload Successful");
          }
        });
    }
}
</script>

</head>
<body style="text-align:center">


<h1 style="color:#F00">SMArt-InterAct</h1><br/>

<h2 style="color:#03F">相片上載</h2>
  <form enctype="multipart/form-data" id="form" method="POST">
     <input type="file" required id="imageid" name="image" onChange="readURL(this);" />
    <img id="blah" src="#" alt="你的相片" /><br/><br/>
     <input type="button" class="test" value="上傳"/>
  </form>

</body>

2 个答案:

答案 0 :(得分:0)

我错了,如果别的请试试这段代码

 if(isset($_POST))
    {
        session_start();
        //The target location for the image to be stored.
        $_SESSION['str'];
        $_SESSION['str1'];
        $_SESSION['str2'];
        //To rename the file
        $_SESSION['img'];
        $_SESSION['img1'];
        $_SESSION['img2'];

         //1st IF statement
         if(!empty($_FILES["info_file"]["name"])){
        move_uploaded_file($_FILES['info_file']['tmp_name'], $_SESSION['str'].$_SESSION['img']);
             echo 'Upload Successful';
        }
        else if(!empty($_FILES["info_file1"]["name"])){
                move_uploaded_file($_FILES['info_file1']['tmp_name'], $_SESSION['str1'].$_SESSION['img1']);
            }else{
               move_uploaded_file($_FILES['info_file2']['tmp_name'], $_SESSION['str2'].$_SESSION['img2']);
                }     
    }

答案 1 :(得分:0)

我自己找到了这个问题的答案。只发布if-else部分而不是整个代码。

 //1st IF statement
           if(!empty($_FILES["info_file"]["name"]))
      {
         move_uploaded_file($_FILES['info_file']['tmp_name'], $_SESSION['str'].$_SESSION['img']);
         echo "$asd";
     }
         if (!empty($_FILES["info_file1"]["name"])){
                move_uploaded_file($_FILES['info_file1']['tmp_name'], $_SESSION['str1'].$_SESSION['img1']);
            echo "$asd1";
            }

      if(empty($_FILES["info_file"]["name"]) || empty($_FILES["info_file1"]["name"]) ){

   move_uploaded_file($_FILES['info_file2']['tmp_name'], $_SESSION['str2'].$_SESSION['img2']);