将动态Javascript表单传递给PHP

时间:2014-05-29 09:19:18

标签: javascript php html

我试图将动态创建的JavaScript表单的值(或者几乎只是它的一个select / option字段)传递给另一个php文件。

这是我的request.php的全部代码(碰巧使用php,JavaScript和HTML):

<?php
include ("DbVerbindung.php");
 ?>
<!-- Verbindung zur Datenbank aufbauen -->
<?php
    include "header.php";
 ?>
<!-- Kopfteil des Webfrontends holen -->

    <!-- Hauptinhaltbereich -->

    <div class="float">

        <script>
<!-- dynamische Abfrage für Optionsfeld -->
function showUser(str) {
if (str=="") {
document.getElementById("gang").innerHTML="";
return;
}
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 (xmlhttp.readyState==4 && xmlhttp.status==200) {
document.getElementById("gang").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","getStudiengang.php?q="+str,true);
xmlhttp.send();
}
        </script>

        <h2>Daten des Wählers auswählen</h2>
        <table id="auswahl">
            <!-- Optionen zur Abfrage der Wählerdaten -->
            <form action="speichern.php" method="POST">
                <tr>
                    <td>Fachbereich:</td>
                    <td id="fachbereich">
                    <select size="1" maxlength="20" name="fachbereich" onChange="showUser(this.value)">
                        <option>Fachbereich auswählen</option>
                        <?php $sql = "SELECT * FROM bereich";
                            $result = mysql_query($sql);
                            while ($row = mysql_fetch_array($result)) {
                                echo '<option value="' . $row[0] . '">' . $row[1] . '</option>';
                            }
                        ?>
                    </select>
                </tr>
                <tr>
                    <td>Studiengang:</td>
                    <td id="gang"></td>
                </tr>
                <tr>
                    <td>Geschlecht:</td>
                    <td id="geschlecht">
                    <select size="1" maxlength="20" name="geschlecht">
                        <?php $sql = "SELECT * FROM geschlecht";
                        $result = mysql_query($sql);
                        while ($row = mysql_fetch_array($result)) {
                            echo '<option value="' . $row[0] . '">' . $row[1] . '</option>';
                        }
                        ?>
                    </select></td>
                </tr>
                <tr>
                    <td>Name:</td>
                    <td id="name"><select size="1" maxlength="30" name="name" onClick="getName.p"</td>
                </tr>
                <tr>
                    <td>Wahllokal:</td>
                    <td id="lokal">
                    <select size="1" maxlength="50" name="lokal">
                        <?php $sql = "SELECT * FROM lokal";
                        $result = mysql_query($sql);
                        while ($row = mysql_fetch_array($result)) {
                            echo '<option value="' . $row[0] . '">' . $row[1] . '</option>';
                        }
                        ?>
                    </select></td>

                </tr>
                <tr>
                    <td id="submit">
                        <input type="submit" name="waehlt" value="Wähler wählt..!">
                    </td>
                </tr>
            </form>
        </table>

    </div>

<?php
    include "footer.php";
 ?>

JS脚本使用另一个php文件 - &gt; getStudiengang.php。这是其代码:

<?php
$q = intval($_GET['q']);

include ("DbVerbindung.php");

$sql = "SELECT * FROM studiengang WHERE fs_b = '" . $q . "'";
$result = mysql_query($sql);

echo "<select size='1' name='studiengang'>";

while ($row = mysql_fetch_array($result)) {

    echo '<option value="' . $row[0] . '">' . $row[1] . '</option>';
}

echo "</select">;
?>

最后但并非最不重要的是,php应将值传递给(speichern.php):

if ($_POST[waehlt]) {
        $uhrzeit = date('G:i:s');
        echo "Wähler tritt seine Wahl an. Uhrzeit: $uhrzeit<br>";
        echo "Übergebene Daten:<br>";
        echo "Fachbereich: ";
        $sql = "SELECT * FROM bereich where b_id = '" . $_POST[fachbereich] . "'";
        $result = mysql_query($sql);

        while ($row = mysql_fetch_array($result)) {
            echo "$row[1]<br>";
        }

        echo "Studiengang: ";
        echo $_POST['studiengang'];
        /*$sql = "SELECT * FROM studiengang where s_id = '" . $_POST[studiengang] . "'";
        $result = mysql_query($sql);

        while ($row = mysql_fetch_array($result)) {
            echo "$row[1]<br>";
        }
        */
        echo "Geschlecht: ";
        $sql = "SELECT * FROM geschlecht where g_id = '" . $_POST[geschlecht] . "'";
        $result = mysql_query($sql);

        while ($row = mysql_fetch_array($result)) {
            echo "$row[1]<br>";
        }

        echo "Wahllokal: ";

        $sql = "SELECT * FROM lokal where l_id = '" . $_POST[lokal] . "'";
        $result = mysql_query($sql);

        while ($row = mysql_fetch_array($result)) {
            echo "$row[1]<br>";
        }

    }

请注意除了&#39; studiengang&#39;之外的所有变量。变量(恰好是动态生成的)传递并显示得很好。 任何帮助将不胜感激!

2 个答案:

答案 0 :(得分:0)

您需要使用.appendChild,以便浏览器了解该项目已添加到表单中。要对仍应有效的代码进行最少的修改,

document.getElementById("gang").innerHTML=xmlhttp.responseText;替换为:

var gang = document.getElementById("gang");
while (gang.firstChild) {
    gang.removeChild(gang.firstChild); //clear all elements
}
var div = document.createElement('div'); 
/*make a div to attach the response text to
if you didn't send the select in the responseText, you could createElement('select')*/
div.innerHTML = xmlhttp.responseText;
gang.appendChild(div); //attach the select

答案 1 :(得分:0)

我们终于解决了这个问题。 无论出于何种原因,都必须“硬编码”。已经是HTML中的选择框,并且它不是由Javascript动态创建的。 Javascript现在只在该部分中动态生成选项字段,这实际上解决了这个问题。

已应用更改的最新版本: request.php中的JavaScript:

<script>
        function showUser(str) {
            if (str=="") {
                document.getElementById("gang").innerHTML="";
                return;
            }
            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 (xmlhttp.readyState==4 && xmlhttp.status==200) {
                    document.getElementById("gang").innerHTML=xmlhttp.responseText;
                }
            }
            xmlhttp.open("GET","getStudiengang.php?q="+str,true);
            xmlhttp.send();
        }

        </script>

Request.php(重要的一段代码):

<form action="save.php" method="POST">
    <tr>
        <td>Studiengang:</td>
        <td>
            <select id="gang" size="1" name="studiengang"></select>
        </td>
    </tr>
</form>

getStudiengang.php:

<?php

$q = intval($_GET['q']);

include ("DbVerbindung.php");

$sql = "SELECT * FROM studiengang WHERE fs_b = '" . $q . "'";
$result = mysql_query($sql);

while ($row = mysql_fetch_array($result)) {

    echo '<option value="' . $row[0] . '">' . $row[1] . '</option>';
}


?>

感谢所有帮助。 serakfalcon给出的答案有一个很好的观点,可能会在以后有用。但这不是一个要求。