Hello everyone,
What I'm trying to do is make a form where a person can click one of two buttons and it would decrement or increment a value.
The value would come from a notepad text file so it would be stored somewhere on the server.
I made it in javascript, but then realized I needed the value to stay when I revisited the page. I'm not working with a database either, just text files stored on a server:
Here is my javascript code for what I'm trying to do in php
<script language="JavaScript">
num_Careers = 0 ; // number of careers to be displayed below
</script>
<script language="JavaScript">
function add_Career(){
if(num_Careers > 14)
{ num_Careers = 15; }
else
{ num_Careers = num_Careers + 1;}
document.AddRemoveForm.thistext.value = num_Careers ;
}
</script>
</script>
<script language="JavaScript">
function remove_Career(){
if(num_Careers < 1)
{ num_Careers = 0; }
else
{num_Careers = num_Careers - 1; }
document.AddRemoveForm.thistext.value = num_Careers ;
}
</script>
</head>
<body>
<form name="AddRemoveForm">
<input type="button" name="thisbutton" value="Add Career" onclick ="add_Career()">
<input type="button" name="thisbutton" value="Remove Career" onclick="remove_Career()">
<input type="text" name="thistext" value=0>
</form>
</body>
</html>
I'm just trying to do the same thing, but using php and using a value stored in a text file. I'm still not that great at php so I was wondering if anyone could help?
I haven't gotten that far, right now I'm trying to figure out how to connect the button click to the php code
<?php
// gets current value
if(isset($_POST['output'])){
$value = $_POST['output'];
} else {
$value = 0;
}
if($_POST['Add']){
// opens the file for reading
$read = fopen("num_Careers.txt", 'r');
// stores the file as variable cNumAdd
$cNumAdd = fread($read, 512);
// closes file
fclose($read);
// opens file again
$write = fopen("num_Careers.txt", "w+");
// adds 1 to the data on the file
$cNumAdd = $cNumAdd + 1;
// writes the data into the file
fwrite($write, $cNumAdd);
// closes file
fclose($write);
}
// comments are the same as above except for subtracting 1 from file
if($_POST['Remove']){
$read = fopen("num_Careers.txt", 'r');
$cNumRemove = fread($read, 512);
fclose($read);
$write = fopen("num_Careers.txt", "w+");
$cNumRemove = $cNumRemove - 1;
fwrite($write, $cNumRemove);
fclose($write);
}
?>
<form method="POST" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input type="submit" name="Add" value="Add Career">
<input type="submit" name="Remove" value="Remove Career">
<input type="text" name="output" value="<?php include("num_Careers.txt"); ?>
</form>
*UPDATED: Moved the php code above the form, for some reason, the initial value didn't show when I put it after the form
Got it to increment and decrement, but now I need to figure out how to take the values off a text file.
I'll keep updating the code the more I figure out.
*UPDATED FINAL: I got it to work and commented most of the code. There is probably a more efficient way to do this. For now it works so cheers to me 🙂 , feel free to use it as you please, hope this thread helps someone else, and always glad to contribute to the community.