表单正在提交但不通过POST发送数据

时间:2017-04-11 07:04:23

标签: php html forms post

它似乎可以复制为this,但事实并非如此。由于同样的原因,this也不是我的情况。

这是我的form代码。

   <form id="form-box" name="formName" action="notes-uploading.php" onSubmit="return validateForm()" method="POST" enctype="multipart/form-data">
            <div class="sub-box" align="left">
                <div class="sub-box-name">
                    <div class="box-num" align="center">1</div>
                    <div class="box-name">Notes Info</div>
                </div>

                <!-- Text [Name] -->
                <input type="text" name="naam" class="sub-box-ip-txt" placeholder="Notes/Assignment Name*" required>


                <!-- Select [Stream] -->
                <select id="select-input" name="stream" required>
                    <option value="Wrong">Choose a stream</option>
                    <option value="Science & Engineering">Science & Engineering</option>
                    <option value="Business & Management">Business & Management</option>
                    <option value="Arts & Humanity">Arts & Humanity</option>
                    <option value="Architecture">Architecture</option>
                    <option value="Medical">Medical</option>
                    <option value="Law">Law</option>
                    <option value="Competition">Competition</option>
                    <option value="Others">Others</option>
                </select>

                <div class="error" id="slctErr"></div>

                <!-- Text [Subject] -->
                <input type="text" name="subject" class="sub-box-ip-txt" placeholder="Subject Name*" required>

                <!-- Text [Topic Name] -->
                <input type="text" name="topic" class="sub-box-ip-txt" placeholder="Topic Name*" >

                <!-- Text [Course] -->
                <input type="text" name="course" class="sub-box-ip-txt" placeholder="Course*" >

                <!-- Text [Branch] -->
                <input type="text" name="branch" class="sub-box-ip-txt" placeholder="Branch*" >

                <span class="field-name">Upload Docs:</span>

                <label id="upload-btn">
                    <input type="file" name="files[]" accept=".jpg, .png, .jpeg, .pdf, .ppt, .docx, .xlsx" id="img-upload" multiple>
                   <span class="upload-button"><i class="fa fa-upload"></i>Select Docs*</span>
                </label>

                <div class="error" id="uploadErr"></div>


                <textarea form="form-box" name="description" class="text-area-input" placeholder="Description"></textarea>


                <input type="submit" name="submit" class="submit">


            </div>
        </form>

以下是validateForm()方法,因为它可能是问题的原因。

<script>
        function validateForm(){

            var checkForm;


            /*Validating Select*/
            var stream = document.forms["formName"]["select-input"];
            var slctErr = document.getElementById("slctErr");
            if(stream.value == "Wrong"){
                slctErr.innerHTML = "Please select a stream";
                checkForm = false;  
            }
            else{
                slctErr.innerHTML = "";
                checkForm = true;   
            }

            /*----Validating Image Upload----*/
            var files = document.getElementById("img-upload").files;
            var uploadErr = document.getElementById("uploadErr");
            if(files.length > 20){
                uploadErr.innerHTML = "Max 20 photos are allowed.";
                checkForm = false;
            }
            else if(files.length == 0){
                uploadErr.innerHTML = "Upload 1 or more photos.";   
                checkForm = false;
            }
            else{
                uploadErr.innerHTML = "";
                checkForm = checkForm && true;  
            }
            return checkForm;
        }
    </script>

我在name的所有input字段中都有form个属性。

以下是我的notes-uploading.php页面代码段。

/*if someone tries to Direct access of this page*/
 if($_SERVER["REQUEST_METHOD"] != "POST"){
    die('Invalid Access: Page: notes-uploading.php; Line: 9'); 
 }

浏览器打印出来。我删除了这一行,因此显示index未定义,即未收到数据。

问题出在哪里?为什么表单正在提交但不通过POST发送数据。

2 个答案:

答案 0 :(得分:0)

你的checkForm何时返回true数据会发生什么?我没有看到任何函数捕获返回值并将数据发送到您的php我认为这可能是问题。

根据您的反馈,您的代码似乎是以任何方式进行连接,这与我对表单的处理方式有关,我使用单独的脚本进行验证,然后将收集的数据提交给php,将其添加到服务器。

通过这样做,数据将使用get事件提交,方法说POST看下面的2个代码并得到一个想法。

HTML:

