Search code examples
phpformspostisset

PHP isset post condition doesn't execute


I have a form that submits on change using post method, i think i'm not using the isset function well because on submit nothing changes on my page where i should be having poducts showing up on my page.

This is my form :

<form method="post" style="margin-left:5px; margin-right: 5px; margin-bottom: 10px;" name="myform">
  <select size="1" onblur="this.size = 1" onchange="myform.submit();" class="form-control" id="filterText">
    <option selected value="">Cat  gories</option>
    <option name="sofa" value="sofa">Canap  s</option>
    <option name="bed" value="bed">Lits</option>
  </select>
</form>

and this is the isset conditions that come after the form on the same file :

if(isset($_POST["sofa"])){
    $url="https://sik.search.blue.cdtapps.com/fr/fr/product-list-page/more-products?sessionId=22037cc5-f73c-4f71-a1d4-a90285da1789&category=fu003&sort=RELEVANCE&start=0&end=275&c=lf&v=20201118";
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_URL, $url);
    $result = curl_exec($ch);
    $products = json_decode($result, true);
    curl_close($ch);
    echo '<div id="wrap" class="wrap">';

     foreach ($products['moreProducts']['productWindow'] as $items)
     {
        echo '<div class="bed_images" id="bed">';
        echo '<img data-enlargeable class="images" src=' . $items['mainImageUrl'] . '?f=xxs'  . '/>';
        echo '<h2 class="img_title">' . $items['name'] . '</h2>';
        echo '<p class="img_price">' . $items['priceNumeral'] . ' ^b ' . '</p>';
        echo '<p class="img_description">' . $items['typeName'] . '</p>';
        echo '</div>';
     }
     echo '</div>';

} elseif (isset($_POST["bed"])){
    $url="https://sik.search.blue.cdtapps.com/fr/fr/product-list-page/more-products?sessionId=22037cc5-f73c-4f71-a1d4-a90285da1789&category=bm003&sort=RELEVANCE&start=0&end=275&c=lf&v=20201118";
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_URL, $url);
    $result = curl_exec($ch);
    $products = json_decode($result, true);
    curl_close($ch);
    echo '<div id="wrap" class="wrap">';
     foreach ($products['moreProducts']['productWindow'] as $items)
     {
        echo '<div class="bed_images" id="bed">';
        echo '<img data-enlargeable class="images" src=' . $items['mainImageUrl'] . '?f=xxs'  . '/>';
        echo '<h2 class="img_title">' . $items['name'] . '</h2>';
        echo '<p class="img_price">' . $items['priceNumeral'] . ' ^b ' . '</p>';
        echo '<p class="img_description">' . $items['typeName'] . '</p>';
        echo '</div>';
     }
     echo '</div>';
} else {
    $url="https://sik.search.blue.cdtapps.com/fr/fr/product-list-page/more-products?sessionId=22037cc5-f73c-4f71-a1d4-a90285da1789&category=fu001&sort=RELEVANCE&start=0&end=275&c=lf&v=20201118";
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_URL, $url);
    $result = curl_exec($ch);
    $products = json_decode($result, true);
    curl_close($ch);
    echo '<div id="wrap" class="wrap">';

     foreach ($products['moreProducts']['productWindow'] as $items)
     {
        echo '<div class="bed_images" id="bed">';
        echo '<img data-enlargeable class="images" src=' . $items['mainImageUrl'] . '?f=xxs'  . '/>';
        echo '<h2 class="img_title">' . $items['name'] . '</h2>';
        echo '<p class="img_price">' . $items['priceNumeral'] . ' ^b ' . '</p>';
        echo '<p class="img_description">' . $items['typeName'] . '</p>';
        echo '</div>';
     }
     echo '</div>';
}
?>

The else condition is verified as i'm having the last url products shown all the time on my page (before and after form submition) So my problem is i think my mis-understanding of the isset condition.


Solution

  • <option> tag doesn't include the name attribute.

    You have to name your <select> tag and use its value :

    <form method="post" style="margin-left:5px; margin-right: 5px; margin-bottom: 10px;" name="myform">
      <!--    v--------------v----- Check this -->
      <select name="furniture" size="1" onblur="this.size = 1" onchange="myform.submit();" class="form-control" id="filterText">
        <option selected value="">Cat  gories</option>
        <option value="sofa">Canap  s</option>
        <option value="bed">Lits</option>
      </select>
    </form>
    
    if(isset($_POST["furniture"]))
    {
        if ($_POST["furniture"] === "sofa")
        {
            // your code for sofa
        }
    }