Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Jul 2009
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Insert and Load Record using prototype

    I am looking some example to insert comments and show comments wihout refreshing the page using prototype .

    Any help ?

  • #2
    Regular Coder hinch's Avatar
    Join Date
    Sep 2005
    Location
    UK
    Posts
    923
    Thanks
    25
    Thanked 80 Times in 80 Posts
    index.php

    PHP Code:
    <!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 Code:
    <?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>';
        }
    }
    ?>
    A programmer is just a tool which converts caffeine into code

    My work: http://www.fcsoftware.co.uk && http://www.firstcontactcrm.com
    My hobby: http://www.angel-computers.co.uk
    My life: http://www.furious-angels.com

  • #3
    Regular Coder hinch's Avatar
    Join Date
    Sep 2005
    Location
    UK
    Posts
    923
    Thanks
    25
    Thanked 80 Times in 80 Posts
    you can ignore the autocomplete stuff
    A programmer is just a tool which converts caffeine into code

    My work: http://www.fcsoftware.co.uk && http://www.firstcontactcrm.com
    My hobby: http://www.angel-computers.co.uk
    My life: http://www.furious-angels.com


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •