使用设置 post 值作为 php 变量
Use set post value as php variable
在一个表单中,我有一个收音机,其中一个值是特定的 API 键,另一个收音机打开文本输入以输入自定义 api 键。
为了区别action中的两个input,设置了不同的名称。
我的问题是如何将 PHP 变量设置为等于非空的 post 值。
例如,如果输入"a"有一个设定值那么$apikey = $_POST['a']
但是如果 "a" 是空的并且 "p" 有一个值那么 $apikey = $_POST['p']
以下是表单中将使用的两个值所使用的代码:
<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">
感谢您的帮助!
这就是 if
语句或 switch
语句的含义。
这是您遇到的问题的示例
<?php
if(isset($_POST['a'])){
//do something with that
}elseif(isset($_POST['p'])){
//p has the value
}else{
//neither have a value.
}
?>
使用三元运算符:
$apiKey = !empty($_POST['a']) ? $_POST['a'] : $_POST['p'];
相当于:
if (!empty($_POST['a'])) {
$apiKey = $_POST['a'];
} else {
$apiKey = $_POST['p'];
}
在一个表单中,我有一个收音机,其中一个值是特定的 API 键,另一个收音机打开文本输入以输入自定义 api 键。
为了区别action中的两个input,设置了不同的名称。
我的问题是如何将 PHP 变量设置为等于非空的 post 值。
例如,如果输入"a"有一个设定值那么$apikey = $_POST['a']
但是如果 "a" 是空的并且 "p" 有一个值那么 $apikey = $_POST['p']
以下是表单中将使用的两个值所使用的代码:
<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">
感谢您的帮助!
这就是 if
语句或 switch
语句的含义。
这是您遇到的问题的示例
<?php
if(isset($_POST['a'])){
//do something with that
}elseif(isset($_POST['p'])){
//p has the value
}else{
//neither have a value.
}
?>
使用三元运算符:
$apiKey = !empty($_POST['a']) ? $_POST['a'] : $_POST['p'];
相当于:
if (!empty($_POST['a'])) {
$apiKey = $_POST['a'];
} else {
$apiKey = $_POST['p'];
}