0

I have a form that the data is submitted to PHP for processing. The form consists of 2 form fields that require a numeric value. Field 1 is Number of employees in let’s say Ohio, and the second is Number of employees nationwide. When the form is submitted and the data processed, it determines the correct program for the client based off of their answers and sends them to a page with program details.

The form data submits and is processed, but I am having these problems. In most cases the first form field will contain data and the second will be empty, or sometimes vice versa. If any field is empty, or if it contains a 0 it processes but the data in the field containing data is ignored and the user is sent to the first if statement URL.

The last elseif statement, no matter what number is entered, it is processed by the first if statement. It has been a long time since I written even a small PHP script, and I am probably missing something simple, but I don’t see it.

<?php
$product1 = $_REQUEST['product1'] && $product2 = $_REQUEST['product2'];

if (($product1 <= 50) && ($product2 <= 50))
{echo "<script type='text/javascript'>location.href='http://yahoo.com/'</script>";}

elseif (($product1 >= 51 && $product1 <=100) && ($product2 >= 51 && $product2 <=100))
{echo "<script type='text/javascript'>location.href='http://msn.com/'</script>";}

elseif (($product1 >= 101 && $product1 <= 200) && ($product2 >= 101 && $product2 <= 200))
{echo "<script type='text/javascript'>location.href='http://google.com/'</script>";}

elseif (($product1 >= 201 && $product1 <= 249) && ($product2 >= 201 && $product2 <= 249))
{echo "<script type='text/javascript'>location.href='http://aol.com/'</script>";}

elseif (($product1 > 250) && ($product2 > 250))
{echo "<script type='text/javascript'>location.href='http://tigerdirect.com/'</script>";}
?>

The form being submitted

<form id="form1" name="form1" method="post" action="http://xxxxttt.com/Untitled-1.php">
  <label>Product 1</label><input type="text" name="product1" id="product1" />
  <label>Product 2</label><input type="text" name="product2" id="product2" />
  <input type="submit" name="submit" id="submit" value="submit" />
</form>
2
  • Use empty() with a conditional statement then. Even using isset() will help. Commented Aug 25, 2014 at 16:44
  • You are not defining your variables correctly at the top. Each variable should have it's own line, you can't use a conditional statement to define 2 variables in this manner. Commented Aug 25, 2014 at 16:47

2 Answers 2

1

Your use of the && operator here is nonsense – it does not lead to the result you want; go read the manual page about operator precedence to find out why.

Replace

$product1 = $_REQUEST['product1'] && $product2 = $_REQUEST['product2'];

with

$product1 = $_REQUEST['product1'];
$product2 = $_REQUEST['product2'];

(And if you want to do it totally correct, then you check first whether these values in $_REQUEST exist, using isset or empty.)

Sign up to request clarification or add additional context in comments.

Comments

0

Your $_REQUEST assignment line is being executed as

$product1 = ($_REQUEST['product1'] && $product2 = $_REQUEST['product2']);

Note the () positioning. In other words, $product1 becomes the result of the boolean && operation. If both $_REQUEST parameters have non-falsy values, then $product1 becomes boolean true.

e.g.

php > $x = 'a';
php > $y = 'b';
php > $p = $x && $q = $y;
php > var_dump($p);
bool(true)

You should have them as separate assignments:

$product1 = $_REQUEST['product1'];
$product2 = $_REQUEST['product2'];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.