【问题标题】:isset is not working on submitisset 无法提交
【发布时间】:2017-12-07 20:45:20
【问题描述】:

这是一个 5 星评级表,我正在努力让它发挥作用。

<div class="stars">
  <form>
    <input class="star star-5" id="star-5" type="radio" name="star" value="5"/>
    <label class="star star-5" for="star-5"></label>
    <input class="star star-4" id="star-4" type="radio" name="star" value="4"/>
    <label class="star star-4" for="star-4"></label>
    <input class="star star-3" id="star-3" type="radio" name="star" value="3"/>
    <label class="star star-3" for="star-3"></label>
    <input class="star star-2" id="star-2" type="radio" name="star" value="2"/>
    <label class="star star-2" for="star-2"></label>
    <input class="star star-1" id="star-1" type="radio" name="star" value="1"/>
    <label class="star star-1" for="star-1"></label>
    <button class="button" type="submit" name="submit" value="submit">Submit</button>

  </form>
</div>

当我按下提交按钮时,没有任何反应。代码不会移动到 if 语句中,尽管它应该移动,因为 submit 不再为 null。

<?php
if(isset($_POST['submit'])){
    $rating = $_POST['star'];
    echo "hello";
    echo $rating;
}
?>

【问题讨论】:

  • 你没有使用POST,默认方法是GET。
  • 设置表单属性如`
  • 使用这个&lt;form method="post" action="" &gt; 并在php 代码上使用if(isset($_POST['star'])) 它会起作用。
  • @BibhudattaSahoo 成功了。谢谢,看来我正在观看的教程在其代码中将此视为错误,并且从未纠正过。如果您想将此作为答案发布,我会检查它。
  • @Syarx 欢迎您,然后通过接受我的回答来结束您的问题。

标签: php html web


【解决方案1】:

设置表单method=post
如果同一页面中的 html 和 php 代码使用
设置表单action="" 否则使用
设置表单action=".php file"

例如:&lt;form method="post" action=" or .php file"&gt;

【讨论】:

    【解决方案2】:

    您只是错过了将表单 methodaction 放入表单中。
    用这个

    &lt;form method="post" action="" &gt;

    在php代码上使用

    if(isset($_POST['star']))
    

    会有用的

    【讨论】:

      【解决方案3】:

      要使用超级全局变量$_POST,您需要为带有提交按钮的表单标签指定方法属性。 将您的表单标签和提交按钮标签更改为:

      <div class="stars">
      <form method="POST" name="starRatingForm" action="<?php echo $_SERVER['PHP_SELF'];?>">
      <input class="star star-5" id="star-5" type="radio" name="star" value="5"/>
      <label class="star star-5" for="star-5"></label>
      <input class="star star-4" id="star-4" type="radio" name="star" value="4"/>
      <label class="star star-4" for="star-4"></label>
      <input class="star star-3" id="star-3" type="radio" name="star" value="3"/>
      <label class="star star-3" for="star-3"></label>
      <input class="star star-2" id="star-2" type="radio" name="star" value="2"/>
      <label class="star star-2" for="star-2"></label>
      <input class="star star-1" id="star-1" type="radio" name="star" value="1"/>
      <label class="star star-1" for="star-1"></label>
      <input type="submit" name="submit" value="submit"/>
      </form>
      </div>
      

      【讨论】:

        【解决方案4】:

        isset() 检查变量是否具有包含 ( False , 0 , or empty string) 的值,但不包含 NULL。如果变量存在则返回 TRUE,否则返回 FALSE

        另一方面,empty() 函数检查变量是否具有empty value, empty string , 0, NULL ,or False。如果变量具有 non-emptynon-zero 值,则返回 FALSE

        示例

        if(!empty($_POST['star'])){
             ....
        }
        

        【讨论】:

          【解决方案5】:

          你必须在表单标签中设置methodaction属性。

           <form method='post' action='page.php'>
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2017-10-22
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2013-06-22
            相关资源
            最近更新 更多