後提交值我有創建使用生活在一個文件中基於瀏覽器的XMLHttpRequest對象或者叫的activeObject ajax.js
javascript函數HTML:user.php的
<ul id="list-of-developers">
<li class="list-title"><strong data-new-link="true">DEVELOPERS</strong>
</li>
</ul>
JAVASCRIPT:ajax.js
function ajaxObj2(meth, url, contype){
var xhttp;
if (window.XMLHttpRequest)
{
xhttp = new XMLHttpRequest();
}
else
{
xhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xhttp.open(meth, url, true);
xhttp.setRequestHeader("Content-type",contype);
xhttp.setRequestHeader("Access-Control-Allow-Origin", "http://localhost:9000/");
return xhttp;
}
function ajaxReturn(x){
if(x.readyState==4 && x.status ==200)
{
return true;
}
}
在其中包含我的HTML和包括ajax.js在文檔的頭部文件中的腳本標籤我user.php的文件。
這裏是JavaScript如何與PHP連接(注意:此標記位於文檔的body標籤外底)
JAVASCRIPT:user.php的(內置)
<script>
window.onload = function()
{
var u = "<?= $u ?>";
var r = "<?= $user_role ?>";
var x = ajaxObj2('GET', 'retrieveDevelopers.php','text/plain');
x.onreadystatechange = function()
{
alert("H u is: "+u+ " r is: "+r + " RESPONSE: " + x.responseText.trim());
if(ajaxReturn(x) ==true)
{
alert("got here");
var list_of_devs = document.getElementById("list-of-developers");
console.log(list_of_devs);
list_of_devs.innerHTML = x.responseText.trim();
}
}
console.log(u);
x.send("u="+u + "&r="+r);
}
</script>
PHP:在retrieveDevelopers.php
<?php
$x = $_GET["u"];
$m = $_GET["r"];
if(isset($_GET["u"]))
{
include_once("phpincludes/db_connx.php");
//echo
$person= preg_replace('#[^a-z0-9]#i','',$_GET["u"]);
$person_sql = "SELECT * FROM USERS WHERE USER_ID=(?) AND ACTIVATED=(?)";
$person_params = array($person, 1);
$person_options = array("Scrollable" => SQLSRV_CURSOR_CLIENT_BUFFERED);
$person_result = sqlsrv_query($db_connx, $person_sql,$person_params, $person_options);
$person_state = sqlsrv_num_rows($person_result);
if($person_state < 1)
{
echo "Failed to verify user";
exit();
}
else
{
echo("It Worked");
exit();
}
}
echo "failed";
exit();
?>
我得到的錯誤「注意:未定義的索引:你在第9行」和「注意:未定義的索引:R在10」
我不明白這是爲什麼。數據庫連接沒有問題。 這些文件都在同一個文件夾中,除了數據庫連接腳本
文件系統:
- /retrieveDevelopers.php
- /user.php
- /phpincludes.php/db_connx。 PHP
- /script/ajax.js
系統信息
Apache Version:2.4.17
PHP Version: 5.6.15
MICROSOFT SQL SERVER 2012
Localhost: localhost:9000
得到的是在URL沒有。你的URL應該看起來像'retrieveDevelopers.php?u =「+ u +」&r =「+ r' – kainaw
看一下ajaxObj2()函數,它將導致url值:」retrieveDevelopers.php?u = value&r =值「被創建 – tksisawesome
只需使用瀏覽器的調試器來確定您的ajaxObj2是否發送了所需的值,然後您就會知道在哪裏查找錯誤,客戶端或服務器端 – thisdotvoid