使用xmlhttprequest从表单中发布数组

时间:2014-06-25 16:34:16

标签: php forms xmlhttprequest

我有一个php生成的表/表单,其复选框如下:

    if ($query) {
if (!mysqli_num_rows($query)) {
    //Empty storage
    echo "There are no items in '$storage'.";
    exit();
} else {
    //form
    echo "<form name='send_parts_form' action='../includes/generatetab.php' method='post'>";
    //Table header
    echo "
        <table>
        <tr>
            <th>Part</th>
            <th>PN</th>
            <th>Manufactured</th>
            <th>Serial</th>
            <th>Site</th>
            <th>Date replaced</th>
            <th>By user</th>
            <th>Faulty</th>
            <th>Send</th>
            <th>Select</th>
        </tr>";
    //Retrieved data
    while ($row = mysqli_fetch_array($query)) {
        echo "<tr>";
        echo "<td>" . $row['part_type'] . "</td>";
        echo "<td>" . $row['pn'] . "</td>";
        echo "<td>" . $row['manufactured'] . "</td>";
        echo "<td>" . $row['serial'] . "</td>";
        echo "<td>" . $row['site_id'] . "</td>";
        echo "<td>" . $row['date'] . "</td>";
        echo "<td>" . $row['user'] . "</td>";
        echo "<td>" . $row['faulty'] . "</td>";
        echo "<td><input type='checkbox' name='send_parts[]' class='checkclass' value=" . $row['id'] . "></td>";
        echo "</tr>";};
    echo "</table>";
    echo "</br>";
    echo "</br>";
    echo "<input type='button' onclick='sendToZG()' value='Send'/>";
    echo "</br>";
    echo "<input type='submit' name='submit' value='Generate tab' />";
    echo "</form>";
    exit();
}
    } else {
    die("Query failed");
   }

用户然后检查他们想要的选项,并在提交(生成选项卡)时,他们获得带有选定值的制表符分隔文本。 我现在想要点击&#34;发送&#34;将值发布到另一个php页面并在同一页面上返回结果(在SentList div下)。我有这样的js:

    //Browser Support Code
    function sendToZG(){
var ajaxRequest;  // The variable that makes Ajax possible!

    try{
    // Opera 8.0+, Firefox, Safari
    ajaxRequest = new XMLHttpRequest();
    }catch (e){
    // Internet Explorer Browsers
    try{
    ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
    }catch (e) {
    try{
     ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
    }catch (e){
     // Something went wrong
     alert("Your browser broke!");
     return false;
    }
    }
    }

    ajaxRequest.onreadystatechange = function(){
    if(ajaxRequest.readyState == 4 && ajaxRequest.status == 200){
    var ajaxDisplay = document.getElementById('SentList');
    ajaxDisplay.innerHTML = ajaxRequest.responseText;
    }
    }

    // Now get the value from page and pass it to server script.
    var formData = new FormData( document.getElementsByName("send_parts")[0] );
    ajaxRequest.open('POST', "../includes/sendtozg.php", true);
    ajaxRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    ajaxRequest.send("send_parts=" + formData);
    }

编辑:ajaxRequest.send(&#34; send_part =&#34; + formData);到ajaxRequest.send(&#34; send_parts =&#34; + formData); 现在它返回:

第53行为foreach()提供的参数无效(这是我在sendtozg.php中获取数据的地方)。

我会在帖子的末尾添加sendtozg.php。

如果不是:

    <form name='send_parts_form' action='../includes/generatetab.php' method='post'>

我回应:

    <form name='send_parts_form' action='../includes/sendtozg.php' method='post'>

提交后,脚本sendtozg.php可以在不同的页面上正常执行。

所以基本上我试图做的是为php生成的表单提供2个选项:

  
      
  1. 生成制表符分隔的txt文件
  2.   
  3. 执行sendtozg.php并在同一页面上返回结果
  4.   

我已经有了两个脚本(generatetab.php和sendtozg.php),它们运行正常。

sendtozg.php:

    if (!empty($_POST['send_parts'])){
foreach ($_POST['send_parts'] as $send_parts){
$getchecked = mysqli_query($con, "SELECT * FROM $storage WHERE id=$send_parts");
while ($row = mysqli_fetch_array($getchecked)) {
    $copypart = mysqli_query($con, "INSERT INTO sent_parts (part_type, pn, manufactured, serial, site_id, date, user, faulty, log)
                                    SELECT part_type, pn, manufactured, serial, site_id, date, user, faulty, log 
                                    FROM $storage WHERE id=$send_parts");                                       

    // check to see if it copied
    $getserial = mysqli_query($con, "SELECT serial FROM $storage WHERE id=$send_parts");
    $getserial_row = mysqli_fetch_array($getserial, MYSQLI_NUM);
    $foundserial = $getserial_row[0];
    $checkcopy = mysqli_query($con, "SELECT id FROM sent_parts WHERE serial = '$foundserial'");

    // add user info and date
    $addinfo = mysqli_query($con, "UPDATE sent_parts SET sent2zg='$user', date2zg='$strdate' WHERE serial = '$foundserial'");

    if (!$check1_res) {
    printf("Error: %s\n", mysqli_error($con));
    exit();
    };

    //delete from storage
    if($checkcopy > 0) {
        $getpart = mysqli_query($con, "SELECT part_type FROM sent_parts WHERE serial='$foundserial'");
        $getpart_row = mysqli_fetch_array($getpart, MYSQLI_NUM);
        $deletedpart = $getpart_row[0];
        $removepart = mysqli_query($con, "DELETE FROM $storage WHERE id = '$send_parts'");
        echo "Part " . $deletedpart . " has been transfered";
    } else {
        echo "Part " . $row['part_type'] . "was NOT transfered";
    };      
};
    } exit ();
    } else {
echo "Nothing was selected, please try again!";
    }

2 个答案:

答案 0 :(得分:0)

您的<form>上没有id属性,因此您需要将id="send_parts"添加到<form>或者您是;我需要将您的代码从getElementById更改为getElementsByName,如下所示:

// Now get the value from page and pass it to server script.
// Serialize the data
var formData = new FormData( document.getElementsByName("send_parts")[0] );
ajaxRequest.open('POST', "../includes/sendtozg.php", true);
ajaxRequest.send(formData);

然后在sendtozg.php中你需要将前两行更改为:

if (!empty($_POST)){
  foreach ($_POST as $send_parts){

答案 1 :(得分:0)

这是sendtozg.js的最终代码:

// Get get the value from page and pass it to server script.
var formData = new FormData( document.getElementsByName("send_parts")[0] );

ajaxRequest.open('POST', "../includes/sendtozg.php", true);
ajaxRequest.setRequestHeader("X-Requested-With", "XMLHttpRequest");
ajaxRequest.send(formData);
}

和sendtozg.php应该是:

    if (!empty($_POST)){
    foreach ($_POST['send_parts'] as $send_parts){
    $getchecked = mysqli_query($con, "SELECT * FROM $storage WHERE id=$send_parts");

顺便说一下:

    print_r ($some_array)

    if (!$check1_res) {
    printf("Error: %s\n", mysqli_error($con));
    exit();
    };

是用于故障排除的绝佳工具。