Jump to content

check if a web link exists before it is added the mysql


jasonc

Recommended Posts

I wish to have a method to check the URL link prior to it being added to the mysql database.

 

I have a basic form, URL, Title.

 

If and only if there is content in the URL field and once the cursor is out of the URL field and in the TItle, which is the second field down on the page, if the URL exists then there would be a red message above the URL field to say that, that link already exists in the database.

 

can someone please help me out here as i have no idea what to do

 

also not sure that the 'check if username exists' script would work for this if changed a bit as i think it may show this message while the text is being inputted,  correct me if i am wrong.

 

 

Link to comment
Share on other sites

ok i'll start from the very start again, but explain each part.

 

i have a form that the members enter a URL and a description in, URL, Title.

 

I wish to have the page show if the URL exisits in my mysql database prior to it being inserted in the database.

 

Mysql table has a field called as you would expect, 'url' and 'title'

 

I need the page to check if the 'url' entered in the form field is already in the database.

 

If it does then it would display a red, 'ALREADY EXISITS' beside the 'url' field in the form.

 

but only when the url field is not in focus, meaning that they have moved down to the next field being the 'title' field of the form, as they could for example be entering 'www.microsoft.com/'  and the database may already contain a link that starts with what they have already typed, so only getti8ng the page to check when they are not in the 'url' field will ensure that they have finished typing in the url.

 

if that make sense.

 

think i have covered everything that i would like to have done in the page.

 

are you able to help out with this?

 

cheers

Link to comment
Share on other sites

So you need basically two things.

 

1. An Ajax part: some JavaScript that will check if URL field has been filled, and focused out. After that it will send request to validate it.

2. A PHP script, that will check if given URL exists in MySQL database and prints out a reply, that your Ajax script can understand.

 

I'm not good at creating JavaScript code (I use ExtJS framework for all Ajax related stuff I need). I could help you with PHP part... but it doesn't make much sense without having JavaScript that creates a request first...

Link to comment
Share on other sites

serverside script that will check if the url exisits and then 'return' a value say 'exisits' or notexisits'  this i can do no problem.

 

its the client side? scripting that i have no idea about, how do i get the client side to send the url to the 'check_if_url_exisits.php' script ?

 

and then how do i get the client to receive the return value from the script.

 

 

Link to comment
Share on other sites

You don't access MySQL directly. You have to send request to PHP script.

 

Consider this example:

http://www.w3schools.com/Ajax/ajax_example_suggest.asp

 

Here they define, which script on server side will the request be send to. You can change it to 'check_url_in_db.php'

var url="gethint.asp";

 

Then they add a query string. In your case that would be a URL to check.

url=url+"?q="+str;

 

After that they send request to the server side script, which does it's work and returns some results.

 

 

They also show function stateChanged(), that basically monitors if the server side scripts have send any replies, and does whatever is needed to be done, when reply is received. (In this case it fills the text field with text received from script)[/code]

Link to comment
Share on other sites

i realise i am not making myself very clear as i have no idea what i am talking about ! :-(

 

but slowly i am finding out things.

 

ok i understand that the client side can not access the Db directly, and now see that i would have to send say a request if i have understood what you mean using something like this

 

-----------------------

display form

 

check if url has data and if not in focus

if data and not in focus then send request to say...

www.site.com/checkurl.php?url=www.theirsite.com

 

then the server would get a visit from the form page to this checkurl.php file and use the info in $_GET['url'] to check the DB for and return the reply needed by the form page to either show or not show the message if the url exisits or not

 

what i do not know is how i get the page to send this request or how i get the reply

Link to comment
Share on other sites

Check the example I posted before.

 

This line:

xmlHttp.onreadystatechange=stateChanged;

says, "when the server side scripts replies, call function stateChanged()"

 

 

These two lines:

xmlHttp.open("GET",url,true);
xmlHttp.send(null);

are for sending request.

 

And the function stateChanged() is for processing reply.

Link to comment
Share on other sites

ok after reading opver and over again i have now come up with the following code.

 

form.php

<html>

<head>
<script type="text/javascript">
function checkurl(str)
{
if (str.length==0)
  { 
  document.getElementById("txtHint").innerHTML="";
  return;
  }
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null)
  {
  alert ("Your browser does not support AJAX!");
  return;
  } 
var url="checkurl.php";
url=url+"?url="+str;
url=url+"&sid="+Math.random();
xmlHttp.onreadystatechange=stateChanged;
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
}
</script>
</head>

