Auto Suggest Application / AJAX /
index.html
<html>
<head>
<script type="text/javascript">
function findmatch(){
if(window.XMLHttpRequest){
xmlhttp=
new XMLHttpRequest();
} else {
xmlhttp=new
ActiveXObject('Microsoft.XMLHTTP');
}
xmlhttp.onreadystatechange=function(){
if(xmlhttp.readyState
==4 && xmlhttp.status==200){
document.getElementById('results').innerHTML
= xmlhttp.responseText;
}
}
xmlhttp.open('GET',
'search.inc.php?search_text='+document.search.search_text.value, true);
xmlhttp.send();
}
</script>
</head>
<body>
<form id="search" name="search">
Type a
name:<br>
<input
type="text" name="search_text"
onkeydown="findmatch();">
</form>
<div id="results"> </div>
</body>
</html>
Search.inc.php
<?php
if(isset($_GET['search_text'])){
$search_text=$_GET['search_text'];
}
if(!empty($search_text)){
if(@mysql_connect('localhost',
'root', '')){
if(@mysql_select_db('a_database')){
$query=
"SELECT name FROM names WHERE name LIKE
'".mysql_real_escape_string($search_text)."%'";
$query_run=mysql_query($query);
while($query_row=mysql_fetch_assoc($query_run)){
//
echo $name=$query_row['name'].'<br>';
echo
$name='<a
href="anotherpage.php?search_text='.$query_row['name'].'">'.$query_row['name'].'</a><br>';
}
}
}
}
?>
POSTing Data / AJAX /
Index.php
<html>
<head>
<script type="text/javascript">
function insert(){
// var text_value
= document.getElementById('insert_text').value;
//
alert(text_value);
if(window.XMLHttpRequest){
xmlhttp=
new XMLHttpRequest();
} else {
xmlhttp=new
ActiveXObject('Microsoft.XMLHTTP');
}
xmlhttp.onreadystatechange=function(){
if(xmlhttp.readyState
==4 && xmlhttp.status==200){
document.getElementById('message').innerHTML
= xmlhttp.responseText;
}
}
parameters='text='+document.getElementById('insert_text').value;
xmlhttp.open('POST',
'update.inc.php', true);
xmlhttp.setRequestHeader('Content-type',
'application/x-www-form-urlencoded');
xmlhttp.send(parameters);
}
</script>
</head>
<body>
Insert: <input type="text" id="insert_text">
<input type="button" value ="Submit"
onclick="insert();">
<div id="message"> </div>
</body>
</html>
Update.inc.php
<?php
// echo $_POST['text'];
@mysql_connect('localhost', 'root', '');
@mysql_select_db('a_database');
if(isset($_POST['text'])){
$text =
$_POST['text'];
if(!empty($text)){
$query="INSERT
INTO data VALUES('', '".mysql_real_escape_string($text)."')";
if($query_run=mysql_query($query)){
echo
'Data inserted!';
}
else {
echo
'Failed';
}
} else {
echo 'Please type something.';
}
}
?>
Try, throw, catch
<?php
$age=16;
try{
if($age>18)
echo 'Old enough';
else throw new
Exception('Not old enough');
} catch(Exception $ex){
echo 'Error:
'.$ex->getMessage();
}
?>
No comments:
Post a Comment