在php中创建选择选项名称的变量而不是值

我有一个选择字段,我将我的选项值用作变量以进行进一步的工作,但我想为我的选项名称创建第二个变量以在其他地方使用。这是我的表格


<form action="second.php" method="post" class="form">

        Client:<select class="default" id="client" name="client" required>

        <option value="" selected>Select Client</option>

        <option value="clienta" name"Client A">Client A</option>

        <option value="clientb" name "Client B">Client B</option>

</select>

</form>


在我的 second.php 文件中,我得到了客户端的值,如下所示:


$client = $_POST["client"];

我不确定如何获取客户的名称而不是价值。我努力了:


$client_name = $_POST["client.name"];

$client_name = $_POST["client[name]"];

$client_name = $_POST["client(name)"];

但是这些都不起作用,当我想返回选项的名称时,它们都返回空白,例如 "Client A" 而不是"clienta"


有谁知道我如何获得名称字段而不是我选择的值?


墨色风雨
浏览 93回答 3
3回答

哆啦的时光机

如果您需要值和名称。最好的归档方式是将它们组合起来<?phpif (isset($_POST['client'])){$pair = explode("/", $_POST["client"]);$name&nbsp; &nbsp;= $pair[0];$value = $pair[1];print "Key: $name<br />";print "Value: $value<br />";}?><form method="post" action="" name="form"><select name="client">&nbsp; &nbsp; <option value="clienta/Client A">Client A</option>&nbsp; &nbsp; <option value="clientb/Client B">Client B</option></select><input name="submit" type="submit"></form>

慕桂英546537

<form action="second.php" method="post" class="form">&nbsp; &nbsp; Client:<select class="default" id="client" data-name="client" required>&nbsp; &nbsp; <option value="" selected>Select Client</option>&nbsp; &nbsp; <option value="clienta" data-name="Client A">Client A</option>&nbsp; &nbsp; <option value="clientb" data-name="Client B">Client B</option>&nbsp; &nbsp; <input type="button" id ="button" value="send">$(document).ready(function(){$('#button').click(function(){&nbsp; &nbsp; $.ajax({&nbsp; &nbsp; &nbsp; &nbsp; type: 'POST',&nbsp; &nbsp; &nbsp; &nbsp; url: 'second.php',&nbsp; &nbsp; &nbsp; &nbsp; data: {'clinet_val':$('#client').val(),'clinet_name':$('#client').find(':selected').data('name')},&nbsp; &nbsp; &nbsp; &nbsp; success:function(response)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; console.log(response);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });});

慕田峪9158850

您不需要添加属性name <option>元素:<form action="second.php" method="post" class="form">&nbsp; &nbsp; &nbsp; &nbsp; Client:<select multiple="multiple" class="default" id="client" name="client[]" required>&nbsp; &nbsp; &nbsp; &nbsp; <option value="" selected>Select Client</option>&nbsp; &nbsp; &nbsp; &nbsp; <option value="ClientA">Client A</option>&nbsp; &nbsp; &nbsp; &nbsp; <option value="ClientB">Client B</option></select></form>在 php 中:$clients = $_POST['client']; // will be array with selected options
打开App,查看更多内容
随时随地看视频慕课网APP