<body>
<form> 
First Name:
<input type="text" id="txt1"
onkeyup="showHint(this.value)">
</form>

<p><span id="txtHint"></span></p> 

</body>
</html>

 

and this which would check if it exisits or not.

 

 

checkurl.php

<?
$url = $_GET['url'];

?>

 

but thats as far as i can go with what i have read in the link you gave.

 

it does not say how i send the response back from the checkurl.php file back to the form.php page

Link to comment
Share on other sites

In this context, you would need to create a stateChanged function which will handle the result..

 

function stateChanged()
{
  if(xmlHttp.status == 200 && xmlHttp.readyState == 4)
  {
     document.getElementById("txtHint").innerHTML = xmlHttp.responseText;
  }
}

responseText = the output of checkurl.php

 

However, this is not how I usually do it, so it may not be exact for your needs.

Link to comment
Share on other sites

i realised i missed out some of the code. oops

 

 

also i clicked a wrong link on w3 and found the 'gethint.php' file.

 

but still it does not update the part of the page it should and i also have it show text for both does and does not exisit.

 

form.php

<html>

<head>
<script type="text/javascript">
function GetXmlHttpObject()
{
var xmlHttp=null;
try
  {
  // Firefox, Opera 8.0+, Safari
  xmlHttp=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
  }
return xmlHttp;
}

function stateChanged() 
{ 
if (xmlHttp.readyState==4)
{ 
document.getElementById("txtHint").innerHTML=xmlHttp.responseText;
}
}

function checkurl(str)
{
if (str.length==0)
  { 
  document.getElementById("txtHint").innerHTML="";
  return;
  }
xmlHttp=GetXmlHttpObject();
if (xmlHttp==null)
  {
  alert ("Your browser does not support AJAX!");
  return;
  } 
var url="checkurl.php";
url=url+"?url="+str;
url=url+"&sid="+Math.random();
xmlHttp.onreadystatechange=stateChanged;
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
}
</script>
</head>

<body>
<form> 
First Name:
<input type="text" id="txt1"
onkeyup="showHint(this.value)">
</form>

<p>.<span id="txtHint"></span>.</p> 

</body>
</html>

 

and the new checkurl.php

<?php
header("Cache-Control: no-cache, must-revalidate");
// Date in the past
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");

$url = $_GET['url'];

if ($url == "www.microsoft.com")
{
$response="exisits";
}
else
{
$response="nil";
}

echo $response;
?>

Link to comment
Share on other sites

arrhh  problem...

 

ok i have added it to my code in my admin area where we add URL's

 

but now it does not work, so i thought i'd add in a

 

alert(test');

 

to the end of the javascript to test if it gets to that point and it does, for every letter i type.

 

but the area that should update which is the same code unaltered yet until this works then i was going to make one change at a time to make sure it works each step of the way.

 

but no change to that area.  not even the 'not in DB' or the 'in DB' text shows up

 

this is the file sctructure of the working test folders...

root...

root/js/AJAXcheckurl.js

root/testAJAX/testAJAX.php

root/testAJAX/checkurl.php

 

and this is the one i have for my live site......

root...

root/js/AJAXcheckurl.js

root/admin/checkurl.php

root/admin/addurl.php (as in testAJAX.php)

 

now the last file addurl.php is made up of lost of incluide()'s to bring the whole page together.

 

now what i wondered is would this be the reason it is not updating the special field that tells us if the url exisits or not.

 

or do i need to have some of this code at the top of the page?

 

reason i am thinking this is same with 'headers' for html you cannot alter the headers once some headers have been sent.

is this also a bit the same with some of the javascript?

a shot in the dark really, but thought i'd have a guess at it.

 

if not do you know why this might not be working.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.