簡體   English   中英

如何從PHP文件Jquery獲取價值?

[英]How to get value from PHP file 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.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值為“ xxxx”。
我已經在警報框中輸入了“ xxxx”,但是我需要將其分配給javascript中的var texts
我不知道該怎么做。
有人知道怎么做嗎?

Index.php得到了:

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

請幫幫我!

它確實可以工作,但是ajax調用是異步的,因此javascript將進行調用並移至下一行。 當您嘗試在彈出框中使用變量時,調用尚未完成,變量將為空。

在ajax調用之后,應將代碼移至成功函數內部,因為只有在此位置才能確定ajax調用已成功完成。

window.onload上調用函數時,您也不需要document(ready)塊,因此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();
                    }
           });             
     });
}

當您使用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