在javascript中传递echo-php
根据问题,我在javscript中实现了一个动态表单,它要求select中选择的第一个选项与新select中的另一个选项匹配:在javascript中传递echo-php,javascript,php,Javascript,Php,根据问题,我在javscript中实现了一个动态表单,它要求select中选择的第一个选项与新select中的另一个选项匹配: <!doctype html> <html> <head> <meta charset="UTF-8"> <title>Documento senza titolo</title> <script> function random_function
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Documento senza titolo</title>
<script>
function random_function()
{
var a=document.getElementById("input").value;
if(a==="INDIA")
{
var arr=["Maharashtra","Delhi"];
}
else if(a==="USA")
{
var arr=["Washington","Texas","New York"];
}
var string="";
for(i=0;i<arr.length;i++)
{
string=string+"<option>"+arr[i]+"</option>";
}
string="<select name='any_name'>"+string+"</select>";
document.getElementById("output").innerHTML=string;
}
</script>
</head>
<body>
<select id="input" onchange="random_function()">
<option>select option</option>
<option>INDIA</option>
<option>USA</option>
</select>
<div id="output">
</div>
</body>
</html>
到目前为止没有什么奇怪的,但现在我想知道,在之前创建了一个db之后,我如何用静态的方式代替cities来实现呢
html的代码如下所示
<select>
<?php
while(($row = mysqli_fetch_assoc($regioni)) !== null){
print(
"<option value='".htmlentities($row['regione'])."'>".
htmlentities($row['regione']).
"</option>"
);
}
?>
</select>
您的示例如下所示:
森扎·蒂托洛文件
你可以这样做
<select id="input">
<option value="">select option</option>
<?php foreach(mysqli_fetch_assoc($regioni) as $region){ ?>
<option value="<?php echo $region['regione']; ?>"><?php echo $region['regione']; ?></option>
<?php } ?>
</select>
若您需要为选择器设置默认值,则可以与某些变量或字符串进行比较
<option value="<?php echo $region['regione']; ?>"<?php ($region['regione'] == 'INDIA') ? echo
' selected="selected"' : '' ?>><?php echo $region['regione']; ?></option>
您将无法使用do while logic访问$row_regioni['regione'],因为变量未在第一次迭代中设置。使用while$row\u regioni=mysqli\u fetch\u assoc$regioni启动循环,然后可以访问array@tshimkus好的,您能给出一个书面示例吗?错误语法:数组\u push$regions、.str\u replace“”、'\\'、$row['regione'].';是的,对不起。太多了。我把它拿走了。
function random_function(){
// ...
var string = "";
for(var i = 0; i < regions.length; i++){
string += "<option value='" + regions[i] + "'>" + regions[i] + "</option>";
}
string = "<select name='any_name'>" + string + "</select>";
//...
}
<select id="input">
<option value="">select option</option>
<?php foreach(mysqli_fetch_assoc($regioni) as $region){ ?>
<option value="<?php echo $region['regione']; ?>"><?php echo $region['regione']; ?></option>
<?php } ?>
</select>
<option value="<?php echo $region['regione']; ?>"<?php ($region['regione'] == 'INDIA') ? echo
' selected="selected"' : '' ?>><?php echo $region['regione']; ?></option>