php foreach()参数必须为数组类型|对象,空值给定于

niwlg2el  于 2023-02-07  发布在  PHP
关注(0)|答案(2)|浏览(175)
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Fiyat listesi</title>
</head>
<body>
<form method="post" action="dosyam.php">
    Fiyat listesi: <input name="seçici1[]" type="checkbox" value="Fiyat">
    Gram <input name="seçici1[]" type="checkbox" value="gram">
<br>
   Elma: <input  type="checkbox" name="Meyve[]" value="Elma" ><br>
   Kiraz: <input  type="checkbox" name="Meyve[]" value="Kiraz">
    <input type="submit" name="gonder " value="Anketi Gönder">


</form>
</body>
</html>

<?php


$sec = $_POST["seçici1"];
foreach ($sec as $secici){
    echo $secici;
}

$Meyve = $_POST["Meyve"];

foreach ($Meyve as $deger) {
    echo $deger."<br>";

}

$gram = array("Elma" => "1.000", "Kiraz" => "1.000", "Elma Fiyat" =>"50TL", "Kiraz Fiyat"=>"60TL" );

if ($deger =="Elma"){
    echo $gram["Elma"];

}

if ($deger == "Kiraz"){
    echo $gram["Kiraz"];
}

else{
    echo "Yanlış değer";
}



?>

输出:警告:第5行C:\xampp\htdocs\phpvideo\项目\dosyam.php中的数组键“seçici 1”未定义
警告:foreach()参数必须是数组类型|对象,第6行的C:\xampp\htdocs\phpvideo\项目\dosyam.php中给出了空值
警告:第11行C:\xampp\htdocs\phpvideo\Projem\dosyam.php中的数组键“Meyve”未定义
警告:foreach()参数必须是数组类型|对象,第13行的C:\xampp\htdocs\phpvideo\项目\dosyam.php中给出了空值
警告:第21行C:\xampp\htdocs\phpvideo\Projem\dosyam.php中的变量$deger未定义
警告:第26行C:\xampp\htdocs\phpvideo\Projem\dosyam.php中的变量$deger未定义
请帮帮我

bq9c1y66

bq9c1y661#

在初始加载页面时,还没有设置$_ POST,因此您需要使用isset()条件保护它的逻辑,这样我们就可以更新它并添加条件,使其看起来像这样

<?php


if(isset($_POST["seçici1"])){

    $sec = $_POST["seçici1"];
    foreach ($sec as $secici){
        echo $secici;
    }
}

if (isset($_POST["Meyve"])){
    
    $gram  = array("Elma" => "1.000", "Kiraz" => "1.000", "Elma Fiyat" =>"50TL", "Kiraz Fiyat"=>"60TL" );
    $Meyve = $_POST["Meyve"];

    foreach ($Meyve as $deger) {
        echo $deger."<br>";
        
        if ($deger =="Elma"){
            echo $gram["Elma"];

        }

        if ($deger == "Kiraz"){
            echo $gram["Kiraz"];
        }

        else{
            echo "Yanlış değer";
        }
    }
}

?〉

daupos2t

daupos2t2#

它看起来像是对自己寻呼
为了避免在页面着陆时的警告错误,可以使用函数isset()来避免这些问题。
范例

if(isset($_POST["gonder"])){ // check if submit button is set or not
    // other instruction
}

相关问题