2021-03-02

Mar 3 In-Class Exercise.

Post your solution to the Mar 3 In-Class Exercise to this thread.
Best,
Chris
Post your solution to the Mar 3 In-Class Exercise to this thread. Best, Chris
2021-03-03

-- Mar 3 In-Class Exercise
 <!DOCTYPE html>
 <html>
 <head>
 	<meta charset="utf-8"/>
 	<title>Ajay Salh</title>
 </head>
 <body>
 	<form method="post" action="addme.php">
		<label for="num">Number to Add: </label>
		<input type="number" id="num" name="num" placeholder="Enter A Number" />
		<button type="submit" name="add">Add</button>
		</br><button type = "submit" name ="reset">Reset to Zero</button>
   	</form>
   	<?php
   		$a = intval(file_get_contents("current.txt"));
   		if (isset($_POST['add'])) {
   			$a = $_POST['num'] + $a;
   			file_put_contents("current.txt", strval($a));
   		}
   		if (isset($_POST['reset'])) {
			$a = 0;
			file_put_contents("current.txt", strval($a));
		}
   	?>
   	</br><p> Total Added So Far: <?= $a; ?></p>
 </body>
 </html>
(Edited: 2021-03-03)
<!DOCTYPE html> <html> <head> <meta charset="utf-8"/> <title>Ajay Salh</title> </head> <body> <form method="post" action="addme.php"> <label for="num">Number to Add: </label> <input type="number" id="num" name="num" placeholder="Enter A Number" /> <button type="submit" name="add">Add</button> </br><button type = "submit" name ="reset">Reset to Zero</button> </form> <?php $a = intval(file_get_contents("current.txt")); if (isset($_POST['add'])) { $a = $_POST['num'] + $a; file_put_contents("current.txt", strval($a)); } if (isset($_POST['reset'])) { $a = 0; file_put_contents("current.txt", strval($a)); } ?> </br><p> Total Added So Far: <?= $a; ?></p> </body> </html>

-- Mar 3 In-Class Exercise
<!DOCTYPE html> <html> <head> <title>Add Me</title> </head>
<body> <form action="addme.php" method="post">
    <label for="mynum">Number to Add:</label>
<input type="number" id="num" name="mynum">
<input type="submit" value="Submit">
</form>

Total Added So Far: <?php
$file_name = 'current.txt'; $count = 0;
if (file_exists('current.txt')) {
    $count = file_get_contents($file_name);
}
if (isset($_POST['mynum'])) {
    $count = $_POST['mynum'] + $count;
    file_put_contents($file_name, $count);
}
echo $count;
?>
</body> </html>
(Edited: 2021-03-03)
<!DOCTYPE html> <html> <head> <title>Add Me</title> </head> <body> <form action="addme.php" method="post"> <label for="mynum">Number to Add:</label><br> <input type="number" id="num" name="mynum"><br> <input type="submit" value="Submit"> </form> <br> Total Added So Far: <?php $file_name = 'current.txt'; $count = 0; if (file_exists('current.txt')) { $count = file_get_contents($file_name); } if (isset($_POST['mynum'])) { $count = $_POST['mynum'] + $count; file_put_contents($file_name, $count); } echo $count; ?> </body> </html>

-- Mar 3 In-Class Exercise
<pre> <!DOCTYPE html> <html> <head> <title>Add Me</title> </head> <body>
<form action="addme.php" method="post">
  <label for="n1">Number to Add:</label>
<input type="number" id="n1" name="n1"> <input type="submit" value="Add">
</form>

Total Added So Far: <?php
$file_name = 'current.txt'; $count = 0;
if (file_exists($file_name))
    $count = file_get_contents($file_name);
if (isset($_POST['n1'])) {
    $count = $_POST['n1'] + $count;
    file_put_contents($file_name, $count);
}
echo $count;
?> </body> </html>
(Edited: 2021-03-03)
<pre> <!DOCTYPE html> <html> <head> <title>Add Me</title> </head> <body> <form action="addme.php" method="post"> <label for="n1">Number to Add:</label><br> <input type="number" id="n1" name="n1"> <input type="submit" value="Add"> </form> <br> Total Added So Far: <?php $file_name = 'current.txt'; $count = 0; if (file_exists($file_name)) $count = file_get_contents($file_name); if (isset($_POST['n1'])) { $count = $_POST['n1'] + $count; file_put_contents($file_name, $count); } echo $count; ?> </body> </html>

