jquery php文件中的多个表单和文件子目录

时间:2015-09-15 07:22:48

标签: javascript php jquery html ajax

我正在尝试使用jquery提交多个表单..但问题是我收到此错误消息

未定义的索引:第4行的C:\ xampp \ htdocs \ test \ approve_test.php中的box3

我的html / jquery代码:



<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<SCRIPT LANGUAGE="JavaScript" type="text/javascript">
/* Collect all forms in document to one and post it */
function submitAllDocumentForms() {
    var arrDocForms = document.getElementsByTagName('form');
    var formCollector = document.createElement("form");
    with(formCollector)
    {
        method = "post";
        action = "approve_test.php";
        name = "formCollector";
        id = "formCollector";
        processData: false;
        contentType: false;
    }
    for(var ix = 0 ; ix < arrDocForms.length ; ix++) {
        appendFormVals2Form(arrDocForms[ix], formCollector);
    }
    document.body.appendChild(formCollector);
    formCollector.submit();
}

/* Function: add all elements from frmCollectFrom and append them to 
             frmCollector before returning frmCollector*/
function appendFormVals2Form(frmCollectFrom, frmCollector) {
    var frm = frmCollectFrom.elements;
    for(var ix = 0 ; ix < frm.length ; ix++)
        frmCollector.appendChild(frm[ix]);
    return frmCollector;
}
</SCRIPT>
</head>
<body>

<FORM METHOD=POST ACTION="approve_test.php" NAME="form1" id="form1">
    <INPUT TYPE="text" NAME="box1" size="20" >
</FORM>
FORM2:
<FORM METHOD=POST ACTION="approve_test.php" NAME="form2" id="form2">
    <INPUT TYPE="text" NAME="box2" size="20" >
</FORM>
<FORM METHOD=POST ACTION="approve_test.php" NAME="form2" id="form2">
    <INPUT TYPE="file" NAME="box3" >
</FORM>

<INPUT TYPE="button" value="Submit Form all" onClick="submitAllDocumentForms()">

</body>
</html>
&#13;
&#13;
&#13;

和php:

&#13;
&#13;
<?php
    $name=trim($_POST["box1"]);
    $fname=trim($_POST["box2"]);
    $file_name=$_FILES["box3"]["name"];
    echo $name."<br>".$fname."<br>".$filename;
?>
&#13;
&#13;
&#13;

有人可以帮助......我只是一个初学者......

1 个答案:

答案 0 :(得分:0)

这是您想要的代码

HTML部分

<!DOCTYPE html>
   <html>
   <head>
   <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
   <script LANGUAGE="JavaScript" type="text/javascript">            
        function submitAllDocumentForms() {
              $("#aaa").val($('form').serialize());
        }
   </script>
   </head>
   <body>

        <FORM id="form1" method="post">
            <INPUT TYPE="text" NAME="box1" size="20" >
    </FORM>
    FORM2:
    <FORM id="form2" method="post">
        <INPUT TYPE="text" NAME="box2" size="20" >
    </FORM>
    <FORM id="form3" method="post" enctype="multipart/form-data" action="<?= base_url("dummy/aaa") ?>">
        <INPUT TYPE="file" NAME="box3" >
        <input type="hidden" name="a" id="aaa" />
        <INPUT TYPE="submit" value="Submit Form all" onClick="submitAllDocumentForms()">
    </FORM>



</body>
</html>

PHP部分

$arr = explode("&", $_POST['a']);

    foreach ($arr as $item) {
        $array = explode("=", $item);
        $post[$array[0]] = $array[1];
    }

    $file_name = $_FILES["box3"]["name"];
    echo $post['box1']. "<br>" . $post['box2']. "<br>" . $file_name;