Loading:


Pobranie listy odnośników ze strony - AJAX i PHP

Skrypt pobiera/sprawdza linki umieszczone na podanej stronie.



Napisz Artyku³

Listing


//index.html   #############################################

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
   "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Ajax</title>
<link href="ajaxcss.css" rel="stylesheet" type="text/css">
<script src="ajaxcore.js" type="text/javascript"></script>
<script src="ajax.js" type="text/javascript"></script>
</head>
<body>
<div id="mainDiv" class="mainDiv">
<div>
  <a href="http://helion.pl">http://helion.pl</a><br>
  <a href="http://marcinlis.com">http://marcinlis.com</a><br>
  <a href="http://google.comp">http://google.comp</a><br>
</div>
<div style="width:50%;margin:0 auto;text-align:center;">
  <input type="button" value="Sprawdź" class="myButton" id="btnSprawdz"
         onclick="startRequest();" style="margin-top:10px;" />
</div>
</div>
<div id="dataDiv" class="dataDiv">
</div>
</body>
</html>





//dane.php   #############################################

<?php
if(isSet($_GET["urls"]) && $_GET["urls"] != ""){
  $urls = explode("\n", $_GET["urls"]);
  $good = Array();
  $bad =  Array();
  foreach($urls as $url){
    if($id = @fopen(trim($url), 'r')){
      $good[] = $url;
      fclose($id);
    }
    else{
      $bad[] = $url;
    }
  }
  echo "<b>Adresy poprawne</b>:<br>";
  foreach($good as $url){
    echo $url . "<br>";
  }
  echo "<b>Adresy niepoprawne</b>:<br>";
  foreach($bad as $url){
    echo $url . "<br>";
  }
}
else{
  echo "Proszę podać adresy do sprawdzenia.";
}
?>








//ajaxcss.css   #############################################

.mainDiv
{
  color: #333333;
  position: relative;
  background-color: #EFEFEF;
  border: 1px solid #000000;
  margin: 10px 10px 10px 10px;
  padding: 14px 14px 14px 14px;
  width: 350px;
  visibility: visible;
}

.dataDiv
{
  color: #333333;
  position: relative;
  background-color: #ffffff;
  border: 1px solid #000000;
  margin: 10px 10px 10px 10px;
  padding: 14px 14px 14px 14px;
  width: 420px;
  visibility: visible;
}

.myButton
{
  font-family:helvetica,sans-serif;
  font-size:84%;
  font-weight:bold;
  border:1px solid;
  border-top-color:#696;
  border-left-color:#696;
  border-right-color:#363;
  border-bottom-color:#363;
  width:120px;
  margin:5px;
}

.myTextInput
{
  border:1px solid;
  background-color: #F8F8F8;
  border-top-color:#696;
  border-left-color:#696;
  border-right-color:#363;
  border-bottom-color:#363;
  width:70%;
}



//ajaxcore.js   #############################################

function getXMLHttpRequestObject()
{
  try{
    return new XMLHttpRequest();
  }
  catch(e){
    try{
      return new ActiveXObject("Microsoft.XMLHTTP");
    }
    catch(e){
      return false;
    }
  }
}

function startGETRequest(url, onComplete, onEnd)
{
  var XMLHttpRequestObject = getXMLHttpRequestObject();
  if(XMLHttpRequestObject){
    XMLHttpRequestObject.open("GET", url);
    XMLHttpRequestObject.onreadystatechange = function()
    {
      if (XMLHttpRequestObject.readyState == 4){
        if(XMLHttpRequestObject.status == 200){
          var responseXML = XMLHttpRequestObject.responseXML;
          var responseText = XMLHttpRequestObject.responseText;
          onComplete(responseText, responseXML);
        }
        delete XMLHttpRequestObject;
        onEnd();
      }
    }
    XMLHttpRequestObject.send(null);
  }
}

function startPOSTRequest(url, params, onComplete, onEnd)
{
  var XMLHttpRequestObject = getXMLHttpRequestObject();
  if(XMLHttpRequestObject){
    XMLHttpRequestObject.open("POST", url);
    XMLHttpRequestObject.setRequestHeader(
      'Content-Type', 'application/x-www-form-urlencoded');
    XMLHttpRequestObject.onreadystatechange = function()
    {
      if (XMLHttpRequestObject.readyState == 4){
        if(XMLHttpRequestObject.status == 200){
          var responseXML = XMLHttpRequestObject.responseXML;
          var responseText = XMLHttpRequestObject.responseText;
          onComplete(responseText, responseXML);
        }
        delete XMLHttpRequestObject;
        onEnd();
      }
    }
    XMLHttpRequestObject.send(params);
  }
}


//ajax.js   #############################################

function startRequest()
{
  var btnSprawdz = document.getElementById('btnSprawdz');
  btnSprawdz.value = "Czekaj";
  btnSprawdz.disabled = true;

  var urlsArr = document.getElementsByTagName('a');
  urlsTxt = "";
  for(i = 0; i < urlsArr.length; i++){
    urlsTxt += urlsArr[i].href + "\n";
  }

  url = "http://localhost/dane.php?urls=" + urlsTxt;
  url = encodeURI(url);

  div = document.getElementById('dataDiv');
  div.innerHTML = "...";

  startGETRequest(url, onComplete, onEnd);
}

function onComplete(text, xml)
{
  var div = document.getElementById('dataDiv');
  div.innerHTML = text;
}

function onEnd()
{
  var btnSprawdz = document.getElementById('btnSprawdz');
  btnSprawdz.value = "Sprawdź";
  btnSprawdz.disabled = false;
}
 




Dodano przez: igor
Ranga: Administrator serwisu Punktów: 0
Komentarze użytkowników
    • Tre¶æ komentarza
      Kod do komentarza (opcjonalnie)
      PHP JavaScript MySQL Smarty SQL HTML CSS ActionScript
      Autor
      Token
      token

       

       








funkcje.net
Wszelkie prawa zastrzeżone©. | Funkcje.net 2008-2024 v.1.5 | design: diviXdesign & rainbowcolors