dqk42179 2016-01-20 01:16
浏览 145
已采纳

if(isset($ _ POST [“myvar”])&& isset($ _ POST [“myvar2”])失败

If i use this code to take as input a number it works fine

<form  method = "post">
    Hours: <input type="text" name="myvar">
</form>

<?php
require 'connect.php';
if (isset($_POST["myvar"])) {

But the following code to get 2 numbers doesn't go pass if. How can i make this work?

<form  method = "post">
    Hours: <input type="text" name="myvar">
</form>
<form  method = "post">
    Minutes: <input type="text" name="myvar2">
</form> 

<?php
require 'connect.php';
if (isset($_POST["myvar"]) && isset($_POST["myvar2"])) {
  • 写回答

7条回答 默认 最新

  • dongre6227 2016-01-20 01:58
    关注

    You just need to use one form for both fields as:

    <form method="post">
        Hours: <input type="text" name="myvar">
        Minutes: <input type="text" name="myvar2">
        <input type="submit" name="submit">
    </form>
    

    And in PHP you can use !empty() for form input.

    <?
    if (!empty($_POST["myvar"]) && !empty($_POST["myvar2"])) {
    ...
    ?>
    

    Second solution, if you still want to use isset() than also add !empty() for checking as:

    <?
    if (isset($_POST["myvar"]) && !empty($_POST["myvar"]) && 
        (isset($_POST["myvar2"]) && !empty($_POST["myvar2"]))) {
        echo "<pre>";
        print_r($_POST);
    }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(6条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部