I’ve taken this as an extract of a much larger file.
If $score1 is empty the no score message appears, the exit() occurs. and the footer div will not appear.
If $score1 is filled, the success message and the footer div both appear fine.
If I remove the exit, then both the success and the no score message appears, and the footer div displays fine.
I would like the no score message to appear when $score1 is empty, aswell as the footer div to appear.
How should I restructure my code so that is so?
Any basic examples would be great!
<input type="text" name="score1"/>
<input type="text" name="score2"/>
<?php
if(isset($_POST['processForm']))
{
$score1 = $_POST['score'];
if (empty($score1)) {echo "You haven't entered a score for score2"; exit();}
else if (empty($score2)) {echo "You haven't entered a score for score1"; exit();}
#connection here
$sql = "INSERT STATEMENT HERE";
mysql_query($sql);
echo "success message";
};
?>
</form>
<div id="footer">Footer</div>
Pardon me for asking, but where is $score2 value set? I don’t see it. In which case, it’ll always be null (empty). Either way, there’s no reason to ever put a call to exit in your code like that. Show me more of the code, so I can get a better understanding of how/where things are set please.
For example:
<?php
if (isset($_POST['processForm'])) {
$score1 = $_POST['score1'];
$score2 = $_POST['score2'];
// initialize error indicator variable
$errors_found = false;
// check for errors, if an error is found set the indicator to true
if (empty($score1)) {
echo "You haven't entered a score for score2";
$errors_found = true;
}
if (empty($score2)) {
echo "You haven't entered a score for score1";
$errors_found = true;
}
// if no errors have been found, insert the data
// ATTENTION: sanitize the user input before using it in a query,
// or even better: use mysqli_ or pdo instead of mysql_
if (!errors_found) {
#connection here
$sql = "INSERT STATEMENT HERE";
mysql_query($sql);
echo "success message";
}
}
?>
</form>
<div id="footer">Footer</div>
Sorry, I typed that out quickly to use as an example, here is more of the code…
<input type="hidden" name="processForm" value="1" />
<input type="hidden" id="handicap" value="<?php echo $handicap ?>"/>
<div id="course">Course Par: <input type="text" id="par" class="par" name="coursepar" onChange="addition(this.form)"/></div><br />
<input type="text" class="hole" readonly="readonly" value="1"/>
<input type="text" class="hole" readonly="readonly" value="2"/>
<input type="text" class="hole" readonly="readonly" value="3"/>
<input type="text" class="hole" readonly="readonly" value="4"/>
<input type="text" class="hole" readonly="readonly" value="5"/>
<input type="text" class="hole" readonly="readonly" value="6"/>
<input type="text" class="hole" readonly="readonly" value="7"/>
<input type="text" class="hole" readonly="readonly" value="8"/>
<input type="text" class="hole" readonly="readonly" value="9"/><br />
<input type="text" class="in" id="in1" onChange="addition(this.form)" name="score1"/>
<input type="text" class="in" id="in2" onChange="addition(this.form)" name="score2"/>
<input type="text" class="in" id="in3" onChange="addition(this.form)" name="score3"/>
<input type="text" class="in" id="in4" onChange="addition(this.form)" name="score4"/>
<input type="text" class="in" id="in5" onChange="addition(this.form)" name="score5"/>
<input type="text" class="in" id="in6" onChange="addition(this.form)" name="score6"/>
<input type="text" class="in" id="in7" onChange="addition(this.form)" name="score7"/>
<input type="text" class="in" id="in8" onChange="addition(this.form)" name="score8"/>
<input type="text" class="in" id="in9" onChange="addition(this.form)" name="score9"/><br />
<input type="text" class="hole" readonly="readonly" value="10"/>
<input type="text" class="hole" readonly="readonly" value="11"/>
<input type="text" class="hole" readonly="readonly" value="12"/>
<input type="text" class="hole" readonly="readonly" value="13"/>
<input type="text" class="hole" readonly="readonly" value="14"/>
<input type="text" class="hole" readonly="readonly" value="15"/>
<input type="text" class="hole" readonly="readonly" value="16"/>
<input type="text" class="hole" readonly="readonly" value="17"/>
<input type="text" class="hole" readonly="readonly" value="18"/><br />
<input type="text" class="in" id="in10" onChange="addition(this.form)" name="score10"/>
<input type="text" class="in" id="in11" onChange="addition(this.form)" name="score11"/>
<input type="text" class="in" id="in12" onChange="addition(this.form)" name="score12"/>
<input type="text" class="in" id="in13" onChange="addition(this.form)" name="score13"/>
<input type="text" class="in" id="in14" onChange="addition(this.form)" name="score14"/>
<input type="text" class="in" id="in15" onChange="addition(this.form)" name="score15"/>
<input type="text" class="in" id="in16" onChange="addition(this.form)" name="score16"/>
<input type="text" class="in" id="in17" onChange="addition(this.form)" name="score17"/>
<input type="text" class="in" id="in18" onChange="addition(this.form)" name="score18"/><br /><br />
<div id="results">
Your Score is: <input type="text" class="result" readonly="readonly" id="score" input onChange="totalscore(this.form)"/>
Par Score: <input type="text" id="parscore" class="result" readonly="readonly" input onChange="totalscore(this.form)"/>
- Handicap: <input type="text" id="handicapscore" class="result" readonly="readonly" name="finalscore"/><br />
<br />
<?php
if(isset($_POST['processForm']))
{
$coursepar = $_POST['coursepar']; $coursepar = trim($coursepar);
$score1 = $_POST['score1']; $score1 = trim($score1);
$score2 = $_POST['score2']; $score2 = trim($score2);
$score3 = $_POST['score3']; $score3 = trim($score3);
$score4 = $_POST['score4']; $score4 = trim($score4);
$score5 = $_POST['score5']; $score5 = trim($score5);
$score6 = $_POST['score6']; $score6 = trim($score6);
$score7 = $_POST['score7']; $score7 = trim($score7);
$score8 = $_POST['score8']; $score8 = trim($score8);
$score9 = $_POST['score9']; $score9 = trim($score9);
$score10 = $_POST['score10']; $score10 = trim($score10);
$score11 = $_POST['score11']; $score11 = trim($score11);
$score12 = $_POST['score12']; $score12 = trim($score12);
$score13 = $_POST['score13']; $score13 = trim($score13);
$score14 = $_POST['score14']; $score14 = trim($score14);
$score15 = $_POST['score15']; $score15 = trim($score15);
$score16 = $_POST['score16']; $score16 = trim($score16);
$score17 = $_POST['score17']; $score17 = trim($score17);
$score18 = $_POST['score18']; $score18 = trim($score18);
$date = $_POST['date'];
if (empty($coursepar)) {echo "You haven't entered a course par"; exit();}
else if (empty($date)) {echo "You haven't entered a date"; exit();}
else if (empty($score1)) {echo "You haven't entered a score for Hole 1"; exit();}
else if (empty($score2)) {echo "You haven't entered a score for Hole 2"; exit();}
else if (empty($score3)) {echo "You haven't entered a score for Hole 3"; exit();}
else if (empty($score4)) {echo "You haven't entered a score for Hole 4"; exit();}
else if (empty($score5)) {echo "You haven't entered a score for Hole 5"; exit();}
else if (empty($score6)) {echo "You haven't entered a score for Hole 6"; exit();}
else if (empty($score7)) {echo "You haven't entered a score for Hole 7"; exit();}
else if (empty($score8)) {echo "You haven't entered a score for Hole 8"; exit();}
else if (empty($score9)) {echo "You haven't entered a score for Hole 9"; exit();}
else if (empty($score10)) {echo "You haven't entered a score for Hole 10"; exit();}
else if (empty($score11)) {echo "You haven't entered a score for Hole 11"; exit();}
else if (empty($score12)) {echo "You haven't entered a score for Hole 12"; exit();}
else if (empty($score13)) {echo "You haven't entered a score for Hole 13"; exit();}
else if (empty($score14)) {echo "You haven't entered a score for Hole 14"; exit();}
else if (empty($score15)) {echo "You haven't entered a score for Hole 15"; exit();}
else if (empty($score16)) {echo "You haven't entered a score for Hole 16"; exit();}
else if (empty($score17)) {echo "You haven't entered a score for Hole 17"; exit();}
else if (empty($score18)) {echo "You haven't entered a score for Hole 18"; exit();}
$year =substr($date,0,4);
#connection here
$connect_solning = mysql_connect($hostname_connect, $username_connect, $password_connect) or trigger_error(mysql_error(),E_USER_ERROR);
@mysql_select_db($database_connect) or die (mysql_error());
$location = mysql_real_escape_string($location);
$sql = "INSERT INTO 18scorecard
(user_id, date, finalscore, competition, year)
VALUES ('$user_id', '$_POST[date]', '$_POST[finalscore]', '$_POST[competition]', '$year' )";
mysql_query($sql);
echo "A scorecard, has now been added.<br/>You can use the form again to enter a new card";
};
?>
</form>
</div>
<div id="footer">Footer</div>
First, take a look at the previous response by Guido. It demonstrates more appropriate logic. Second, I would probably not Exit() on first encountered error, but rather, keep a tally, like so.
$errors = array();
if ($condition1) { $errors[] = 'error message 1'; }
if ($condition2) { $errors[] = 'error message 2'; }
Then later…
if (count($errors) > 0)
{
display_errors(); // echo out a ul or something
}
else
{
commit_form(); // commit and redirect to new page
}
If you want to, you can use the error index to match a message with a field, and list the message near the field itself, rather than creating a list at the page top. Which ever suits you.