私は今このjavascript/jqueryコードを持っています:
私は私のPHPファイルから値を得ることができますが、警告ボックスにしか表示できません。
この値(結果)を「テキスト」に設定して、ポップアップに表示されるようにしたいと思います。PHPファイルから値を取得する方法Jquery?
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() {
\t // create a new div element
\t // and give it popup content
\t var newDiv = document.createElement("div");
\t var texts;
\t
\t /////////////////////////////////////////////////////////////////////////////////////////
\t $(document).ready(function() {
$.ajax({
url:"index.php",
\t \t \t \t \t
success:function(result){
texts = result; // does not work
}
});
});
\t /////////////////////////////////////////////////////////////////////////////////////////
\t
\t
\t
\t 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>';
\t // Add The Background cover
\t var BG = document.createElement("div");
\t BG.style.width = '100%';
\t BG.style.height = '100%';
\t BG.style.background = 'black';
\t BG.style.position = 'fixed';
\t BG.style.top = '0';
\t BG.style.left = '0';
\t BG.style.opacity = '0.7';
\t BG.style.zIndex = '99900';
\t BG.style.display = 'none';
\t BG.setAttribute("id", "bgcover");
\t // add the newly created elements and its content into the DOM
\t document.body.appendChild(BG);
\t document.body.insertBefore(newDiv, BG);
\t // open popup onload
\t
\t
\t openPopup();
}
function openPopup() {
\t var el = document.getElementById('popup');
\t var BG = document.getElementById('bgcover');
\t el.style.display = 'block';
\t BG.style.display = 'block';
\t
\t
}
function tostoring() {
\t window.location.href = 'http://localhost/Sms%20management%20systeem/testing/storing.php';
}
function closePopup() {
\t var el = document.getElementById('popup');
\t var BG = document.getElementById('bgcover');
\t el.style.display = 'none';
\t BG.style.display = 'none';
}
results
値として 'XXXX' を得ました。
私は 'xxxx'を既にアラートボックスに入れていますが、それをjavascriptのvar texts
に割り当てる必要があります。
これを行う方法がわかりません。
誰でもこれを行う方法を知っていますか?
のindex.phpを得た:
<?php
$var = "xxxx";
echo json_encode($var);
?>
は私を助けてください!
変数 'texts'は、その関数内では定義されていません。そして、たとえそれがあったとしても、関数スコープ内のローカル変数の値を変更すると、何らかの形で外部の関数の値が変更されることは期待できません。それは恐ろしい問題につながります。コードはそれ以上は動作しません。なぜなら、あなたはそのような衝突を全部持っているからです。プログラミング言語では、可視性の範囲について読む必要があります。グローバル変数またはグローバルオブジェクトにバインドされたプロパティを使用できます。または、その値を受け入れる関数を呼び出すこともできます。 – arkascha
これはおそらく動作しますが、変数にアクセスしようとするとajax呼び出しがまだ終了していません。 ajax呼び出しの後でコードをsuccess関数内に移動する必要があります。 – jeroen
うわー、これまでのところ@jeroenありがとう –