dongsimu4422 2016-11-27 20:55
浏览 26

使用javascript嵌入div中的html表单

I am using a HTML form, but some of the input and select boxes are loaded with Ajax and put into a div that appears when certain criteria is met

The thing is when I submit the form the results do not include the data that was loaded from the div, but does include form data that is on the main html page

Please help!

Ajax:

    function showFormData(str) {    
    if (str == "") {
        document.getElementById("showFormData").innerHTML = "";
        return;
    } else {
        if (window.XMLHttpRequest) {
            // code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp = new XMLHttpRequest();
        } else {
            // code for IE6, IE5
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange = function() {
            if (this.readyState == 4 && this.status == 200) {
                document.getElementById("showFormData").innerHTML = this.responseText;
            }
        };
        xmlhttp.open("GET","getFormData.php?model_id="+str,true);
        xmlhttp.send();
        showSpecData(str);
    }
}

Main page:

echo "<form action=\"temp_page_checkformdata.php\" method=\"post\">
";
echo "<select name=\"manufacturers\" onchange=\"showModels(this.value)\">
";
echo "<option selected value=\"\"></option>
";
echo "<option value=\"Test\">Test</option>
";
echo "</select>
";
echo "<div id=\"modelData\"></div><div id=\"showFormData\"></div>
";
echo "</form>
";

getFormData.php

$res = mysql_query("SELECT * FROM formdata WHERE manditory = '1'");
echo "<div id=\"manditorytests\">";
while ($result = mysql_fetch_array($res)) {
echo $result['test_name'] . " " . $result['form_html']. "<br>";
}
echo "</div>";
echo "<div id=\"machinetests\">";
while ($testNames = mysql_fetch_array($getModelFormData)) {
$res = mysql_query("SELECT * FROM formdata WHERE id = '".$testNames['test_id']."' AND manditory = '0'");
while ($result = mysql_fetch_array($res)) {
echo $result['test_name'] . " " . $result['form_html']. "<br>";
}
}
echo "</div>";
echo "<input type=\"submit\" name=\"SubmitButton\" value=\"Submit\">";
  • 写回答

1条回答 默认 最新

  • du67560 2016-11-27 21:08
    关注

    Instead of directing the form to submit the data, have the button click fire a function (preventDefault) and package the data into an object and send that via AJAX. If you are using jQuery you can drop this in as the data payload. If you are using straight Javascript you might have to stringify it and then unpack it on the server. This also gives you a chance to do any input validation prior to sending to the server.

    评论

报告相同问题?

悬赏问题

  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