-- Mar 3 In-Class Exercise
  <html>
 <body>
 <h1>Number</h1>
 <form action="#" method ="post">
  <label for="number">Add</label>
  <input type="number" id="number" min="0" max="infinite">
  <input type="submit" id= "submit" value ="Submit">
</form>
<?php
 if(isset($_POST['submit']))
 {
 $number = $_POST['number'];
 $file=fopen("current.txt", "a")
 fwrite($file,"number :");
 fwrite = ($file, $number ."\n");
 fclose($file);
 }
 ?>
 </body>
 </html>
(Edited: 2021-03-03)
<html> <body> <h1>Number</h1> <form action="#" method ="post"> <label for="number">Add</label> <input type="number" id="number" min="0" max="infinite"> <input type="submit" id= "submit" value ="Submit"><br> </form> <?php if(isset($_POST['submit'])) { $number = $_POST['number']; $file=fopen("current.txt", "a") fwrite($file,"number :"); fwrite = ($file, $number ."\n"); fclose($file); } ?> </body> </html>

-- Mar 3 In-Class Exercise
 <!DOCTYPE html>
 <html>
 <body>
 <h1>Add number</h1>
 <p>Choose a number so that I can add it to the count:</p>
 <?php
 function calc_sum($curr_num) {
   $total = 0;
   $total_file = "current.txt";
   if (file_exists($total_file)){
     $total = (int) file_get_contents($total_file);
   }
   else {
     fopen($total_file, 'w') or die("Unable to open file");
   }
   $total = $total + $curr_num;
   echo "<h3>Total Added So Far: ". $total ."</h3>";
   file_put_contents($total_file, $total);
   fclose($total_file);
 }
 ?>
 <form action="addme.php" method="post">
   <label for="NumAdd">Number To Add</label>
   <input type="number" name="Num">
<input type="submit" name="subm" value="Submit"> </form> <?php if (isset($_POST['subm'])) { calc_sum($_POST['Num']); } ?>
 </body>
 </html>
(Edited: 2021-03-03)
<!DOCTYPE html> <html> <body> <h1>Add number</h1> <p>Choose a number so that I can add it to the count:</p> <?php function calc_sum($curr_num) { $total = 0; $total_file = "current.txt"; if (file_exists($total_file)){ $total = (int) file_get_contents($total_file); } else { fopen($total_file, 'w') or die("Unable to open file"); } $total = $total + $curr_num; echo "<h3>Total Added So Far: ". $total ."</h3>"; file_put_contents($total_file, $total); fclose($total_file); } ?> <form action="addme.php" method="post"> <label for="NumAdd">Number To Add</label> <input type="number" name="Num"><br> <input type="submit" name="subm" value="Submit"> </form> <?php if (isset($_POST['subm'])) { calc_sum($_POST['Num']); } ?> </body> </html>

-- Mar 3 In-Class Exercise
<!DOCTYPE html> <head>
    <title>Exercise</title>
</head> <body>
    <?php $sum = 0; ?>
    <form method="get" action="addme.php">
        <label for="numIn">Number To Add:</label>
        <input type="text" name="numberToAdd" size="10" id="numIn"/>
        </br>
        <label for="addButton">Add:</label>
        <input type="submit" name="Add" value="Add" id="addButton"/>
    </form>
    <?php
        if (file_exists("current.txt")) {
            $sum = file_get_contents("current.txt");
        }
        if (isset($_POST["numberToAdd"])) {
            $sum = $_POST["numbertoAdd"] + $sum;
            file_put_contents("current.txt", $sum);
        }
    ?>
    <p>Total Added So Far:<?php echo $sum; ?></p>
</body> </html>
<!DOCTYPE html> <head> <title>Exercise</title> </head> <body> <?php $sum = 0; ?> <form method="get" action="addme.php"> <label for="numIn">Number To Add:</label> <input type="text" name="numberToAdd" size="10" id="numIn"/> </br> <label for="addButton">Add:</label> <input type="submit" name="Add" value="Add" id="addButton"/> </form> <?php if (file_exists("current.txt")) { $sum = file_get_contents("current.txt"); } if (isset($_POST["numberToAdd"])) { $sum = $_POST["numbertoAdd"] + $sum; file_put_contents("current.txt", $sum); } ?> <p>Total Added So Far:<?php echo $sum; ?></p> </body> </html>

