[英]how to pass php variables to javascript function
I have 2 comboboxes and i want to pass the selected value to anoither php by ajax the problem is that how can pass the tow vaules in one function 我有2个组合框,我想通过ajax将选定的值传递给aoither php,问题是如何在一个函数中传递两个拖曳值
<script>
function showUser(str,str2) {
if (str == "" ||str2 =="" ) {
document.getElementById("txtHint").innerHTML = "";
return;
} else {
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
} else {
// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open("GET","cg_comboBox.php?q="+str+"&p="+str2,true);
xmlhttp.send();
}
}
</script>
<script>
function fun(v)
{
document.getElementById("xxx").innerHTML = v;
}
</script>
html html
<div class="form-group">
<label for="GroupName" class="col-sm-2 control-label">Movie Name</label>
<div class="col-md-2">
<select class="combobox" width="200" style="width: 200px" name="mov" id="mov" onchange="fun(this.value)">
<option value="">Select Movie </option>
<?php //code ?>
</select>
<br>
</div>
<label for="GroupName" class="col-sm-2 control-label">Group Name</label>
<div class="col-md-2">
<?php $x = "<div id=\"xxx\" name=\"xxx\" ><b></b></div>";
//echo $x; ?>
<select class="combobox" width="200" style="width: 200px" onchange="showUser(this.value,x)" id='x'>
<option value="">Select Cinema Group</option>
<?php //code ?>
</select>
i want to add $x value in the second parameter but how? 我想在第二个参数中添加$ x值,但是如何?
这应该可以解决您的问题:
onchange="showUser(this.value,'"+<?php echo $x; ?>+"')"
Your problem is most likely that you are naming your script with an HTML or HTM extension. 您的问题很可能是使用HTML或HTM扩展名命名脚本。 Name your script file with a PHP extension and not an HTML extension. 用PHP扩展名而不是HTML扩展名命名脚本文件。 This is a common problem. 这是一个普遍的问题。
Once you do that, the solutions posted by others will work, for example: 完成后,其他人发布的解决方案将起作用,例如:
<?php $x = 'something!'; ?>
onchange="showUser(this.value,'"+<?php echo $x; ?>+"')"
First assign a value fo $x and then use the value in javascript. 首先为$ x分配一个值,然后在javascript中使用该值。
If your .js file is static, and not output by PHP, then have your PHP output a variable dynamic definition, as follows: 如果您的.js文件是静态的,而不是PHP输出的,则让您的PHP输出一个变量动态定义,如下所示:
var myvar = <?php echo $x; ?>;
And in your HTML page use the variable as follows: 并在HTML页面中使用变量,如下所示:
onchange="showUser(this.value, myvar);"
That should do it! 那应该做! :) :)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.