简体   繁体   English

Select 标签 HTML 动态 javaScript PHP

[英]Select tag HTML dynamic javaScript PHP

Hello I want to achieve is to make "Select Form HTML" dynamic using JavaScript, What I mean by that is I expect every time I select a dropdown must be selected the value that I set to that tag.您好,我想要实现的是使用 JavaScript 使“选择表单 HTML”动态化,我的意思是我希望每次 select 时都必须选择一个下拉列表,该值是我设置为该标签的值。

The data from tag is from database I loop the data using php来自标签的数据来自数据库我使用 php 循环数据

ex.前任。 1 src: Get selected value/text from Select on change 1 src: 在更改时从 Select 获取选定的值/文本
This example is 100% working correct but what I need is not to get the value but to assign like sample below next example 2此示例 100% 工作正确,但我需要的不是获取值,而是分配下一个示例 2 下面的示例

function handleSelectChange(event) {

    var selectElement = event.target;
    var value = selectElement.value;
    alert(value);
}

<select onchange="handleSelectChange(event)">
    <option value="1">one</option>
    <option value="2">two</option>
</select>

ex 2 Those function will alert or run everytime I select/Click each of them F1 or F2 ex 2 那些 function 每次我选择/单击它们 F1 或 F2 时都会发出警报或运行

<table>
    <tr>
      <td onclick="myFunction1()">F1</td>
      <td onclick="myFunction2()">F2</td>
    </tr>
</table>

<script>
 
 // Function 1 will run when I click the F1 
 function myFunction1() { alert('myFunction1'); }
 // Function 2 will run when I click the F2
 function myFunction2() { alert('myFunction2'); }

In example 1 As you can see the first example select form html will grab the the value of option tag, right?.在示例 1 中您可以看到第一个示例 select 形式 html 将获取选项标签的值,对吧?
Now In example number 2 it will run the function when I click each of the F1 or F2现在在示例 2 中,当我单击 F1 或 F2 时,它将运行 function

So what I need to the program is pass a value from my database to my javaScript function and run it like in alert or example 1 but in "Select tag" HTML version所以我需要程序将一个值从我的数据库传递到我的 javaScript function 并像在警报或示例 1 中一样运行它,但在“选择标签”Z4C4AD5FCA2E7A3F74DBB1CED00381AAZ 版本中

ex.前任。 3 Here's my query 3 这是我的查询

 <form action=""> 
  <select name="customers" id="myId" class="form-control">
    <option value="">Select a customer:</option>

 <?php 
    
 $user_id =  1;
 
 $sql     = "SELECT * FROM `myTable` WHERE user_id = '$user_id' Order by create_at DESC";
 $result  = $mysqli->query($sql);
 
 if ($result->num_rows > 0) {
   
  // output data of each row
  while($row = $result->fetch_assoc()) 
  { ?>
    
   <!-- appsFunction('<?php echo $row['colName2']; ?>') << will be the value that should run in console.log -->
   <option value="<?php echo $row['id']; ?>" onclick="appsFunction('<?php echo $row['colName2']; ?>')"><?php echo $row['colName1']; ?></option>

  <?php }
  
 } else {  return false;  }
 
 ?>
 </select>
</form>

ex.前任。 3 part 2 javascript 3 部分 2 javascript

<script>

function appsFunction(passVar) {
  
  colose.log(passVar);

}

</script>

As you can see in my first example a "Select Tag" HTML when I select dropdown it returns to me a value right?, in second example when I click F1 or F2 it will run the function then return alert, What I need here is When I select the dropdown it will accept the value I pass in function "appsFunction(passVar)" appsFunction('<?php echo $row['colName2']; ?>') from select tag which the value is from my database.. so I need help idea how to do that properly..正如您在我的第一个示例中看到的那样,当我在 select 下拉列表中看到“选择标签”HTML 时,它会返回给我一个值,对吧? When I select the dropdown it will accept the value I pass in function "appsFunction(passVar)" appsFunction('<?php echo $row['colName2']; ?>') from select tag which the value is from my database. .所以我需要帮助知道如何正确地做到这一点..

NOTE: The function must be run when I select the dropdown, the function must be accept the value I set which from my database it's like totally example number 2 but in Select tag HTML version not just text or html table. NOTE: The function must be run when I select the dropdown, the function must be accept the value I set which from my database it's like totally example number 2 but in Select tag HTML version not just text or html table.

Thanks in advance for solving my problem.提前感谢您解决我的问题。

If you register an event handler on the select element itself that listens for change events you will be able to process the selected OPTION and access any value/property associated with it.如果您在select元素本身上注册一个事件处理程序来侦听change事件,您将能够处理选定的 OPTION 并访问与其关联的任何值/属性。 You cannot assign an event handler directly to an OPTION element as you are trying to do here.您不能像在此处尝试那样将事件处理程序直接分配给OPTION元素。 You can add as many dataset attributes as you need to the OPTION elements however which you can easily access in the Javascript event handler.您可以根据需要向OPTION元素添加任意数量的dataset属性,但是您可以在 Javascript 事件处理程序中轻松访问这些属性。

If the value you pass into the SQL statement is to be dynamic ( from a GET or POST request most usually ) you really need to use a Prepared Statement to help mitigate SQL Injection attacks.如果您传递给 SQL 语句的值是dynamic的(通常来自 GET 或 POST 请求),您确实需要使用准备好的语句来帮助缓解 SQL 注入攻击。

As your select menu is making use of only 3 columns from the mytable table you should limit the returned fields to those columns which in turns helps when using a Prepared Statement as you can easily assign the results as variables.由于您的select菜单仅使用mytable表中的 3 列,因此您应该将返回的字段限制为这些列,这反过来在使用 Prepared Statement 时会有所帮助,因为您可以轻松地将结果分配为变量。

<form name='geronimo'> 

    <select name="customers" class="form-control">
        <option selected hidden disabled>Select a customer:
        <?php 

            $user_id =  1;

            $sql="select `id`, `colName1`, `colName2` from `mytable` where user_id = ? order by create_at desc";
            $stmt=$mysqli->prepare($sql);
            $stmt->bind_param('s',$user_id);
            $res=$stmt->execute();
            
            if( $res ){
                $stmt->bind_result($id,$col1,$col2);
                while( $stmt->fetch() ){
                    printf('<option value="%s" data-col="%s">%s', $id, $col2, $col1 );
                }
                $stmt->free_result();
                $stmt->close();
            }
        ?>
    </select>
</form>


<script>
    document.forms.geronimo.customers.addEventListener('change',function(e){
        let id=this.value;
        let col2=this.dataset.col;
        let col1=this.options[this.options.selectedIndex].text;
        
        alert( id + ' ' + col2 + ' ' + col1 )
    });
</script>

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

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