-- Mar 3 In-Class Exercise
<!-- place holder, not working --> <!DOCTYPE html> <html> <body>
<form method = "post">
  <label for="field">Add number:</label>
<input type="number" id="field" name="field">
<input type="submit" value="Submit Number">
</form>
Numbers Entered: <?php
 $file_name = 'nums.txt'; $nums = 0;
if (file_exists('nums.txt')) {
    $count = file_get_contents($file_name);
} else {
   $fileHandle = fopen("nums.txt", "w");
   fwrite($fileHandle, $nums);
   fclose($fileHandle);
} if (isset($_POST['number'])) {
    $nums = $_POST['number'] + $nums;
    file_put_contents($file_name, $nums);
} ?>
</body> </html>
(Edited: 2021-03-03)
<!-- place holder, not working --> <!DOCTYPE html> <html> <body> <form method = "post"> <label for="field">Add number:</label><br> <input type="number" id="field" name="field"><br> <input type="submit" value="Submit Number"> </form> Numbers Entered: <?php $file_name = 'nums.txt'; $nums = 0; if (file_exists('nums.txt')) { $count = file_get_contents($file_name); } else { $fileHandle = fopen("nums.txt", "w"); fwrite($fileHandle, $nums); fclose($fileHandle); } if (isset($_POST['number'])) { $nums = $_POST['number'] + $nums; file_put_contents($file_name, $nums); } ?> </body> </html>

-- Mar 3 In-Class Exercise
<!DOCTYPE html> <html lang="en">
    <head>
        <title>In-class exercise</title>
    </head>
    <body>
        <form method="post" action="addme.php">
            <label for="addNumber">Number to Add </label>
            <input type="text" name="addNumber" size="10" />
            <button type="Submit" size="10" name="click">Add</button>
        </form>
<?php
    if (isset($_POST['click'])) {
        $file_name = 'current.txt'; 
        $count = 0;
        if (file_exists($file_name)){
            $count = (int) file_get_contents($file_name);
        }
        else {
            fopen($file_name, 'w'); 
         }
        $count = $count + $_POST['addNumber'];
        echo "<h3>Total Added So Far: ". $count ."</h3>";
        file_put_contents($file_name, $count);
    }
    ?>
    </body>
</html>
(Edited: 2021-03-03)
<!DOCTYPE html> <html lang="en"> <head> <title>In-class exercise</title> </head> <body> <form method="post" action="addme.php"> <label for="addNumber">Number to Add </label> <input type="text" name="addNumber" size="10" /> <button type="Submit" size="10" name="click">Add</button> </form> <?php if (isset($_POST['click'])) { $file_name = 'current.txt'; $count = 0; if (file_exists($file_name)){ $count = (int) file_get_contents($file_name); } else { fopen($file_name, 'w'); } $count = $count + $_POST['addNumber']; echo "<h3>Total Added So Far: ". $count ."</h3>"; file_put_contents($file_name, $count); } ?> </body> </html>

-- Mar 3 In-Class Exercise
<?php
$val = 0;
$fileHandle = fopen("my.txt", "a");
?> 
 
<form method="get" action="addme.php">
   <input type="text" name="my_textfield"
       size="10" />
   <input type="hidden" name="secret_data"
      value="do not peak" />
   <input type="submit" name="sendform"
      value="Add" /> 
 
</form> 
 
<?php
if($_REQUEST){
  $val += $_REQUEST[my_textfield];
  fwrite($fileHandle, $val);
}//if
else{}//else
?> 
 
<p> Total Added So Far: <?php echo $val ?> </p> 
 
<?php
fclose($fileHandle); 
 
(Edited: 2021-03-03)
<pre> <?php $val = 0; $fileHandle = fopen("my.txt", "a"); ?> <form method="get" action="addme.php"> <input type="text" name="my_textfield" size="10" /> <input type="hidden" name="secret_data" value="do not peak" /> <input type="submit" name="sendform" value="Add" /> </form> <?php if($_REQUEST){ $val += $_REQUEST[my_textfield]; fwrite($fileHandle, $val); }//if else{}//else ?> <p> Total Added So Far: <?php echo $val ?> </p> <?php fclose($fileHandle); </pre>
[ Next ]
X