【问题标题】:PHP isset post condition doesn't executePHP isset 后置条件不执行
【发布时间】:2020-12-16 10:29:09
【问题描述】:

我有一个使用 post 方法提交更改的表单,我认为我没有很好地使用 isset 函数,因为在提交时我的页面上没有任何更改,我应该在我的页面上显示产品。

这是我的表格:

<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>

这是在同一个文件的表单之后出现的 isset 条件:

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

else 条件得到验证,因为我的页面上一直显示最后一个 url 产品(表单提交之前和之后)所以我的问题是我认为我对 isset 条件的误解。

【问题讨论】:

  • 我认为&lt;select&gt; 是表单的一部分。因此,选择名称(如果有的话)位于$_POST 数组中,而不是其选项之一的名称。该值将是选定的值。

标签: php forms post isset


【解决方案1】:

&lt;option&gt; tag doesn't include the name attribute.

你必须命名你的&lt;select&gt; 标签并使用它的值:

<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
    }
}

【讨论】:

  • 是的,正是我的想法。 ?
  • @A_frikha 不客气,我们每天都在学习 :)
  • 我遇到了一个问题,尽管“无法在表达式的结果上使用 isset()”我在网上查看并尝试了多种方法,但我无法使用“访问家具中的价值沙发” ==="
  • 你是如何使用isset 来获得这样的信息的?
  • @A_FRIKHA 请仔细阅读我的代码。 if ($_POST["furniture"] === "sofa"),没有isset()
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-05-19
  • 2017-04-10
  • 1970-01-01
  • 2019-09-22
  • 2015-02-27
  • 2016-11-24
  • 1970-01-01
相关资源
最近更新 更多