PHP 错误 : Notice: Undefined variable

标签 php javascript

<分区>

Possible Duplicate:
PHP: “Notice: Undefined variable” and “Notice: Undefined index”

我希望当我在选择框中选择某个值时多次显示一个字符串,例如,如果我选择值 2,它将显示一个字符串两次。

这是我试过的代码:

<!DOCTYPE HTML>
<html>
<head>
<script>
function checkIt()
{
    var getNum = document.getElementById("numb").value;

    //this_file.php ... im specifying for just. you specify this full code in any of file and specify the whole url path
    location.href="this_file.php?countIt="+getNum;
}
</script>
    <style type="text/css">
    .centrer
    {
        position: absolute;
        left: 50%;
        top: 50%;
        font-size:24px;
    }
    </style>

</head>
<body>
    <select name="nombres" id="numb" onchange="checkIt();">
        <option>1</option>
        <option>2</option>
        <option>3</option>
    </select>

<?php
if($_REQUEST["countIt"])
{
    $displayTimes = $_REQUEST["countIt"]; 
}
?>
   <div class="centrer">
    <?php
        $s = "Hello World! <br/>";
        for($i=0; $i<$displayTimes; $i++)
            echo $s;
    ?>
    </div>
</body>
</html>

但是我有一个问题:

Notice: Undefined variable: displayTimes in C:\Program Files\EasyPHP-12.1\www\site\Untitled-1.php on line 43

注意第 43 行是:for($i=0; $i<$displayTimes; $i++)

还有另一个问题,当我选择“1”值时,它会执行任何操作,而且当我选择其他值时,选择框中的选定值仍然是值“1”,我还有一个问题是没有仅通过使用 PHP 而无需使用 JS 的其他方法?

最佳答案

$displayTimes 只有在满足条件时才会被声明,这是由以下原因引起的:

if($_REQUEST["countIt"]) <------------------- Condition 
{
    $displayTimes = $_REQUEST["countIt"];
}

将上面的替换为:

$displayTimes = isset($_REQUEST["countIt"]) ? $_REQUEST["countIt"] : 0;

关于PHP 错误 : Notice: Undefined variable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12965250/

相关文章:

php - 如何在 Laravel 中正确嵌套 where 子句?

javascript - 如何访问 AngularJS Controller 中由 $resource get() 填充的数组?

javascript - 为什么 javascript 变量打印到控制台时未定义

php - 在 WordPress 中从我的数据库检索数据时出现问题

javascript - AJAX操作页面上的错误处理

javascript - 如何将php变量中的样式代码执行到html textarea

php - 保存与 Yii 有多对多关系的模型

javascript - 如果函数不存在写函数 - javascript

javascript - 使用 php artisan serve 无法在公共(public)文件夹中加载任何 js/css?

javascript - 如何根据页面上不同对象的位置在 Javascript/Mootools 中定位箭头