[英]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 等)
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 插入所需的值后返回形式
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.