按下按钮时运行功能并插入表格

时间:2013-09-08 22:42:04

标签: php javascript html mysql ajax

所以我有这个表单,我想在其中添加一个使函数运行的按钮。

这就是我的表单的样子:

<form  action="" method="POST"> 
CVR-nummer:<br> 
<input name="cvr" id="cvr" type="text" value="" size="30"/> <button onclick="<?php getinfo(); ?>">Run the function</button><br> 
Navn:<br> 
<input name="navn" type="text" value="<?php if(isset($navn)) {echo $navn;} ?>" size="30"/><br>
Adresse:<br> 
<input name="adresse" type="text" value="<?php if(isset($adresse)) {echo $adresse;} ?>" size="30"/><br>
Postnummer:<br> 
<input name="postnr" type="text" value="<?php if(isset($postnr)) {echo $postnr;} ?>" size="30"/><br>
By:<br> 
<input name="by" type="text" value="<?php if(isset($by)) {echo $by;} ?>" size="30"/><br>
<input type="submit" value="Registrer"/> 
</form>

这是我使用API​​的函数

<?php 
function getinfo() {
    $cvr = "12345678";
    $api = json_decode(file_get_contents("http://cvrapi.dk/{$cvr}/"),true);
    $navn = $api['navn'];
    $adresse = $api['adresse'];
    $postnr = $api['postnr'];
    $by = $api['by'];
}

?>

当我点击我希望它的按钮时: 1.获得cvr-field的价值。 2.验证cvr-field,使其正好有8个数字。 3.如果验证确定,则必须设置变量。 4.变量必须以表格形式显示。

关于如何做到这一点的任何想法?

更新

<script type="text/javascript">
function checkCVR()
{
    var cvr = document.getElementById("cvr").value;
    var reg = new RegExp("^[0-9]{8}$");
    if(!reg.test(cvr))
    {
        document.getElementById("cvr_error").style.display = 'block';
        document.getElementById("cvr_error").innerHTML = "CVR Error!";
    }
    else
    {
        document.getElementById("cvr_error").style.display = 'none';
        // var url = "http://cvrapi.dk/"+cvr+"/";
        // var url = "http://haxify.org/result.php";
        var url = "json.txt";

        var xmlhttp = new XMLHttpRequest();
        xmlhttp.open("GET", url, true);
        xmlhttp.onreadystatechange = function () {
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200)
            {
                var json = JSON.parse(xmlhttp.responseText);
                document.getElementById("navn").value = json["navn"];
                document.getElementById("adresse").value = json["adresse"];
                document.getElementById("postnr").value = json["postnr"];
                document.getElementById("by").value = json["by"];
            }
        };
        xmlhttp.send(null);
    }
}
</script>
<form  action="" method="POST"> 
    CVR-nummer:<br> 
    <input name="cvr" id="cvr" type="text" value="" size="30"/>
    <input type="button" onClick="checkCVR()" value="Run the function" /><br />
    <span id="cvr_error" style="color:red;display:none;"></span>
    Navn:<br />
    <input name="navn" id="navn" type="text" value="" size="30"/><br />
    Adresse:<br />
    <input name="adresse" id="adresse" type="text" value="" size="30"/><br />
    Postnummer:<br />
    <input name="postnr" id="postnr" type="text" value="" size="30"/><br />
    By:<br />
    <input name="by" id="by" type="text" value="" size="30"/><br />
    <input type="submit" value="Registrer"/> 
</form>

这个实际上可以在一个网站上运行,但不能在我自己的网站上运行。对于任何软件或其他东西是否有任何要求,或者在我的特定网站上不能使用什么内容?

测试: http://haxify.org/form_v4.php

0 个答案:

没有答案