简体   繁体   English

如何在 php 中获取 select 的值?

[英]How to get select value on form in php?

I want to get a selected value on the form.我想在表单上获得一个选定的值。

Suppose If I select category "car" from sub-category "nissan".假设如果我 select 来自子类别“日产”的类别“汽车”。 I want this value on php.我想要 php 上的这个值。

Not value 1 and 1 because I do not recognise what value is selected.不是值 1 和 1 因为我不认识选择了什么值。 I want name or unique value in my php file.我想要我的 php 文件中的名称或唯一值。

This is php file on the same page:这是同一页上的 php 文件:

<?php

if(isset($_POST['select1'])){
    $select1 = $_POST['select1'];
}

if(isset($_POST['select2'])){
 $select2 = $_POST['select2'];
}

?>


 <html>
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js 
 </script>
<form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']) ?>" method="post">
<select name="select1" id="select1">
 <option value=""></option>
 <option value="1">car</option>
 <option value="2">phone</option>
 <option value="3">tv</option>
</select>


<select name="select2" id="select2">
 <option value=""></option>
 <option value="1">toyota</option>
 <option value="1">nissan</option>
 <option value="1">bmw</option>
 <option value="2">Iphone</option>
 <option value="2">LG</option>
 <option value="2">Samsung</option>
 <option value="3">Philips</option>
 <option value="3">Samsung</option>
</select>
</form>
</html>

<script>
 $("#select1").change(function() {
  if ($(this).data('options') == undefined) {
  /*Taking an array of all options-2 and kind of embedding it on the select1*/
  $(this).data('options', $('#select2 option').clone());
  }
  var id = $(this).val();
  var options = $(this).data('options').filter('[value=' + id + ']');
  $('#select2').html(options);
 });
</script>

Any idea or suggestions would be welcome Thank You.欢迎任何想法或建议谢谢。

You can remove the value property from all your options if you want to receive the text on the server instead.如果您想改为在服务器上接收文本,则可以从所有选项中删除value属性。 Try this尝试这个

 // get all categories and sub categories let categories = {}; $('#select2 option[data-category]').each(function(i, element){ let category = $(this).data('category'); // add category if( categories[category] == undefined ) categories[category] = []; // add sub category to category categories[category].push(element.textContent); // remove the option from the DOM element.remove(); }); // append related sub categories to #select 2 $("#select1").change(function() { let subs = categories[this.value]?? []; // sub categories for selected category $('#select2').html('<option></option>').append(subs.map(txt => `<option>${txt}</option>`)); });
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF'])?>" method="post"> <select name="select1" id="select1"> <option></option> <option>car</option> <option>phone</option> <option>tv</option> </select> <select name="select2" id="select2"> <option></option> <option data-category="car">toyota</option> <option data-category="car">nissan</option> <option data-category="car">bmw</option> <option data-category="phone">Iphone</option> <option data-category="phone">LG</option> <option data-category="phone">Samsung</option> <option data-category="tv">Philips</option> <option data-category="tv">Samsung</option> </select> </form>

hmm, Add the data attribute in the parent option and give a value like data-parent='1' and change value='car'嗯,在 parent 选项中添加 data 属性并提供一个值,如 data-parent='1' 并更改 value='car'

same you can do with child data-child='1' and value="toyota"同样你可以用 child data-child='1' 和 value="toyota"

then simply target the data attribute in js and remove other options from the child options然后简单地针对js中的数据属性并从子选项中删除其他选项

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM