简体   繁体   English

将 Javascript 转换为 PHP 变量

[英]Converting Javascript to PHP Variable

I'm looking for some help I am trying to get the value that is selected on the first dropdown (id="select1") without submitting the form after some research I have been able to get the value of the first dropdown and output it with an alert with the below.我正在寻找一些帮助我试图获得在第一个下拉列表中选择的值 (id="select1") 在经过一些研究后不提交表单我已经能够获得第一个下拉列表的值和 output带有以下警报。

 <select name="select1" id="select1" class="form-control" required="required" 
 onchange="getValue(this)">

 <script>
 function getValue(obj){
 alert(obj.value);
 }
 </script>

What I'm struggling to do is store it as a php variable which I can use in the second dropdown (id="select2) this is my code any help with pointing me in the right direction would be appreciated.我正在努力做的是将它存储为 php 变量,我可以在第二个下拉列表中使用它(id="select2),这是我的代码,任何帮助我指向正确方向的帮助将不胜感激。

 <div class="form-group">
 <label>Select 1</label>
 <br>
 <select name="select1" id="select1" class="form-control" required="required" 
 onchange="getValue(this)">
 <option disabled selected>-- Select 1 --</option>
 <?php
 $records = mysqli_query($conn, "SELECT name1 From table_001");   

 while($row = mysqli_fetch_array($records))
 {
 echo "<option value='". $row['name1'] ."'>" .$row['name1'] ."</option>";  
 }  
  ?>
  
  </select>
 </div>
 
 <script>
 function getValue(obj){
   return(obj.value) ;
 }
 </script>
 
 <?php
 $phpvar = "OUTPUT OF THE ABOVE JS";
 ?>
 
 <div class="form-group">
 <label>Select 2</label>
 <br>
 <select name="select2" id="select2" class="form-control" required="required">
 <option disabled selected>-- Select 2 --</option>
 <?php
 $records1 = mysqli_query($conn, "SELECT name2 From table_002 where name1 = '$phpvar'");   
      
 while($row = mysqli_fetch_array($records1))
 {
 echo "<option value='". $row['name2'] ."'>" .$row['name2'] ."</option>";  
 }  
 ?>
 </select>
 </div>

If I've understood your question right, you can't just "store" a variable in php .如果我正确理解了您的问题,您不能只在 php 中“存储”一个变量 PHP is a server side, JS is a client side. PHP是服务器端,JS是客户端。 The only way to exchange data between them is an http request-response pair.在它们之间交换数据的唯一方法是 http 请求-响应对。 (We're not talking about websockets etc) (我们不是在谈论 websockets 等)

You can do the http request in two ways:您可以通过两种方式执行 http 请求:

  1. Form submit.表格提交。 When you're submiting a form, the data form this form is sent to a server either via GET or POST method.当您提交表单时,该表单的数据将通过 GET 或 POST 方法发送到服务器。 Then the server performs some actions and send a response.然后服务器执行一些操作并发送响应。 The page then is refreshed (or you are redirected to another page etc).然后刷新页面(或者您被重定向到另一个页面等)。
  2. AJAX request. AJAX 请求。 The principle is the same except you don't leave the actual page (there is no page refresh or redirection).原理是一样的,只是你不离开实际页面(没有页面刷新或重定向)。 The JS code is doing a request, then it receives a response event when response comes from server, and then you can do whatever you want with this response (insert it on the page, change select value etc). JS 代码正在执行请求,然后当响应来自服务器时它会接收响应事件,然后您可以使用此响应做任何您想做的事情(将其插入页面,更改 select 值等)。

Form submit way表单提交方式

I don't think this is your case, cause you need first of all submit the first form and then the page will be refreshed and the new page will be returned with the first form cleared but with a desired value inserted from php in a second form我不认为这是你的情况,因为你首先需要提交第一个表单,然后页面将被刷新,新页面将在第一个表单被清除但从 php 插入所需的值后返回形式

AJAX way AJAX方式

This is the preferable solution for you.这是您的首选解决方案。 You can make an ajax request either withXMLHttpRequest (the old way) or fetch (the modern way).您可以使用XMLHttpRequest (旧方式)或fetch (现代方式)发出 ajax 请求。 These are two native ways in browser (there exist also a lot of libraries, like jquery.ajax(), that are mostly the wrappers above these two methods described above).这是浏览器中的两种原生方式(也存在很多库,例如 jquery.ajax(),它们大多是上述这两种方法之上的包装器)。

Here is a basic example using fetch API, you may put this in a onChange event handler for your first select (:这是使用 fetch API 的基本示例,您可以将其放在第一个 select 的 onChange 事件处理程序中(:

fetch('./some_address.php')
  .then((response) => {
    if (!response.ok) {
      throw new Error(`HTTP error! Status: ${response.status}`);
    }

    // your server-side script may return a complex (or not :) json object or just a plain text, read the documentation for your case
    return response.json(); // or return response.text();
  })
  .then((data) => {
    // do whatever you need with your response from the server, for example set the value for your second select
  });

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

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