我有以下的code在PHP中,允许用户选择从下拉列表中的名称,然后通过该名称作为一个字符串交给一个JavaScript函数:
I have the following code in PHP that allows the user to select a name from a drop-down, then pass that name as a string over to a JavaScript function:
<?php
include('./db.php');
$PM = mysqli_query($con, "SELECT DISTINCT PMName FROM report WHERE PMname <> '' ORDER BY PMName ASC");
?>
<select class="navbar-inverse" placeholder="PM Name" name="PMName" onchange="showUser(this.value)">
<?php
while ($row = mysqli_fetch_row($PM)) {
$selected = array_key_exists('PMName', $_POST) && $_POST['PMName'] == $row[0] ? ' selected' : '';
printf(" <option value='%s' %s>%s</option>\n", $row[0], $selected, $row[0]);
}
?>
JavaScript函数然后读取字符串:
The JavaScript function then reads the string:
function showUser(str) {
if (str !==".PM") {
if (str=="") {
document.getElementById("txtHint").innerHTML="";
return;
}
}
$("#txtHint").load( "getuser.php?q="+str );
console.log(str);
}
和应该放置的输出 getuser.php?Q =约翰%李四
到 txtHint DIV。
and is supposed to place the output of getuser.php?q=John%Doe
into the txtHint DIV.
在 STR 的价值似乎并没有完全传递到
$(#txtHint)的负载(getuser.php Q =?+ STR);
的code线
the str value does not APPEAR to be fully passing into the
$("#txtHint").load( "getuser.php?q="+str );
line of code.
当我看着我的控制台,我看到下面的一次JavaScript函数火灾:
When I look at my console, I see the following once the JavaScript function fires:
HR finished loading: GET "http://localhost/getuser.php?q=John"
John Doe
如果我改变code阅读只是 $(#txtHint)的负载(getuser.php Q =?);
,(不通过了 STR
变量),它工作正常,并加载getuser.php页放入DIV,(当然,没有名字,我不回来任何信息,但它的工作原理......)
If I change the code to read just $("#txtHint").load( "getuser.php?q=");
, (without passing the str
variable,) it works fine and loads the getuser.php page into the DIV, (of course, without a name I don't get back any info, but it works...)
我在做什么错在这里?如果的console.log(STR)
正在输出李四
,那么很明显,整个字符串李四
,所以为什么我的控制台输出?Q =约翰·
?并造成getuser.php不加载到DIV?
What am I doing wrong here? If the console.log(str)
is outputting John Doe
, then obviously the entire string is John Doe
, so why is my console outputting ?q=John
? and causing getuser.php not to load into the DIV?
我很困惑在这里...
I'm very confused here....
您需要连接code的字符串,如果你想将它作为一个查询变量发送:
You need to encode your string if you want to send it as a query variable:
$("#txtHint").load( "getuser.php?q="+encodeURIComponent(str));