1
<a href="#" class="button">Etiam posuere</a> </div>

我怎样才能使这个调用 php 使用?因为我希望它在 php 中执行此操作:

所以按钮正在读取这个 php,或者它可以做和这个一样的事情。

if (isset($_POST['do1'])){
    $klasse = $_POST['antall'];
    $klasse = strtolower($klasse);
    $dato = $_POST['dato'];
    $fag = $_POST['fag'];
    $tema = $_POST['tema'];
    $info = $_POST['info'];

    if ($info == "") {
        $info = "ukjent";
    }
    if ($tema == "") {
        $tema = "ukjent";
    }
    $exists = file_exists("proveload/prove$klasse.txt");
    if(!$exists) {
        $ourFileName = "proveload/prove$klasse.txt";
        $ourFileHandle = fopen($ourFileName, 'w') or die("Noe er feil med filen");
        $stringData = "$dato $fag ($tema , $info).";
        fwrite($ourFileHandle, $stringData);
        fclose($ourFileHandle);
        echo "lagde filen prove$klasse.txt ";
    } else {
        $file = "proveload/prove$klasse.txt"; 
        $data = "$dato $fag ($tema , $info).\n";
        $fp = fopen($file, "a", "\n") or die("Couldn't open $file for writing!");
        fwrite($fp, $data) or die("Couldn't write values to file!"); 
    }
}

索引.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<!--


-->
<html xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title></title>
        <meta name="keywords" content="" />
        <meta name="description" content="" />
        <link href="http://fonts.googleapis.com/css?family=Source+Sans+Pro:200,300,400,600,700,900" rel="stylesheet" />
        <link href="default.css" rel="stylesheet" type="text/css" media="all" />
        <link href="fonts.css" rel="stylesheet" type="text/css" media="all" />
        <!--[if IE 6]><link href="default_ie6.css" rel="stylesheet" type="text/css" /><![endif]-->
</head>
<body>
    <div id="header-wrapper">
        <div id="header" class="container">
        <div id="menu">
            <ul>
            <li class="current_page_item">
                        <a href="#" accesskey="1" title="">
                        Legg inn prøver
                        </a>
                    </li>
            </ul>
        </div>
        <div id="logo">
            <h1>
                    <a href="#"></a>
                </h1>
        </div>
        </div>
    </div>
    <div id="wrapper1">
        <div id="welcome" class="container">
        <div class="title">
            <h2>Få oversikt over prøvene du skal ha!</h2>
            <span class="byline">
                    Her kan du legge inn prøvene du skal ha. Disse vil bli sortert i riktig rekkefølge, og den nermeste prøven du skal ha vil bli vist i programmet
                </span>
            </div>
        <div class="content">
                <tr>
                    <td class=mainTxt colspan=2><br></td>
                </tr>
                <tr>
                    <td class=mainTxt><br>klasse: (feks 3pbc)<br></td>
                    <td class=mainTxt><input type=text name=antall><br></td>
                </tr>
                <tr>
                    <td class=mainTxt><br>fag:<br></td>
                    <td class=mainTxt><input type=text name=fag><br></td>
                </tr>
                <tr>
                    <td class=mainTxt><br>dato (feks 24.09.2013)<br>:</td>
                    <td class=mainTxt><input type=text name=dato><br></td>
                </tr>
                <tr>
                    <td class=mainTxt><br>tema:<br></td>
                    <td class=mainTxt><input type=text name=tema><br></td>
                </tr>
                <tr>
                    <td class=mainTxt><br>har du programfag? hvis ja, hvilket?:<br></td>
                    <td class=mainTxt><input type=text value=ingen name=programfag><br></td>
                </tr>
                <tr>
                    <td class=mainTxt><br>annet:<br></td>
                    <td class=mainTxt><input type=text name=info><br></td>
                </tr>

            </div>      
        </div>
    </div>
</body>
</html>

<?

  print <<<ENDHTML

<form method="post" action="">
    <tr>
        <td class=mainTxt colspan=2><input type="submit" align="center" value="ferdig" name="do1"></td>
    </tr>
</form>

ENDHTML;
<?

<?
if (isset($_POST['do1'])){
    $klasse = $_POST['antall'];
    $klasse = strtolower($klasse);
    $dato = $_POST['dato'];
    $fag = $_POST['fag'];
    $tema = $_POST['tema'];
    $info = $_POST['info'];

    if ($info == "") {
        $info = "ukjent";
    }
    if ($tema == "") {
        $tema = "ukjent";
    }
    $exists = file_exists("proveload/prove$klasse.txt");
    if(!$exists) {
        $ourFileName = "proveload/prove$klasse.txt";
        $ourFileHandle = fopen($ourFileName, 'w') or die("Noe er feil med filen");
        $stringData = "$dato $fag ($tema , $info).";
        fwrite($ourFileHandle, $stringData);
        fclose($ourFileHandle);
        echo "lagde filen prove$klasse.txt ";
    } else {
        $file = "proveload/prove$klasse.txt"; 
        $data = "$dato $fag ($tema , $info).\n";
        $fp = fopen($file, "a", "\n") or die("Couldn't open $file for writing!");
        fwrite($fp, $data) or die("Couldn't write values to file!");     
    }
}
?>
4

3 回答 3

2

更好的解决方案是制作一个传统和标准的表单+提交按钮。Buuuuuuut,如果您仍想以这种方式进行操作,则应使用 onClick 属性并调用 ajax 请求。你会在谷歌上找到很多关于它的信息。

于 2013-09-24T20:55:53.353 回答
2

HTML 文件在客户端(即用户浏览器)上运行,php 在服务器上运行。你需要让这些一起工作。

您可以使用链接,就像您拥有的一样,并将 href 设置为指向您的 php 文件。但是,您不能使用此方法轻松发送数据;您的 php 需要表单数据,因此如果您使用此解决方案,则 php 需要编辑。或者,您可以使用 HTML 表单。您可以在此处了解如何执行此操作:https ://developer.mozilla.org/en-US/docs/Web/Guide/HTML/Forms 。这是我会为您的场景推荐的解决方案

最后,您可以使用 Ajax。然而,这更难,至少需要基本的 JavaScript 知识。因此,这不是我建议您目前考虑的事情

于 2013-09-24T20:59:32.213 回答
1

当然,这不会起作用。使按钮在 php 中工作的最简单方法是:

echo <<<EOT
<form action="index.php" method="post"> 
<input type="submit" value="submit">
</form> 
EOT;
于 2013-09-24T21:54:26.797 回答