Button to increment contents of html file by one each press -


im creating website chickens have "egg counter". have hidden part of site password protected want put button every time press it, adds 1 egg value. currently, have html file has number in have embedded site.

(this code embeds .html file)

        <h2><b>egg counter!<b/></h2>         <p>so far our chickens have laid</p>         <font size="20" color="#ffffff"><embed src="./eggs.html"></embed></font> 

i'm novice programmer , i'm not sure whether right approach have no idea how make button changes egg value.

any appreciated!

i think want number stored, it's showing , can update , when hipster chickens lay eggs. previous answer allow increment number while have browser open.

i think best way php.

so first of want page .php , lets admin/loggedin.php.

you want create blank file in same directory called eggs.txt , permissions 0755.

then want form

<form method="post" action="loggedin.php">    <input type="text" name="num" value="0"/>    <input type="submit" value="chicken came first" name="eggs"> </form> 

to explain method / action:

method  

is how want send data

action  

page.

options $_post , $_get, don't think going detail here help.

action= tell form page send data to, in case we've chosen send same page form on (loggedin.php).

so once submit form have $_post['num'] available contains number entered form input field.

to this, want add @ top of page: edit (have changed section of code below - file_put_contents should of been inside if($_post['eggs']) {

<?php //this checks see if form submitted checking 'eggs' set. if($_post['eggs']) {   // create nicer variable our egg number, deserves nice name.   $egg = $_post['num'];     //this put new number of eggs in our eggs.txt file    file_put_contents('eggs.txt',$egg); }     ?> 

file_put_contents() writes file

next want able number file on display page. this:

<div class="my_egg_count">    <?php echo file_get_contents('eggs.txt'); ?>  </div> 

file_get_contents() reads file, echo 'echo's' server client side.

let me know if have trouble this, don't want end egg on face.

edit:: try this

if(is_writeable('eggs.txt')) {     if(file_put_contents('eggs.txt',$egg)) {         echo 'file updated';         }else {         echo 'error updating file';      } }else {     echo "error: file isn't writeable [possibly named incorrectly, isn't in correct location, or wrong permissions]";    } 

Comments