jasonc Posted November 8, 2008 Share Posted November 8, 2008 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. Quote Link to comment Share on other sites More sharing options...
jasonc Posted November 8, 2008 Author Share Posted November 8, 2008 just re-read it and not wanting to check that the url is valid. only check if a web link exists in mysql DB before it is added the mysql Quote Link to comment Share on other sites More sharing options...
jasonc Posted November 9, 2008 Author Share Posted November 9, 2008 does anyone use AJAX anymore?! I have never used it and hoped that coming here might get a few tips on how i start Quote Link to comment Share on other sites More sharing options...
Ken2k7 Posted November 9, 2008 Share Posted November 9, 2008 AJAX can't 100% determine if a URL exists. Actually, there is little chance it'll work because if a website is hosted on another server, AJAX won't work. I think PHP's cURL may work though. Quote Link to comment Share on other sites More sharing options...
jasonc Posted November 9, 2008 Author Share Posted November 9, 2008 no no sorry i posted a reply to my original post explaining this. not if the website is valid. ajax to search my database for the url being entered Quote Link to comment Share on other sites More sharing options...
Mchl Posted November 9, 2008 Share Posted November 9, 2008 Ajax can't search your database. You have to call a server side script that will do that and return a reply with required information (wether the site exists in DB or not) Quote Link to comment Share on other sites More sharing options...
jasonc Posted November 9, 2008 Author Share Posted November 9, 2008 how do i do this? Quote Link to comment Share on other sites More sharing options...
Mchl Posted November 9, 2008 Share Posted November 9, 2008 Which part you're asking about? And don't try to tell me 'all of them' because if this is the case, you should split your question into several parts (and topics) Quote Link to comment Share on other sites More sharing options...
jasonc Posted November 9, 2008 Author Share Posted November 9, 2008 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 Quote Link to comment Share on other sites More sharing options...
Mchl Posted November 9, 2008 Share Posted November 9, 2008 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... Quote Link to comment Share on other sites More sharing options...
jasonc Posted November 9, 2008 Author Share Posted November 9, 2008 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. Quote Link to comment Share on other sites More sharing options...
Mchl Posted November 9, 2008 Share Posted November 9, 2008 Well... that's what's Ajax is all about. I suugest W3Schools tutorials, to get some basic understanding. Quote Link to comment Share on other sites More sharing options...
jasonc Posted November 9, 2008 Author Share Posted November 9, 2008 i have already tried there and even taken another look today, but there is nothing to explain how i access mysql. Quote Link to comment Share on other sites More sharing options...
Mchl Posted November 9, 2008 Share Posted November 9, 2008 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] Quote Link to comment Share on other sites More sharing options...
jasonc Posted November 9, 2008 Author Share Posted November 9, 2008 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 Quote Link to comment Share on other sites More sharing options...
Mchl Posted November 9, 2008 Share Posted November 9, 2008 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. Quote Link to comment Share on other sites More sharing options...
jasonc Posted November 9, 2008 Author Share Posted November 9, 2008 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 Quote Link to comment Share on other sites More sharing options...
xtopolis Posted November 9, 2008 Share Posted November 9, 2008 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. Quote Link to comment Share on other sites More sharing options...
jasonc Posted November 9, 2008 Author Share Posted November 9, 2008 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; ?> Quote Link to comment Share on other sites More sharing options...
jasonc Posted November 9, 2008 Author Share Posted November 9, 2008 i had the incorrect function name in the main form.php file should have been checkurl but i still had the original files content being getHint now it works in IE and now in FireFox 3.0.3 THANK YOU FOR YOUR HELP Quote Link to comment Share on other sites More sharing options...
xtopolis Posted November 9, 2008 Share Posted November 9, 2008 works in FF for me as well. good job Quote Link to comment Share on other sites More sharing options...
jasonc Posted November 10, 2008 Author Share Posted November 10, 2008 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. Quote Link to comment Share on other sites More sharing options...
xtopolis Posted November 10, 2008 Share Posted November 10, 2008 Use Firefox . Look at Tools->ErrorConsole. it will tell you if there are any errors from Javascript. if none, add: error_reporting(E_ALL); ini_set('display_errors',1); to the topmost php page, to see if it's a php error. Quote Link to comment Share on other sites More sharing options...
Mchl Posted November 10, 2008 Share Posted November 10, 2008 For firefox install Firebug plugin. Extremely helpful in debugging JavaScript and Ajax. Quote Link to comment Share on other sites More sharing options...
jasonc Posted November 10, 2008 Author Share Posted November 10, 2008 Error: Permission denied to call method Location.toString Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.