php - 在不提交表单的情况下获取选项值

标签 php html

<分区>

我想在不提交表单的情况下使用用户选择的选项值。这是我的 HTML 代码

Combo Box<br>
<select name="select1">
<option value="IVP" selected>IVP</option>
<option value="SURTEK">SURTEK</option>
<option value="GUITARTUNER">GUITARTUNER</option>
<option value="OTHER">OTHER</option>
</select>

我想将选择的选项值取到一个php变量中,以便根据选项值显示一组新的数据。 谢谢

最佳答案

正如其他人所建议的,您应该使用 AJAX。我建议查看 AJAX 调用的 javascript/Jquery 示例。

我想你想根据用户选择的选项修改网页的一部分,最好的方法是有一个单独的 PHP 脚本来接收所选的选项(在 javascript/JQuery 中捕获)并返回新的您要显示的 HTML。

例如在Jquery中获取选中的选项:

var selected_option_value=$("#select1 option:selected").val();

同样在 Jquery 中执行 AJAX 调用,使用 POST 传递值:

  $.post("script_that_receives_value.php", {option_value: selected_option_value},
      function(data){ //this will be executed once the `script_that_receives_value.php` ends its execution, `data` contains everything said script echoed.
          $("#place_where_you_want_the_new_html").html(data);
      }
  );

希望这对您有所帮助!

编辑:让我们为上面的示例提供更多细节:

假设您有一个 index.html 页面,其中包含 <select name="select1">在你的问题中给出:

第一步是在有人选择其中一个选项时链接一个事件,如何做到这一点:

1- 第一种方法:

<select name='select1' id='select1' onchange='load_new_content()'>

这样当有人更改了 <select> 的选定值时列出 javascript 函数 load_new_content()将被执行。注意我添加了 id='select1'到标签,这用于在 javascript/JQuery 中搜索此元素,如果需要在 javascript/JQuery 中使用该标签,则应始终使用 id 属性。

2- 第二种方式,使用 JQuery 链接事件:

要做到这一点,你应该有一个 <script> <head> 内的标签index.html 的。这里面<script>你应该有的标签:

$(document).ready(function(){
      // everything here will be executed once index.html has finished loading, so at the start when the user is yet to do anything.
      $("#select1").change(load_new_content()); //this translates to: "when the element with id='select1' changes its value execute load_new_content() function"
});

无论您想使用哪个选项,您现在都需要这个 load_new_content()功能。它也应该在 <script> 中声明<head> 的标签标记,就像 $(document).ready 函数一样。

function load_new_content(){
     var selected_option_value=$("#select1 option:selected").val(); //get the value of the current selected option.

     $.post("script_that_receives_value.php", {option_value: selected_option_value},
         function(data){ //this will be executed once the `script_that_receives_value.php` ends its execution, `data` contains everything said script echoed.
              $("#place_where_you_want_the_new_html").html(data);
              alert(data); //just to see what it returns
         }
     );
} 

现在唯一剩下的就是这个 script_that_receives_value.php :

<?php
     $selected_option=$_POST['option_value'];

     //Do what you need with this value, then echo what you want to be returned.

     echo "you have selected the option with value=$selected_option";
?>

关于php - 在不提交表单的情况下获取选项值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15153595/

相关文章:

html - 文本对齐 : right; not working for <label>

javascript - 如何裁剪 IFrame 的内容(从 IFrame 容器,不使用滚动)

html - iPad 上的导航栏菜单响应

javascript - 如何将 onload 属性与 ng-controller 指令一起使用来执行 Controller 中定义的函数?

php - 遍历数组 PHP

php - AJAX 自动刷新不包含的 div?

php - 将随机字符附加到用户上传照片的末尾

php - 在PHP中使用系统的时区设置

html - 表格中 Strage 1px 白色边框

php - 使用 preg_replace 替换字符串中的数组变量