I am making a 'health program' and you get 1 point per 'healthy' thing you do each day, for example, if you have 30 minutes of exercise you get a point. How it works is shown below:
<form method="post">
<label>Did you have 30 minutes of exercise today? (+1)</label>
<select name="question">
<option value="yes">Yes</option>
<option value="no">No</option>
</select> <input type="submit" class="btn btn-outline-light" name="form1">
</form>
<form method="post">
<label>Have you drinken 8 glasses of water today? (+1)</label>
<select name="question">
<option value="yes">Yes</option>
<option value="no">No</option>
</select> <input type="submit" class="btn btn-outline-light" name="form2">
</form>
<form method="post">
<label>Did you use your phone for more than two hours today? (-1)</label>
<select name="question">
<option value="yes">Yes</option>
<option value="no">No</option>
</select> <input type="submit" class="btn btn-outline-light" name="form3">
</form>
<?php
/* Then using PHP I want to have each form submitted, add or remove 1 from an overall score, something like this: */
$score = 0;
if (isset($_POST['form1'])) {
$question = $_POST['question'];
if ($question = "yes") {
// +1 point
$score = +1;
}
}
if (isset($_POST['form2'])) {
$question = $_POST['question'];
if ($question = "yes") {
// +1 point
$score = +1;
}
}
if (isset($_POST['form3'])) {
if ($question = "yes") {
// -1 point
$score = +1;
}
} ?>
Then I want either +1 or -1 from $score. I'm not sure how to do the actual adding of the points using PHP. I want the $score to be updated outside of the if statement otherwise, for each if statement it will add one and $score will still be = to 1
Thanks!
Jacob
</div>
You have to store the data and can do it in different places. Here's some of them:
Within a normal website scope, you'd usually go with a database. Each time you submit the form and process the data in the backend of your application (remember that php cannot be execute on the client), you save the data and use it for subsequent data processing.
It seems to me that javascript would better handle what you're trying to achieve, allowing you to update the interface realtime, bypassing completely the need to send the form to the backend. In this case you can choose to send the total form score to the backend or save it directly to storage accessible from javascript (cookies, local storage, ...). Again, the most common way to do this is as follows:
If it is a must to use php (maybe you are working on an assignment) and cannot use a database, you may want to combine all the answers in one single form:
<form method="post">
<div>
<label>Did you have 30 minutes of exercise today? (+1)</label>
<select name="question0">
<option value="yes">Yes</option>
<option value="no">No</option>
</div>
<div>
</select> <input type="submit" class="btn btn-outline-light" name="form1">
<label>Have you drinken 8 glasses of water today? (+1)</label>
<select name="question1">
<option value="yes">Yes</option>
<option value="no">No</option>
</div>
<div>
</select> <input type="submit" class="btn btn-outline-light" name="form2">
<label>Did you use your phone for more than two hours today? (-1)</label>
<select name="question2">
<option value="yes">Yes</option>
<option value="no">No</option>
</select> <input type="submit" class="btn btn-outline-light" name="form3">
</div>
</form>
// BackendController.php
<?php
$score = 0; // or grab initial value from persistent storage (cookie, db, etc)
$questions = ['question0', 'question1', 'question2'];
foreach($questions as $question) {
$val = $_POST[$question];
$question == 'yes' ? $score++ : $score--;
}
?>
This way you can send and process all parameters in a single shot, and can also shorten the php script. Still, remember that you will lose the data once the connection is closed. In this case, you may want to save the result to session or cookie and use that retrieved value each next request.