0

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?

Joshua Crocker
  • 449
  • 4
  • 15
  • 2
    We're going to need to see more code in order to help you, preferably the actual PHP code you're experiencing issues with. – Ash Apr 05 '16 at 09:57
  • Is your script running for more than 60seconds before it errors? – ʰᵈˑ Apr 05 '16 at 09:58
  • 3
    A string containing `"null"` is confusing with `null` itself; I would suggest using `` to help code clarity. – Egg Apr 05 '16 at 09:59
  • index.php contents wouldn't go a miss, there are loads of things that could cause "Premature end of script headers" error. – Eihwaz Apr 05 '16 at 09:59
  • Change to `!==` instead of `!=`. For the error http://stackoverflow.com/questions/12153518/connection-reset-by-peer-mod-fcgid-error-reading-data-from-fastcgi-server – Chay22 Apr 05 '16 at 09:59

2 Answers2

1

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
}
MarcM
  • 2,173
  • 22
  • 32
-1

Have you tried using another value instead of 'null' ? For instance :

<option value="0">No Reward, save my points.</option>
D14n4
  • 130
  • 6