<body onLoad="pauseLoad()">
       <div id="divCenter" class="box">
         <form name="clientTimeSheet" id="clientTimeSheet" method="post" enctype="multipart/form-data">
             <label id="userName">Hello <!--User Name--></label>
             <label id="uId" hidden></label>
              <div style="width: 166px; position: absolute; left: 642px; top: 20px; height: 44px;">
                <img src="../images/logo.png" width="142" height="33"></div>
              <br><br>
             <label for="date">Date:</label>
             <label id="date" style="margin-left:43px;"></label>
         <br><br>
      <label for="catSelect">Category:</label>
          <select name="catSelect" id="catSelect" onFocus="" onChange="getClient(this.id); catId(this.id);"  style="margin-left:14px; width:180px">
          </select>
             <input type="text" id="cateId" size="1" hidden>
      <label for="clientSelect" style="margin-left:14px;">Client:</label>
          <select name="clientSelect" id="clientSelect" onChange="clientId(this.id)" style="width:180px;"></select>
             <input type="text" id="clintId" size="1" hidden>
        <br><br>
      <label for="startTime">Start Time:</label>
          <input type="text" name="startTime" id="startTime" style="margin-left:7px;" size="8" readonly>
          <button type="button" name="getStartTime" id="getStartTime" onMouseDown="getSvrTime()"  onMouseUp="startBtnDisable()">Get Time</button>
      <label for="endTime" style="margin-left:42px;">End Time:</label>
          <input type="text" name="endTime" id="endTime" size="8" readonly>
          <button type="button" name="getEndTime" id="getEndTime" onMouseDown="getSvrEndTime()" onMouseUp="endBtnDisable(); timeDifference(endTime, startTime);">Get Time</button>
      <label for="spentTime"  style="margin-left:30px;">Spent Time:</label>
          <input type="text" name="spentTime" id="spentTime" size="4" onFocus="" readonly>
        <br><br>        
      <label for="volume">Volume:</label>
          <input type="text" name="volume" id="volume" size="4" onkeyup="checkNum(this)" style="margin-left:23px;">
      <label for="noPl" style="margin-left: 35px;">No. of Product Lines:</label>
          <input type="text" name="noPl" id="noPl" size="4" onkeyup="checkNum(this)">
      <label for="remarks" style="margin-left:34px;">Remarks</label>
          <input type="text" name="remarks" id="remarks" size="31">      
         <br><br>
             <label id="msgID" hidden></label>
             <div id="msg"></div>
      <div id="sbmBtns">
          <input type="button" value="Reset" name="reset" id="reset" class="btn" onclick="resetForm()">
          <input type="button" value="Submit" name="submit" id="submit" class="btn" onmousedown="pauseLoad2()">
      </div>
    </form>
   </div>
  </body>

JavaScript的:

/**
 * Created by SiNUX on 4/4/2017.
 */
//This will submit all the data to the process PHP to add everything to the data base
function timeSheetAdd(){

    var uId = document.getElementById("uId").innerHTML,
        date = document.getElementById("date").innerHTML,
        cat = document.getElementById("cateId").value,
        clId = document.getElementById("clintId").value,
        sTime = document.getElementById("startTime").value,
        eTime = document.getElementById("endTime").value,
        spTime = document.getElementById("spentTime").value,
        vl = document.getElementById("volume").value,
        nPl = document.getElementById("noPl").value,
        rem = document.getElementById("remarks").value;

    if (cat === "" || clId ==="" || sTime === "" || eTime === "" || spTime === "" || vl === ""){

        document.getElementById("msgID").innerHTML = "2";

    }else if(spTime === "00:00:00"){

        document.getElementById("msgID").innerHTML = "4";

    }else{

        if(window.XMLHttpRequest){

            xmlhttp=new XMLHttpRequest();

        }else{

            xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        }

        xmlhttp.onreadystatechange=function(){

            if(xmlhttp.readyState==4 && xmlhttp.status==200){

                document.getElementById("msgID").innerHTML = xmlhttp.responseText;

            }
        }
        xmlhttp.open("POST","../bin/TimeSheetSubmit.php?uId="+uId+"&jdate="+date+"&cat="+cat+"&clientid="+clId+"&stTime="+sTime+"&enTime="+eTime
            +"&spnTime="+spTime+"&vol="+vl+"&pl="+nPl+"&rem="+rem,true);
        xmlhttp.send();
    }
}

PHP服务器端:

<?php
/**
 * Created by PhpStorm.
 * User: SiNUX
 * Date: 4/5/2017
 * Time: 11:24 AM
 */

include_once("../iConnect/handShake.php");

