Malin Pedersen
Malin Pedersen

Reputation: 73

HTML button / php

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

how can i make this call php use? since i want it to do this in php:

so the button is reading this php, or so it can do the same as what this does.

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!"); 
    }
}

index.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!");     
    }
}
?>

Upvotes: 1

Views: 694

Answers (3)

Loko
Loko

Reputation: 6679

Of course this wont just work. Easiest way to make a button work in php is:

echo <<<EOT
<form action="index.php" method="post"> 
<input type="submit" value="submit">
</form> 
EOT;

Upvotes: 1

Braders
Braders

Reputation: 445

HTML file runs on the client (ie. the users browser), the php runs on the server. You need to make these work together.

You could use a link, like you have, and set the href to point to your php file. However, you cannot easily send data with this method; your php is expecting form data, so the php would need editing if you use this solution. Alternatively, you can use a HTML form. You can find out how to do this here: https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/Forms. This is the solution I would recomend for your scenario

Finally, you can use Ajax. However, this is harder and requires at least basic JavaScript knowledge. Therefore this is not something I would suggest you look into at this time

Upvotes: 2

The better solution is to make a traditional and standard form+submit buttom. Buuuuuuut if you still want to make it in that way you should use onClick propety and call an ajax request. You will find a LOT of information about it on Google.

Upvotes: 2

Related Questions