dtkyayvldeaqhl7151 2014-08-25 16:41
浏览 40

php表单变量处理

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条回答 默认 最新

  • dounan9070 2014-08-25 16:45
    关注

    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.)

    评论

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c