if($_REQUEST["uId"] != ""){

    if($_REQUEST["pl"] =="" && $_REQUEST["rem"] == ""){

        //This query will add all the data sent from the user front end form
        $addTimeSheetData = "INSERT INTO usertimetrack (jDate, usrId, Category, Client, startTime, endTime, timeSpent, Volume)
VALUES (:jdate,:uId, :cat, :clientid, :stTime, :enTime, :spnTime,:vol)";

        //Binding data to query
        $addTimeSheetQuery = $dbConnect -> prepare($addTimeSheetData);
        $addTimeSheetQuery -> bindParam(':jdate',$_REQUEST["jdate"]);
        $addTimeSheetQuery -> bindParam(':uId',$_REQUEST["uId"]);
        $addTimeSheetQuery -> bindParam(':cat',$_REQUEST["cat"]);
        $addTimeSheetQuery -> bindParam(':clientid',$_REQUEST["clientid"]);
        $addTimeSheetQuery -> bindParam(':stTime',$_REQUEST["stTime"]);
        $addTimeSheetQuery -> bindParam(':enTime',$_REQUEST["enTime"]);
        $addTimeSheetQuery -> bindParam(':spnTime',$_REQUEST["spnTime"]);
        $addTimeSheetQuery -> bindParam(':vol',$_REQUEST["vol"]);

    }elseif ($_REQUEST["rem"] == ""){

        //This query will add all the data sent from the user front end form
        $addTimeSheetData = "INSERT INTO usertimetrack (jDate, usrId, Category, Client, startTime, endTime, timeSpent, Volume, noOfProductLines)
VALUES (:jdate,:uId, :cat, :clientid, :stTime, :enTime, :spnTime,:vol, :pl)";

        //Binding data to query
        $addTimeSheetQuery = $dbConnect -> prepare($addTimeSheetData);
        $addTimeSheetQuery -> bindParam(':jdate',$_REQUEST["jdate"]);
        $addTimeSheetQuery -> bindParam(':uId',$_REQUEST["uId"]);
        $addTimeSheetQuery -> bindParam(':cat',$_REQUEST["cat"]);
        $addTimeSheetQuery -> bindParam(':clientid',$_REQUEST["clientid"]);
        $addTimeSheetQuery -> bindParam(':stTime',$_REQUEST["stTime"]);
        $addTimeSheetQuery -> bindParam(':enTime',$_REQUEST["enTime"]);
        $addTimeSheetQuery -> bindParam(':spnTime',$_REQUEST["spnTime"]);
        $addTimeSheetQuery -> bindParam(':vol',$_REQUEST["vol"]);
        $addTimeSheetQuery -> bindParam(':pl',$_REQUEST["pl"]);

    }elseif ($_REQUEST["pl"] == ""){

        //This query will add all the data sent from the user front end form
        $addTimeSheetData = "INSERT INTO usertimetrack (jDate, usrId, Category, Client, startTime, endTime, timeSpent, Volume, Remarks)
VALUES (:jdate,:uId, :cat, :clientid, :stTime, :enTime, :spnTime,:vol, :rem)";

        //Binding data to query
        $addTimeSheetQuery = $dbConnect -> prepare($addTimeSheetData);
        $addTimeSheetQuery -> bindParam(':jdate',$_REQUEST["jdate"]);
        $addTimeSheetQuery -> bindParam(':uId',$_REQUEST["uId"]);
        $addTimeSheetQuery -> bindParam(':cat',$_REQUEST["cat"]);
        $addTimeSheetQuery -> bindParam(':clientid',$_REQUEST["clientid"]);
        $addTimeSheetQuery -> bindParam(':stTime',$_REQUEST["stTime"]);
        $addTimeSheetQuery -> bindParam(':enTime',$_REQUEST["enTime"]);
        $addTimeSheetQuery -> bindParam(':spnTime',$_REQUEST["spnTime"]);
        $addTimeSheetQuery -> bindParam(':vol',$_REQUEST["vol"]);
        $addTimeSheetQuery -> bindParam(':rem',$_REQUEST["rem"]);


    }else{

        //This query will add all the data sent from the user front end form
        $addTimeSheetData = "INSERT INTO usertimetrack (jDate, usrId, Category, Client, startTime, endTime, timeSpent, Volume, noOfProductLines, Remarks)
VALUES (:jdate,:uId, :cat, :clientid, :stTime, :enTime, :spnTime,:vol, :pl, :rem)";

        //Binding data to query
        $addTimeSheetQuery = $dbConnect -> prepare($addTimeSheetData);
        $addTimeSheetQuery -> bindParam(':jdate',$_REQUEST["jdate"]);
        $addTimeSheetQuery -> bindParam(':uId',$_REQUEST["uId"]);
        $addTimeSheetQuery -> bindParam(':cat',$_REQUEST["cat"]);
        $addTimeSheetQuery -> bindParam(':clientid',$_REQUEST["clientid"]);
        $addTimeSheetQuery -> bindParam(':stTime',$_REQUEST["stTime"]);
        $addTimeSheetQuery -> bindParam(':enTime',$_REQUEST["enTime"]);
        $addTimeSheetQuery -> bindParam(':spnTime',$_REQUEST["spnTime"]);
        $addTimeSheetQuery -> bindParam(':vol',$_REQUEST["vol"]);
        $addTimeSheetQuery -> bindParam(':pl',$_REQUEST["pl"]);
        $addTimeSheetQuery -> bindParam(':rem',$_REQUEST["rem"]);

    }


    if($addTimeSheetQuery -> execute()){
        echo "1";
    }else{
        echo "10";
    }

}else{
    echo "3";
}

答案 1 :(得分:0)

好吧,我发现了这个错误。这是一个非常愚蠢的错误。我检查了我的数据库,数据就在那里,即数据上传,一切都很好。如果die函数在开头,那么为什么要上传data

问题是我notes-uplaoding.php

的最后一行
/*Redirection after uploading*/
header("Location: notes-uploading.php");

问题是,我已经将header函数重定向到自身,即我正在访问我的页面两次,并且它第一次正常工作并上传数据,但在第二次访问时,它不是通过POST来的。 所以只需更改header函数的内容,我的问题就解决了。