I have a php script which allows users to select certain rewards when they reach a certain amount of points. They select the reward they would like using a select
dropdown menu.
<select name="reward" id="reward">
<option value="null">No Reward, save my points.</option>
<option value="1" data-type="percent" data-value="10">Option 1: 10% off [10 points]</option>
<option value="2" data-type="money" data-value="500">Option 2: £5.00 off [15 points]</option>
<option value="4" data-type="money" data-value="1500">Option 3: £15.00 off [45 points]</option>
<option value="5" data-type="percent" data-value="98">Option 4: 98% off [500 points]</option>
</select>
This dropdown works fine, as does the code when the user select a reward (ie. the value of the select is a number), however when they leave their reward option as "No Reward, save my points." I begin to get problems.
If the PHP code, an if statement with the conditional $_POST['reward'] != 'null'
will break PHP, causing it to quit, leaving me with an Error 500 in the browser and the following error in the error logs:
(104)Connection reset by peer: mod_fcgid: error reading data from FastCGI server
Premature end of script headers: index.php
When I change the condition in the if statement to $_POST['reward'] == 'null'
, however, the code seems to run fine, without giving me any errors.
Any ideas?
Using 'null' as one of the values of the select will lead to confusion. Is not the same a string with the value 'null' (as it may have the value 'Lorem Ipsum...' or 'John Doe') than null value.
Instead use any other value and things will be easy and clear. For instance:
<select name="reward" id="reward">
<option value="0">No Reward, save my points.</option>
<option value="1" data-type="percent" data-value="10">Option 1: 10% off [10 points]</option>
<option value="2" data-type="money" data-value="500">Option 2: £5.00 off [15 points]</option>
<option value="4" data-type="money" data-value="1500">Option 3: £15.00 off [45 points]</option>
<option value="5" data-type="percent" data-value="98">Option 4: 98% off [500 points]</option>
</select>
And then, from PHP you can check the value:
if ($_POST['reward'] != '0')
{
// your code here
}