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 2 of 2
  1. #1
    Regular Coder
    Join Date
    Jan 2011
    Posts
    117
    Thanks
    27
    Thanked 0 Times in 0 Posts

    Pulling Users From An exsisting database on a shoutbox

    I would like to know how i can pull users from one of my mysql tables instead of having someone pick a username like the code i am going to provide:

    Shoutbox.php
    PHP Code:
    <?php
    /***************************/
    //@Author: Adrian "yEnS" Mato Gondelle & Ivan Guardado Castro
    //@website: www.yensdesign.com
    //@email: yensamg@gmail.com
    //@license: Feel free to use it, but keep this credits please!                    
    /***************************/

    /************************
        CONSTANTS
    /************************/
    define("HOST""YOUR HOST");
    define("USER""YOUR USER");
    define("PASSWORD""YOUR USER PASSWORD");
    define("DB""YOUR DATABASE");

    /************************
        FUNCTIONS
    /************************/
    function connect($db$user$password){
        
    $link = @mysql_connect($db$user$password);
        if (!
    $link)
            die(
    "Could not connect: ".mysql_error());
        else{
            
    $db mysql_select_db(DB);
            if(!
    $db)
                die(
    "Could not select database: ".mysql_error());
            else return 
    $link;
        }
    }
    function 
    getContent($link$num){
        
    $res = @mysql_query("SELECT date, user, message FROM shoutbox ORDER BY date DESC LIMIT ".$num$link);
        if(!
    $res)
            die(
    "Error: ".mysql_error());
        else
            return 
    $res;
    }
    function 
    insertMessage($user$message){
        
    $query sprintf("INSERT INTO shoutbox(user, message) VALUES('%s', '%s');"mysql_real_escape_string(strip_tags($user)), mysql_real_escape_string(strip_tags($message)));
        
    $res = @mysql_query($query);
        if(!
    $res)
            die(
    "Error: ".mysql_error());
        else
            return 
    $res;
    }

    /******************************
        MANAGE REQUESTS
    /******************************/
    if(!$_POST['action']){
        
    //We are redirecting people to our shoutbox page if they try to enter in our shoutbox.php
        
    header ("Location: index.html"); 
    }
    else{
        
    $link connect(HOSTUSERPASSWORD);
        switch(
    $_POST['action']){
            case 
    "update":
                
    $res getContent($link20);
                while(
    $row mysql_fetch_array($res)){
                    
    $result .= "<li><strong>".$row['user']."</strong><img src=\"css/images/bullet.gif\" alt=\"-\" />".$row['message']." <span class=\"date\">".$row['date']."</span></li>";
                }
                echo 
    $result;
                break;
            case 
    "insert":
                echo 
    insertMessage($_POST['nick'], $_POST['message']);
                break;
        }
        
    mysql_close($link);
    }


    ?>
    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 xmlns="http://www.w3.org/1999/xhtml" dir="ltr">
    <
    head>
        <
    meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <
    title>yensdesign.com Create a shoutbox using PHP and AJAX with jQuery</title>
        <
    link rel="stylesheet" href="css/general.css" type="text/css" media="screen" />
    </
    head>
    <
    body>
        <
    a id="logo" title="Go to yensdesign.com!" href="http://www.yensdesign.com"><img src="css/images/logo.jpg" alt="yensdesign.com" /></a>
        <
    form method="post" id="form">
            <
    table>
                <
    tr>
                    <
    td><label>User</label></td>
                    <
    td><input class="text user" id="nick" type="text" MAXLENGTH="25" /></td>
                </
    tr>
                <
    tr>
                    <
    td><label>Message</label></td>
                    <
    td><input class="text" id="message" type="text" MAXLENGTH="255" /></td>
                </
    tr>
                <
    tr>
                    <
    td></td>
                    <
    td><input id="send" type="submit" value="Shout it!" /></td>
                </
    tr>
            </
    table>
        </
    form>
        <
    div id="container">
            <
    ul class="menu">
                <
    li>Shoutbox</li>
            </
    ul>
            <
    span class="clear"></span>
            <
    div class="content">
                <
    h1>Latest Messages</h1>
                <
    div id="loading"><img src="css/images/loading.gif" alt="Loading..." /></div>
                <
    ul>
                <
    ul>
            </
    div>
        </
    div>
        <
    script type="text/javascript" src="jquery.js"></script>
        <script type="text/javascript" src="shoutbox.js"></script>
    </body>
    </html> 
    shoutbox.js
    PHP Code:
    /***************************/
    //@Author: Adrian "yEnS" Mato Gondelle & Ivan Guardado Castro
    //@website: www.yensdesign.com
    //@email: yensamg@gmail.com
    //@license: Feel free to use it, but keep this credits please!                    
    /***************************/

    $(document).ready(function(){
        
    //global vars
        
    var inputUser = $("#nick");
        var 
    inputMessage = $("#message");
        var 
    loading = $("#loading");
        var 
    messageList = $(".content > ul");
        
        
    //functions
        
    function updateShoutbox(){
            
    //just for the fade effect
            
    messageList.hide();
            
    loading.fadeIn();
            
    //send the post to shoutbox.php
            
    $.ajax({
                
    type"POST"url"shoutbox.php"data"action=update",
                
    complete: function(data){
                    
    loading.fadeOut();
                    
    messageList.html(data.responseText);
                    
    messageList.fadeIn(2000);
                }
            });
        }
        
    //check if all fields are filled
        
    function checkForm(){
            if(
    inputUser.attr("value") && inputMessage.attr("value"))
                return 
    true;
            else
                return 
    false;
        }
        
        
    //Load for the first time the shoutbox data
        
    updateShoutbox();
        
        
    //on submit event
        
    $("#form").submit(function(){
            if(
    checkForm()){
                var 
    nick inputUser.attr("value");
                var 
    message inputMessage.attr("value");
                
    //we deactivate submit button while sending
                
    $("#send").attr({ disabled:truevalue:"Sending..." });
                $(
    "#send").blur();
                
    //send the post to shoutbox.php
                
    $.ajax({
                    
    type"POST"url"shoutbox.php"data"action=insert&nick=" nick "&message=" message,
                    
    complete: function(data){
                        
    messageList.html(data.responseText);
                        
    updateShoutbox();
                        
    //reactivate the send button
                        
    $("#send").attr({ disabled:falsevalue:"Shout it!" });
                    }
                 });
            }
            else 
    alert("Please fill all fields!");
            
    //we prevent the refresh of the page after submitting the form
            
    return false;
        });
    }); 
    table.sql
    PHP Code:
    CREATE TABLE `shoutbox`(
        `
    idint(5NOT NULL auto_increment,
        `
    datetimestamp NOT NULL default CURRENT_TIMESTAMP,
        `
    uservarchar(25NOT NULL default 'anonimous',
        `
    messagevarchar(255NOT NULL default '',
        
    PRIMARY KEY (`id`)
    ); 
    All i want is for the user not to enter a "custom" name when they submit a shout. The shout box will be on a page where they are logged into their account. So i would like the shoutbox to pull there account username and show there username during a shout. I hope i said this to where someone can understand. Can i get some help with this please? Thank you in advanced guys!

    Regards,
    xxcorrosionxx

  • #2
    Senior Coder
    Join Date
    Jul 2011
    Posts
    1,226
    Thanks
    3
    Thanked 171 Times in 171 Posts
    You should return a query selecting the username, then fill it into the input with id 'nick' value attribute. But, it should be said, the user will still be able to edit this as it's an input box. Judging on the Javascript, it shouldn't matter what html entity type the information is in - as long as it has the id nick. So, I would consider changing that to a <span id="nick"> or some other container that you think is suitable, so that the user cannot change their username.


  •  

    Posting Permissions

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