调用两个JS功能,不打印消息 [英] calling two js functions and don't print message
问题描述
我在阿贾克斯的问题,它不会打印我想要的消息。让我们来解释一下。 在PHP中code我有一个输入标签:
I have problem in ajax and it doesn't print the message I want. Let's explain. In php code i have an input tag:
<input type="submit" id="login_button_add" name="submit" value="Add"
onclick="add_building(); showbuildings( );" />
这两个JS功能是:
Those two js functions are:
function add_building(){
var str1=document.getElementById("building_name").value;
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("txtHint10").innerHTML=xmlhttp.responseText;}
}
xmlhttp.open("GET","add_building.php?q="+str1,true);
xmlhttp.send();
}
在 add_building.php
我的数据库和打印信息添加一行。查询工作正常,但它不与ID,我在我的html code打印消息在我的网页。我觉得现在的问题是,我所说的第二个js函数。因为当我打电话 add_building()
单独它的工作原理完美(打印信息)。
In add_building.php
I add a row in database and print messages. The query works fine, but it doesn't print the message in my page with id that I have in my html code. I think that the problem is that I call second js function. Because when I call add_building()
alone it works perfect(prints messages).
add_building.php
的PHP code是:
The php code of add_building.php
is:
$q=$_GET["q"];
if ($q!==''){
$link= mysqli_connect(...);
mysqli_set_charset($link, "utf8");
$sql="SELECT * FROM buildings WHERE name='$q'";
$result = mysqli_query($link,$sql);
if (!mysqli_num_rows($result)){
mysqli_set_charset($link, "utf8");
$sql="INSERT INTO buildings VALUES ('','$q','')";
$result =mysqli_query($link,$sql);
echo "The building added successfully.";
}
else {echo 'Building name exists. Try a different.';}
@ db_close($link);
}
else{echo 'Please insert a name.';}
另外js函数是:
The other js function is:
function showbuildings(str)
{
if (str=="")
{
document.getElementById("show_buildings_js").innerHTML="";
return;
}
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("show_buildings_js").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.open("GET","showbds.php?q=",true);
xmlhttp.send();
}
在此功能在我的网页打印表格。这工作得很好。
In this function I print the table in my page. That works fine.
现在的问题是,从 add_building.php
的消息 ID ='txtHint10'
不打印,
尽管所有的人在 add_building.php
的工作。我觉得现在的问题是,我所说的第二个js函数,我有两个xmlhttp.responseText。因为当我打电话js函数 add_building()
单独它完美的作品,并打印信息。
The problem is that the messages from add_building.php
don't print in id='txtHint10'
,
despite all the others in add_building.php
work. I think that the problem is that I call second js function and I have two xmlhttp.responseText. Because when I call js function add_building()
alone it works perfect and prints the messages.
推荐答案
现在的问题是,你是覆盖你的第二个JavaScript函数 XMLHTTP
变量。其结果是,仅来自第二功能的回调被执行
The problem is that you are overwriting you xmlhttp
variable with the second javascript function. The result is that only the callback from the second function is executed.
对方对于这两个功能独立工作,你就需要使用不同的变量名或局部声明它们在你的每一个函数 VAR XMLHTTP;
(清洁剂溶液)。
For both functions to work independently of each other, you would need to use different variable names or declare them locally in each of your functions with var xmlhttp;
(the cleaner solution).
请注意,在JavaScript中变量的范围是全球性的,除非你使用 VAR
在函数声明它。
Note that the scope of a variable in javascript is global unless you declare it using var
in your function.
这篇关于调用两个JS功能,不打印消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!