...

View Full Version : Insert and Load Record using prototype



zahidraf
09-15-2009, 08:18 AM
I am looking some example to insert comments and show comments wihout refreshing the page using prototype .

Any help ?

hinch
09-15-2009, 01:28 PM
index.php



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="en" xml:lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<title>Ajax test</title>
<script type="text/javascript" src="js/prototype.js"></script>
<script src="js/scriptaculous.js" type="text/javascript"></script>
<script>
function dbadd() {
var inputtext = $F('inputtext');
var url = './xml.php';
var pars = 'action=add&input=' + inputtext;
var myAjax = new Ajax.Updater({success: 'result'},url,
{
method:'post',
parameters: pars,
onFailure: reportError
});
}
function dbget() {
var url = './xml.php';
var pars = 'action=get';
var myAjax = new Ajax.Updater({success: 'retrieved'},url,
{
method:'post',
parameters: pars,
onFailure: reportError
});
}
function dbupdate() {
var updatetext = $F('updatetext');
var updateid = $F('updateid');
var url = './xml.php';
var pars = 'action=update&input=' + updatetext + '&id=' + updateid;
var myAjax = new Ajax.Updater({success: 'confirmupdate'},url,
{
method:'post',
parameters: pars,
onFailure: reportError
});
}


new Ajax.Autocompleter("autocomplete", "autocomplete_choices", "./autocomplete.php", {
paramName: "value",
minChars: 2,
indicator: 'indicator1'
});




function reportError(request)
{
alert('Sorry. There was an error.');
}
</script>
<style>
div.autocomplete {
position:absolute;
width:250px;
background-color:white;
border:1px solid #888;
margin:0px;
padding:0px;
}
div.autocomplete ul {
list-style-type:none;
margin:0px;
padding:0px;
}
div.autocomplete ul li.selected { background-color: #ffb;}
div.autocomplete ul li {
list-style-type:none;
display:block;
margin:0;
padding:2px;
height:32px;
cursor:pointer;
}
</style>
</head>
<body>

<input type="text" id="autocomplete" name="autocomplete_parameter"/>
<span id="indicator1" style="display: none">
<img src="images/spinner.gif" alt="Working..." />
</span>
<div id="autocomplete_choices" class="autocomplete"></div>



<div id="myDiv">
<p><input type="text" value="" id="inputtext" /></p>
</div>
<div id="myOtherDiv">
<p id="result"></p>
</div>
<input type="button" value="Add to DB" onclick="dbadd();"/><br/>
<br/><br/><br/>
<div id="myOtherDiv">
<p id="retrieved"></p>
</div>
<input type="button" value="Retrieve DB" onclick="dbget();"/><br/>
<br/>
<br/><br/><br/>
<div id="myDiv">
<p>ID <input type="text" value="" id="updateid" /></p>
<p>New Text<input type="text" value="" id="updatetext" /></p>
</div>
<div id="myOtherDiv">
<p id="confirmupdate"></p>
</div>
<input type="button" value="Update DB Entry" onclick="dbupdate();"/><br/>
<br/>
</body>
</html>


xml.php


<?php
// Setup Databse Connection
$serverAddress = "localhost";
$databaseToUse = "ajaxtest";
$databaseUsername = "root";
$databasePassword = "";

// DB Connection String
$database = mysql_pconnect($serverAddress, $databaseUsername, $databasePassword);
mysql_select_db($databaseToUse,$database);

if ($_POST['action']=="add") {
$insertsql = "insert into `testing` (inputText) values ('".mysql_real_escape_string($_POST['input'])."')";
$result = mysql_query($insertsql);
if (!$result) {
die('Invalid query: ' . mysql_error());
} else {
echo 'Successfully added <strong>'.$_POST['input'].'</strong> to the database';
}
}
if ($_POST['action']=="get") {
$insertsql = "SELECT * FROM `testing` order by ID ASC";
$result = mysql_query($insertsql);
if (!$result) {
die('Invalid query: ' . mysql_error());
} else {
while ($resultlist = mysql_fetch_array($result)) {
echo $resultlist[0].",".$resultlist[1]."<br />";
}
}
}
if ($_POST['action']=="update") {
$updatesql = "update `testing` set inputText='".mysql_real_escape_string($_POST['input'])."' where ID=".$_POST['id'];
$result = mysql_query($updatesql);
if (!$result) {
die('Invalid query: ' . mysql_error());
} else {
echo 'Successfully updated DB entry <strong>'.$_POST['id'].'</strong> with the data <strong>'.$_POST['input'].'</strong>';
}
}
?>

hinch
09-15-2009, 01:28 PM
you can ignore the autocomplete stuff :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum