输出未定义

时间:2015-11-10 21:54:32

标签: javascript php html undefined

我正在尝试注册到不同的课程,您应该输入您的姓名,电子邮件和课程的下拉框。该信息应该进入.dat文件,其中所有参与者都已注册,但不知何故将它们视为未定义。

<script>
window.onload = startup;

var xmlhttp;

function startup() {
    document.getElementById("save").onclick = save;

    }

function save() {
    var url = "proxy.php?class=" + class + "&name=" + name + "&mail=" + mail;
    var class = document.getElementById("class").value;
    var name = document.getElementById("name").value;
    var mail = document.getElementById("mail").value

    xmlhttp = new XMLHttpRequest();
    xmlhttp.onreadystatechange = status;
    xmlhttp.open("GET", url, true);
    xmlhttp.send();
}

function status(){
    var kurs = document.getElementById("class").value;
    if (xmlhttp.readyState ===4 && xmlhttp.status === 200) {
        document.getElementById("output").innerHTML = "Signed up for" + kurs;
    }
}
</script>   
<body>
        <input id = "name" type="text" placeholder="Name"></input>
        <input id = "mail" type="text" placeholder="Email"></input>
        <form action="proxy.php" method="get">
            <select id="class">
                <option value="812" id="it">SuperIT</option>
                <option value="614" id="mh">MH</option>
            </select>
        </form>
        <button id="save" onclick="save()">Sign up</button>
        <a href="paameldinger.dat">paamelding.dat</a>         // to view the .dat file
        <p id="output"></p>

通过看起来像这样的代理运行它;

<?php

$name = $_GET["name"];
$mail = $_GET["mail"];
$class = $_GET["select"];

$read = fopen("paameldinger.dat", "a");

$line = $class . "¤" . $name . "¤" . $mail . "\n";

fwrite($read,$line);

fclose($read);

?> 

输出最终看起来像这样:

¤undefined¤undefined
¤undefined¤undefined

EDIT;有点把代码转移到英语,因为我愚蠢到用挪威语编写代码,所以可能会在代码中混淆一些挪威语单词,对不起!

1 个答案:

答案 0 :(得分:1)

在定义变量之前使用变量,当然它们都是未定义的。更改您的代码如下:

var class = document.getElementById("class").value;
var name = document.getElementById("name").value;
var mail = document.getElementById("mail").value;

var url = "proxy.php?class=" + class + "&name=" + name + "&mail=" + mail;