在将JavaScript数组传递到我的php文件中需要帮助

时间:2019-07-04 17:19:47

标签: javascript php html

如何将javascript数组传输到我的php文件?我在互联网上看到我可以使用AJAX进行此操作,但是我只是在考虑是否有更简单的方法可以做到这一点。请在下面检查我的代码:

 <html>
 <head>
 <script type = "text/javascript" src = "js/jquery.1.js"></script>
 </head>
 <body>
 <h3>SCENARIO</h3><select name="SCENARIO" type ="text" id = "SCENARIO">
 <option value="WEBSERVER">WEBSERVER</option>
 <option value="APPLICATION">APPLICATION</option>
 </select>
 <h3>DESCRIPTION</h3><input type="text" name="DESCRIPTION" 
id="DESCRIPTION" placeholder ="Check - Status - HTTPD PROCESS">
<h3>COMMAND</h3><input type="text" name="COMMAND" id="COMMAND" 
 placeholder ="./ops_script/check_RPA/check_HTTPD.sh

<button onclick="getTextBoxValues2();">Add</button>


 <table id="taskTable" border="1">
 <tr>
 <th>SCENARIO</th>
 <th>DESCRIPTION</th>
 <th>COMMAND</th>
 </tr>
 </table>

 <button onclick="delete();">Delete</button>


 <script type ="text/javascript">

 function getTextBoxValues2()
 scenario = document.getElementById("SCENARIO").value;
 description = document.getElementById("DESCRIPTION").value;
 command = document.getElementById("COMMAND").value;

 var tasktable = document.getElementById("taskTable");
 var tasktableArray = tasktable.length;
 var row = tasktable.insertRow(tasktableArray);

 var cell_0 = row.insertCell(0);
 var cell_1 = row.insertCell(1);
 var cell_2 = row.insertCell(2);

 cell_0.innerHTML = scenario;
 cell_1.innerHTML = description;
 cell_2.innerHTML = command;

 var x = document.getElementById("taskTable");
 var taskDefinitionArray = [];

 for (var r = 1, n = x.rows.length; r < n; r++) {
 for (var c= 1, m = x.rows[r].cells.length; c < m; c++){

 taskDefinitionArray.push(x.rows[r].cells[c].innerHTML);
 }
 }                    
    }
        }


  </script>
  <script>
   function delete(){
   var rowCount = taskTable.rows.length;
   for (var i =rowCount -1; i >0; i--){
   taskTable.deleteRow(i);
    }


    }

    </script>
    </body>
    </html>

我的目标是将名为taskDefinitionArray的数组变量传输到名为InsertManual.php的PHP文件中。在这种情况下,我有两个名为getTextBoxValues2和delete的函数。如果您知道将其传输到php文件的简单方法,请告诉我,谢谢!

1 个答案:

答案 0 :(得分:1)

您可以使用AJAX,进行此操作 通过JSON.stringify()

将JSON格式的js数组转换为
function ProcessAJAXRequest()
{
    $.ajax
    ({
        type: "POST",
        url: "myphpfile.php",
        data: {"myJSArray" : JSON.stringify(myArray)},
        success: function (data) 
        {
            alert(data);
        }
    });
}

然后在PHP中使用json_decode()函数获取数组中的值

json_decode($_POST["myJSArray"]);