|
|
|
| Вот не функционирует. Хочу, чтоб при вводе в input появлялась картинка. Вопрос не в том, нужно ли это реализовывать через аякс, вопрос в том, почему не оплучается.
index.html
<html xmlns="http://www.w3.org/xhtml">
<head>
<title>QUICKSTART</title>
<script type="text/javascript" src="quickstart.js"></script>
</head>
<body onload="process()">
Введите слово:
<input type="text" id="myName"><br>
А вот оно:<br>
<div id="divMassage"></div>
</body>
</html>
|
mp.js
var xmlHttp = createXmlHttpRequestObject();
function createXmlHttpRequestObject()
{
var xmlHttp;
if(window.ActiveXObject)
{
try
{
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
catch (e)
{
xmlHttp = false;
}
}
else
{
try
{
xmlHttp = new XMLHttpRequest();
}
catch (e)
{
xmlHttp = false;
}
}
if (!xmlHttp)
alert("Error creating the XMLHttpRequest object.");
else
return xmlHttp;
}
function process()
{
if (xmlHttp.readyState == 4 || xmlHttp.readyState == 0)
{
name = encodeURIComponent(document.getElementById("myName").value);
if(name.length > 0) {
xmlHttp.open("GET", "mp.php?name=" + name, true);
}
xmlHttp.onreadystatechange = handleServerResponse;
xmlHttp.send(null);
}
}
function handleServerResponse()
{
if (xmlHttp.readyState == 4)
{
if (xmlHttp.status == 200)
{
xmlResponse = xmlHttp.responseXML;
xmlDocumentElement = xmlResponse.documentElement;
helloMessage = xmlDocumentElement.firstChild.data;
document.getElementById("divMessage").innerHTML =
'<i>' + helloMessage + '</i>';
}
else
{
alert("There was a problem accessing the server: " + xmlHttp.statusText);
}
}
}
|
mp.php
<?php
header("content-type: text/xml");
echo '<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>';
echo '<response>';
$name = $_GET['name'];
if ($name != '') echo "<img src='logo38.jpg'>";
echo "</response>";
?>
|
Пожалуйста, укажите на ошибку, ибо не возможно продолжать изучать дальше при ступаре в самом начале. | |
|
|
|
|
|
|
|
для: Bubba
(14.09.2008 в 21:03)
| | я сам не очень в аджаксе - немного баловался, у Вас в первом файле идет ссылка на
<script type="text/javascript" src="quickstart.js"></script>
|
в то время как файл называется mp.js
пляс в самом аджаксе, по идее надо прописать полный путь:
url = 'http://localhost/vash_site/mp.js/mp.php?name='; //это скрипт, который вернет какие то данные
|
xmlHttp.open("GET", url+name.value, true);
|
возможно в input вставить onclick | |
|
|
|
|
|
|
|
для: Bubba
(14.09.2008 в 21:03)
| |
///Вот это оригинально)
xmlHttp.open("GET", "mp.php?name=" + name, true);
//...
xmlHttp.send(null);
//Обычно делается так:
xmlHttp.open("GET", "mp.php", true);
//...
xmlHttp.send("name=" + name);
|
| |
|
|
|
|
|
|
|
для: Bubba
(14.09.2008 в 21:03)
| | Вообще можно проще...
Да и смотрите сами, это рабочий вариант:
<html>
<head>
<title>AJAX</title>
<script type="text/javascript">
var httpRequest = createHttpRequest();
var resultId = '';
function createHttpRequest()
{
var httpRequest;
var browser = navigator.appName;
if (browser == "Microsoft Internet Explorer") {
httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
}else{
httpRequest = new XMLHttpRequest();
}
return httpRequest;
}
function sendRequest(file, _resultId, getRequestProc)
{
var txt = document.getElementById("txt").value;
httpRequest.open('get', file+'?name=' + txt, true);
httpRequest.onreadystatechange = getRequestProc;
httpRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=windows-1251");
httpRequest.send(null);
}
function getRequest()
{
if (httpRequest.readyState == 4){
window.document.getElementById('result').innerHTML = httpRequest.responseText;
}else{
document.getElementById('result').innerHTML = "<img src='preloader.gif'>";
}
}
</script>
<meta content="text/html; charset=windows-1251"></head>
<body>
<input type="text" id="txt" onKeyPress="if (event.keyCode==13) sendRequest('ajaxtext.php','result',getRequest);">
<p>Нажмите ввод</p>
<div id="result"></div>
</body>
</html>
|
ajaxtext.php
<?php
if(isset($_GET['name'])){
$name= $_GET['name'];
}
else{
$name= null;
}
sleep(3);//Это для наглядности работы прелоадера
echo $name;
?>
|
Вот http://test.inkz.ru/demo/ajax.html можно посмотреть. | |
|
|
|
|
|
|
|
для: Николай2357
(16.09.2008 в 22:57)
| | Хороший вариант. спасибо. И спасибо модерам, что перенесли тему. | |
|
|
|