怎么将PHP中的字符串赋值给js中的变量

时间:2022-10-28 21:22:37
我遇到的问题是不能调用.js插件包,只能用手工方式将PHP返回的字符串赋值给<javascript>中的变量代码如下,在线等:谢谢不吝赐教:
这是get_uname.php页面
<?php
function get_usernames()
{
$connect = mysql_connect("localhost","root") or die("连接失败……!");
mysql_selectdb("bugtracker");
$query="select username from mantis_user_table";
$result=mysql_query($query,$connect) or die ("查询数据库失败");
while ($row=mysql_fetch_array($result))
{
$array[]=$row["username"];
}
$result=implode("|",$array);
$result1=json_encode($result);
return $result1;
}

?>


这是js页面:
<?php 
require_once 'get_unames.php';
 $num=get_usernames();
?>
 <script language="javascript" type="text/javascript">
function checkword()
{
   var alltxt;
   var wordvalue=document.getElementById("word").value.toLowerCase();
   var alltxt= eval("(<?php echo $num;?>)");//这里是我想将返回的字符串赋值给js的变量,这是我写的,没用
   var alltxtpp=alltxt.toLowerCase();
   var alltxt_xiang=alltxt.split("|");
   var alltxt_xiang1=alltxtpp.split("|");
   var inhtml="<ul>";
   var isyou=0;
   for (i=0;i<alltxt_xiang1.length;i++)
   {
       if (alltxt_xiang1[i].substr(0,wordvalue.length)==wordvalue)
       {
           inhtml=inhtml+"<li onclick=\"document.getElementById('word').value=this.innerHTML;document.getElementById('showmenu').style.display='none';\" onmouseover=\"this.style.backgroundColor='#666666'\" onmouseout=\"this.style.backgroundColor=''\">"+alltxt_xiang[i]+"</li>";
           isyou=1;
       }
   }
   inhtml=inhtml+"</ul>";
   if (isyou==1)
   {
       document.getElementById("showmenu").innerHTML=inhtml;
       document.getElementById("showmenu").style.display="";
   }
   else
   {
       document.getElementById("showmenu").innerHTML="";
       document.getElementById("showmenu").style.display="none";
   }
   if (wordvalue=="")
   {
       document.getElementById("showmenu").innerHTML="";
       document.getElementById("showmenu").style.display="none";
   }
}
</script>
<input type="text" name="word" size="20" id="word" onkeyup="javascript:checkword()">
<div style="position: absolute; width: 200px; height: 100px; z-index: 1; left: 10px; top: 40px;border:1px solid #666666;display:none;" id="showmenu">

12 个解决方案

#1


补:var alltxt;这个是注释掉的

#2


很简单。。。
var jsParamter=<?php echo $phpParamter; ?>;

#3


如果是字符串还要加引号

#4


<?php 
require_once 'get_unames.php';
 $num=get_usernames();//这是页面中的方法返回一个json值
?>
 <script language="javascript" type="text/javascript">
function checkword()
{
   var wordvalue=document.getElementById("word").value.toLowerCase();
   var alltxt = <?php echo '$num'; ?>;
   var alltxtpp=alltxt.toLowerCase();
   var alltxt_xiang=alltxt.split("|");
   var alltxt_xiang1=alltxtpp.split("|");
   var inhtml="<ul>";
   var isyou=0;
   for (i=0;i<alltxt_xiang1.length;i++)
   {
       if (alltxt_xiang1[i].substr(0,wordvalue.length)==wordvalue)
       {
           inhtml=inhtml+"<li onclick=\"document.getElementById('word').value=this.innerHTML;document.getElementById('showmenu').style.display='none';\" onmouseover=\"this.style.backgroundColor='#666666'\" onmouseout=\"this.style.backgroundColor=''\">"+alltxt_xiang[i]+"</li>";
           isyou=1;
       }
   }
   inhtml=inhtml+"</ul>";
   if (isyou==1)
   {
       document.getElementById("showmenu").innerHTML=inhtml;
       document.getElementById("showmenu").style.display="";
   }
   else
   {
       document.getElementById("showmenu").innerHTML="";
       document.getElementById("showmenu").style.display="none";
   }
   if (wordvalue=="")
   {
       document.getElementById("showmenu").innerHTML="";
       document.getElementById("showmenu").style.display="none";
   }
}
</script>
<input type="text" name="word" size="20" id="word" onkeyup="javascript:checkword()">
<div style="position: absolute; width: 200px; height: 100px; z-index: 1; left: 10px; top: 40px;border:1px solid #666666;display:none;" id="showmenu">


我这样写了  但是报$num这个变量未定义  这是怎么回事呢?  谢谢帮忙 

#5


'get_unames.php'页面有这个方法get_usernames();帮我看下这个功能哪里错了 ,谢谢各位

#6


1、确认你的php程序文件是 utf-8 编码的,否则需对 $result 转码。不然的话 json_encode 将失败
2、你有 $result=implode("|",$array);
这样 $result 是一个形如 "a|b|c" 的串,不是合法的js语句。
对于 var alltxt= eval("(<?php echo $num;?>)"); 将产生错误
你只需 var alltxt= <?php echo $num;?>; 就可以了

