Javascript 如何从PHP文件Jquery中获取值?
我现在有了这个javascript/jquery代码:Javascript 如何从PHP文件Jquery中获取值?,javascript,php,jquery,Javascript,Php,Jquery,我现在有了这个javascript/jquery代码: 我可以从php文件中获取值,但只能在警报框中显示该值。 我喜欢将这个值(结果)设置为“文本”,这样它就会显示在我的弹出窗口中 var script=document.createElement('script'); script.src=http://code.jquery.com/jquery-1.11.0.min.js'; // jquery script.type='text/javascript'; document.getEle
我可以从php文件中获取值,但只能在警报框中显示该值。
我喜欢将这个值(结果)设置为“文本”,这样它就会显示在我的弹出窗口中
var script=document.createElement('script');
script.src=http://code.jquery.com/jquery-1.11.0.min.js'; // jquery
script.type='text/javascript';
document.getElementsByTagName('head')[0].appendChild(脚本);
window.onload=addElement;
函数addElement(){
//创建一个新的div元素
//并给它弹出的内容
var newDiv=document.createElement(“div”);
var文本;
/////////////////////////////////////////////////////////////////////////////////////////
$(文档).ready(函数(){
$.ajax({
url:“index.php”,
成功:功能(结果){
text=result;//不起作用
}
});
});
/////////////////////////////////////////////////////////////////////////////////////////
newDiv.innerHTML+=''+text+'sluitenmer Informatie';
//添加背景封面
var BG=document.createElement(“div”);
BG.style.width='100%';
BG.style.height='100%';
BG.style.background='黑色';
BG.style.position='固定';
BG.style.top='0';
BG.style.left='0';
BG.style.opacity='0.7';
BG.style.zIndex='99900';
BG.style.display='none';
BG.setAttribute(“id”、“bgcover”);
//将新创建的元素及其内容添加到DOM中
文件.正文.附件(BG);
文件.正文.插入前(新div,BG);
//打开弹出窗口加载
openPopup();
}
函数openPopup(){
var el=document.getElementById('popup');
var BG=document.getElementById('bgcover');
el.style.display='block';
BG.style.display='block';
}
函数tostoring(){
window.location.href=http://localhost/Sms%20management%20systeem/testing/storing.php';
}
函数closePopup(){
var el=document.getElementById('popup');
var BG=document.getElementById('bgcover');
el.style.display='none';
BG.style.display='none';
}
当您使用json\u encode
在index.php文件中保存结果时,您必须对结果进行解码,如下所示:
$.ajax({
url:"index.php",
success:function(result){
var obj = $.parseJSON(result);
console.log(obj);
}
});
它确实可以工作,但是ajax调用是异步的,所以javascript将进行调用并转到下一行。当您尝试使用弹出框中的变量时,调用尚未完成,您的变量将为空 您应该在ajax调用之后将代码移动到success函数中,因为只有在那里您才能确定ajax调用已经成功完成 在
窗口上调用函数时,也不需要document(ready)块。onload
这样DOM就已经准备好了
您应该/也可以将css移动到外部css文件中,因为这样维护和共享会容易得多
function addElement() {
// create a new div element
// and give it popup content
var newDiv = document.createElement("div");
var texts;
$(document).ready(function() {
$.ajax({
url:"index.php",
success:function(result){
texts = result; // does not work
newDiv.innerHTML += '<div id="popup"><div class="popup_body">' + texts + '</div><button class="close_button"onClick="closePopup()">Sluiten</button><button class="close_button"onClick="tostoring()">Meer Informatie</button></div>';
// Move all the css to an external css file
// add the newly created elements and its content into the DOM
document.body.appendChild(BG);
document.body.insertBefore(newDiv, BG);
// open popup onload
openPopup();
}
});
});
}
函数addElement(){
//创建一个新的div元素
//并给它弹出的内容
var newDiv=document.createElement(“div”);
var文本;
$(文档).ready(函数(){
$.ajax({
url:“index.php”,
成功:功能(结果){
text=result;//不起作用
newDiv.innerHTML+=''+text+'sluitenmer Informatie';
//将所有css移动到外部css文件
//将新创建的元素及其内容添加到DOM中
文件.正文.附件(BG);
文件.正文.插入前(新div,BG);
//打开弹出窗口加载
openPopup();
}
});
});
}
变量文本
未在该函数中定义。即使是这样,你也不能期望在函数范围内改变局部变量的值会在某种程度上改变函数范围外的值。这将导致可怕的问题,任何代码都无法工作,因为到处都会发生这样的冲突。您需要阅读编程语言中的可见性范围。可以使用全局变量或绑定到某个全局对象的属性。或者你可以调用一个接受该值的函数。它可能会工作,但是当你试图访问变量时,ajax调用还没有完成。你应该在ajax调用之后将代码移动到success函数中。哇,到目前为止它工作得很好@jeroen ThanksI需要在我的js文件中包含css,因为我们的网站将只包含此脚本,它必须工作。您的解决方案有效,谢谢