Home > Net >  foreach() argument must be of type array|object, null given in
foreach() argument must be of type array|object, null given in

Time:04-05

<!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";
}





?>

output: Warning: Undefined array key "seçici1" in C:\xampp\htdocs\phpvideo\Projem\dosyam.php on line 5

Warning: foreach() argument must be of type array|object, null given in C:\xampp\htdocs\phpvideo\Projem\dosyam.php on line 6

Warning: Undefined array key "Meyve" in C:\xampp\htdocs\phpvideo\Projem\dosyam.php on line 11

Warning: foreach() argument must be of type array|object, null given in C:\xampp\htdocs\phpvideo\Projem\dosyam.php on line 13

Warning: Undefined variable $deger in C:\xampp\htdocs\phpvideo\Projem\dosyam.php on line 21

Warning: Undefined variable $deger in C:\xampp\htdocs\phpvideo\Projem\dosyam.php on line 26 Yanlış değer

Plss help mee

CodePudding user response:

at the initial loading of the page, no $_POST is set yet so you need to guard it's logic with isset() condition, so we can update it and add the condition to look like that

<?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";
        }
    }
}

?>

CodePudding user response:

it seem like a page call on itself

for avoir the warnning errors on the landing on the page u can use the function isset() for avoid those probleme

example

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