#7


引用 6 楼 xuzuning 的回复:
1、确认你的php程序文件是 utf-8 编码的,否则需对 $result 转码。不然的话 json_encode 将失败
2、你有 $result=implode("|",$array);
这样 $result 是一个形如 "a|b|c" 的串,不是合法的js语句。
对于 var alltxt= eval("(<?php echo $num;?>)"); 将产生错误
你只需 var al……

我的PHP程序是utf-8文件,按照您说的我将var alltxt= eval("(<?php echo $num;?>)"); 改成了var alltxt= <?php echo $num;?>; 它报的错是$num未定义。可视我上面PHP中已经定义了啊……,麻烦指导指导 谢谢……

#8


var alltxt = "<?php echo $num; ?>";

#9


$result=implode("|",$array);
$result1=json_encode($result);
这本来就错了。。。。上面那句没用,JSON_ENCODE本来就可以吧数组转换成JSON字符串,你上面那句把它搞成一个奇怪字符串传进去根本生成不了JSON字符串....

#10


在出错的页面中,右键=》查看源代码 帖出结果

#11


谢谢大家了,可以运行了……呵呵 ……

#12


引用 11 楼 jinjong 的回复:
谢谢大家了,可以运行了……呵呵 ……


你是怎么解决的啊?

#1


补:var alltxt;这个是注释掉的

#2


很简单。。。
var jsParamter=<?php echo $phpParamter; ?>;

#3


如果是字符串还要加引号

#4


<?php 
require_once 'get_unames.php';
 $num=get_usernames();//这是页面中的方法返回一个json值
?>
 <script language="javascript" type="text/javascript">
function checkword()
{
   var wordvalue=document.getElementById("word").value.toLowerCase();
   var alltxt = <?php echo '$num'; ?>;
   var alltxtpp=alltxt.toLowerCase();
   var alltxt_xiang=alltxt.split("|");
   var alltxt_xiang1=alltxtpp.split("|");
   var inhtml="<ul>";
   var isyou=0;
   for (i=0;i<alltxt_xiang1.length;i++)
   {
       if (alltxt_xiang1[i].substr(0,wordvalue.length)==wordvalue)
       {
           inhtml=inhtml+"<li onclick=\"document.getElementById('word').value=this.innerHTML;document.getElementById('showmenu').style.display='none';\" onmouseover=\"this.style.backgroundColor='#666666'\" onmouseout=\"this.style.backgroundColor=''\">"+alltxt_xiang[i]+"</li>";
           isyou=1;
       }
   }
   inhtml=inhtml+"</ul>";
   if (isyou==1)
   {
       document.getElementById("showmenu").innerHTML=inhtml;
       document.getElementById("showmenu").style.display="";
   }
   else
   {
       document.getElementById("showmenu").innerHTML="";
       document.getElementById("showmenu").style.display="none";
   }
   if (wordvalue=="")
   {
       document.getElementById("showmenu").innerHTML="";
       document.getElementById("showmenu").style.display="none";
   }
}
</script>
<input type="text" name="word" size="20" id="word" onkeyup="javascript:checkword()">
<div style="position: absolute; width: 200px; height: 100px; z-index: 1; left: 10px; top: 40px;border:1px solid #666666;display:none;" id="showmenu">


我这样写了  但是报$num这个变量未定义  这是怎么回事呢?  谢谢帮忙 

#5


'get_unames.php'页面有这个方法get_usernames();帮我看下这个功能哪里错了 ,谢谢各位

#6


1、确认你的php程序文件是 utf-8 编码的,否则需对 $result 转码。不然的话 json_encode 将失败
2、你有 $result=implode("|",$array);
这样 $result 是一个形如 "a|b|c" 的串,不是合法的js语句。
对于 var alltxt= eval("(<?php echo $num;?>)"); 将产生错误
你只需 var alltxt= <?php echo $num;?>; 就可以了

#7


引用 6 楼 xuzuning 的回复:
1、确认你的php程序文件是 utf-8 编码的,否则需对 $result 转码。不然的话 json_encode 将失败
2、你有 $result=implode("|",$array);
这样 $result 是一个形如 "a|b|c" 的串,不是合法的js语句。
对于 var alltxt= eval("(<?php echo $num;?>)"); 将产生错误
你只需 var al……

我的PHP程序是utf-8文件,按照您说的我将var alltxt= eval("(<?php echo $num;?>)"); 改成了var alltxt= <?php echo $num;?>; 它报的错是$num未定义。可视我上面PHP中已经定义了啊……,麻烦指导指导 谢谢……

#8


var alltxt = "<?php echo $num; ?>";

#9


$result=implode("|",$array);
$result1=json_encode($result);
这本来就错了。。。。上面那句没用,JSON_ENCODE本来就可以吧数组转换成JSON字符串,你上面那句把它搞成一个奇怪字符串传进去根本生成不了JSON字符串....

#10


在出错的页面中,右键=》查看源代码 帖出结果

#11


谢谢大家了,可以运行了……呵呵 ……

#12


引用 11 楼 jinjong 的回复:
谢谢大家了,可以运行了……呵呵 ……


你是怎么解决的啊?