dqd3690 2015-10-27 23:08
浏览 18

调查卡在一个循环中

I've got the rest of the code working, but it seems like my if statement for age, keeps getting stuck and reruns the statement. I can see in the var_dump that it got the correct value, but it keeps going back to the statement where it says to enter a numeric value.

<?php

session_start();

var_dump($_SESSION);

$isValid = true;

$fullName = "";
$age = "";
$gender = "";



if (isset($_POST) && !empty($_POST))
{
    if (isset($_POST['fullName']) && !empty($_POST['fullName']))    
    { 
        $_SESSION['fullName'] = $_POST['fullName']; 
    }
    else
    {   
        $isValid = false;
        echo "Please fill in your Full Name.";
    } 

    if (isset ($_POST['age']) && !empty($_POST['age'])) 
    { 
        $_SESSION['age'] = $_POST['age']; 

        if (empty($_POST['age']))
        {   
            $isValid = false;
            echo "Please fill in your Age."; 
        }

        else if(!is_numeric($age))
        {
            $isValid = false;
            echo "Must be a numeric value.";
        }
    }
    else
    {
        $isValid = true;
    }

    if (isset($_POST['yourGender']) && !empty($_POST['yourGender']))    
    { 
        $_SESSION['yourGender'] = $_POST['yourGender']; 
    }
    else
    {   
        $isValid = false;
        echo"Please pick your gender"; 
    }

    if ($isValid)
    {
        header('Location: qp2.php');
    }
}

?>

<html>
    <head>
        <title> Questions Page 1</title>
    </head>

<body>

    <form method="post" action="qp1.php"> 

        <!--Name --> 
        Full Name: <input type="text" name="fullName" value="<?php echo $fullName; ?>"/><br/>

        <!-- Email --> 
        Age: <input type="text" name="age" value="<?php echo $age; ?>"/><br/>   
        <br/>

        <!-- How? Dropdown -->
        How did you hear about us?
        <select name="yourGender" value="<?php echo $gender; ?>">
            <option value=""></option>
            <option value="male">Male</option>
            <option value="female">Female</option>
        </select>

        <br/>

        <!-- Submit button --> 
        <input type="button" value="Back" onclick="location.href='index.php'"/>
        <input type="submit" value="Next"/>


    </form>

</body>
</html>

Can anyone tell me what is wrong with my statements?

  • 写回答

1条回答 默认 最新

  • 普通网友 2015-10-27 23:13
    关注

    You branch on if(!is_numeric($age)) without having assigned $age.

    Either assign $age=$_SESSION['age']; or check if(!is_numeric($_SESSION['age']))

    评论

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测