简体   繁体   English

如何从PHP文件Jquery获取价值?

[英]How to get value from PHP file Jquery?

I now have this javascript/jquery code: 我现在有这个javascript / jquery代码:
I can get the value from my php file but i can only show it in a alert box. 我可以从php文件中获取值,但只能在警报框中显示它。
I like to have this value (result) set into "texts" so it will show in my popup. 我喜欢将此值(结果)设置为“文本”,以便将其显示在弹出窗口中。

 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(script); window.onload = addElement; 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" style=" position: fixed;top: 15%;width: 800px;height: 200px;margin: auto;z-index: 99999;display: block;left:25%;background-color: #fff; border: 1px solid #ddd; border-radius: 5px; box-shadow: 0 1px 6px 4px #000; overflow: hidden; padding: 10px;"><div class="popup_body" style=" height: 160px;">' + texts + '</div><button style="padding: 10px;" class="close_button"onClick="closePopup()">Sluiten</button><button style="padding: 10px;" class="close_button"onClick="tostoring()">Meer Informatie</button></div>'; // Add The Background cover var BG = document.createElement("div"); BG.style.width = '100%'; BG.style.height = '100%'; BG.style.background = 'black'; BG.style.position = 'fixed'; 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"); // add the newly created elements and its content into the DOM document.body.appendChild(BG); document.body.insertBefore(newDiv, BG); // open popup onload openPopup(); } function openPopup() { var el = document.getElementById('popup'); var BG = document.getElementById('bgcover'); el.style.display = 'block'; BG.style.display = 'block'; } function tostoring() { window.location.href = 'http://localhost/Sms%20management%20systeem/testing/storing.php'; } function closePopup() { var el = document.getElementById('popup'); var BG = document.getElementById('bgcover'); el.style.display = 'none'; BG.style.display = 'none'; } 

results got 'xxxx' as value. results值为“ xxxx”。
I got 'xxxx' into a alert box already, but i need to assign it to var texts in javascript. 我已经在警报框中输入了“ xxxx”,但是我需要将其分配给javascript中的var texts
I don't know how to do this. 我不知道该怎么做。
Anyone know how to do this? 有人知道怎么做吗?

Index.php got: Index.php得到了:

<?php
    $var = "xxxx";
    echo json_encode($var);
?>

Help me please! 请帮帮我!

It does work, but the ajax call is asynchronous so javascript will make the call and move on to the next line. 它确实可以工作,但是ajax调用是异步的,因此javascript将进行调用并移至下一行。 And when you try to use the variable in your pop-up box, the call has not yet finished and your variable will be empty. 当您尝试在弹出框中使用变量时,调用尚未完成,变量将为空。

You should move the code after the ajax call to inside the success function as only there you know for sure that the ajax call has finished successfully. 在ajax调用之后,应将代码移至成功函数内部,因为只有在此位置才能确定ajax调用已成功完成。

You also don't need the document(ready) block as you are calling your function on window.onload so the DOM will already be ready. window.onload上调用函数时,您也不需要document(ready)块,因此DOM已经准备就绪。

You should / could also move the css to an external css file as that will be a lot easier to maintain and share. 您还应该/也可以将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();
                    }
           });             
     });
}

As you are econding the result in index.php file using json_encode , you have to decode the result in response as below: 当您使用json_encode在index.php文件中查找结果时,必须对响应进行解码,如下所示:

$.ajax({
  url:"index.php",
  success:function(result){
    var obj = $.parseJSON(result);
    console.log(obj);
  }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM