douying8666 2016-11-19 02:57
浏览 41
已采纳

使用set post value作为php变量

In a form I have a radio, of which one value is a specific API Key and the other radio opens a text input to enter a custom api key.

In order to distinguish from the two inputs in the action, they are set under different names.

My question is how do I set a PHP variable to equal the post value that is not empty.

For example, if input "a" has a set value then $apikey = $_POST['a'] but if "a" is empty and "p" has a value then $apikey = $_POST['p']

Here is the code used in the form for the two values that will be used:

<input type="radio" name="a" id="apibeta" value="###APIKEY###" onclick="javascript:hide();" required />

<input id='yes' name="p" class="form-control" placeholder="Personal API Key" type="text">

Thank you for any help!

  • 写回答

2条回答 默认 最新

  • dph19153 2016-11-19 03:01
    关注

    Using the ternary operator:

    $apiKey = !empty($_POST['a']) ? $_POST['a'] : $_POST['p'];
    

    Which is equivalent to:

    if (!empty($_POST['a'])) {
        $apiKey = $_POST['a'];
    } else {
        $apiKey = $_POST['p'];
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?
  • ¥15 python爬取bilibili校园招聘网站
  • ¥30 求解达问题(有红包)
  • ¥15 请解包一个pak文件
  • ¥15 不同系统编译兼容问题