1
<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!");     
    }
}
?>
7
  • Could you help me with that? Commented Sep 24, 2013 at 20:46
  • 4
    PHP runs on the SERVER. Your button is on the CLIENT. You need to learn JavaScript. Commented Sep 24, 2013 at 20:47
  • That PHP code is expecting form values. Why not make this a form and have that form submit to that PHP page? Commented Sep 24, 2013 at 20:47
  • form and submit button, standard stuff. why make it difficult? Commented Sep 24, 2013 at 20:52
  • Im only familiar with php, but my new index.html that a friend wanted me to use use html. Commented Sep 24, 2013 at 20:55

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Beat me to it! I still posted it with an example but good explanation! +1
2

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

3 Comments

hmm.. how hard is it to do the code there to php instead of html? i tried also the php method for button, and then call the posts, but it didnt work.
is it any thing i can do? the forms are in php`s, only i need is to do the button to php then, and then using php as i did?
It sounds like you might need a submit button; it takes the form data and sends it to the server
1

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;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.