25 users online (0 members and 25 guests)  


  Results 1 to 5 of 5

Related

  1. onSubmit sends email of form data    Forum: HTML Forum
    Replies: 2
  2. myspace log in trouble.    Forum: Myspace Forum
    Replies: 3
  3. Perl Trouble    Forum: CGI Perl Forum
    Replies: 0
  4. Table Trouble    Forum: HTML Forum
    Replies: 19
  1. #1
    Nesper's Avatar
    New User

    Status
    Offline
    Join Date
    Aug 2004
    Location
    Melbourne, Australia
    Posts
    9

    onSubmit trouble

    Hiyas,

    I'm working on a live chat script using a mixture of php, mysql and js.
    For the most part it works, but i have a slight problem with the form. As a user, if you type in your message and click the chat button everything works fine however if you just type in your message and hit enter the message gets submitted to the mysql db however the doSubmit function(which adds the colour chosen from a dropdown box to your entry and empties and refocuses the text box in the form - code shown below) dosen't work. Is there a way to call the function by hitting the enter key?

    The form is like:

    Code:
    <form onSubmit="return doSubmit" name="chatform" method="post" action="chat.php" target="posts"> 
    
    <input type="text" name="chat" size="70"> 
    
    <script language="JavaScript">
    
    document.chatform.chat.focus();
    
    </script>
    
    <input type="hidden" name="nick" value="<?php echo $nick; ?>"> 
    
    <input type="button" onclick="doSubmit()" name="Submit" value="Chat"> 
    
    <select name="col"> 
    
    <option>Black</option> 
    
    <option>Red</option> 
    
    <option>Green</option> 
    
    <option>Blue</option> 
    
    <option>Magenta</option> 
    
    </select> 
    
    
    <a href="javascript:sendFace(1)"><img src="/forum/YaBBImages/smiley.gif" border=0 name="DoFace1"></a>
    
    <a href="javascript:sendFace(2)"><img src="/forum/YaBBImages/sad.gif" border=0 name="DoFace2"></a>
    
    <a href="javascript:sendFace(3)"><img src="/forum/YaBBImages/grin.gif" border=0 name="DoFace3"></a>
    
    <input type="hidden" name="action" value="posts"> 
    
    </form>
    The doSubmit function is as follows:

    Code:
    function doSubmit() 
    
    {
    
    if(document.chatform.chat.value == '') { 
    
    alert('Please enter some text!'); 
    
    document.chatform.chat.focus(); 
    
    return false;
    
    }
    
    document.chatform.chat.value =document.chatform.chat.value.replace(/(<([^>]+)>)/ig,""); 
    
    document.chatform.chat.value = '<font color="'+document.chatform.col[document.chatform.col.selectedIndex].text+'">'+document.chatform.chat.value+'</font>';
    
    document.chatform.submit(); 
    
    document.chatform.chat.value = ''; 
    
    document.chatform.chat.focus();
    
    return true;
    
    }
    Thanks in advance,
    Nesper.

  2. #2
    Nesper's Avatar
    New User

    Status
    Offline
    Join Date
    Aug 2004
    Location
    Melbourne, Australia
    Posts
    9
    if you need anymore info or you don't quite understand my problem let me know.

    You can try out the chat http://www.nesperion.net to see what i'm talking about but it's part of an online game i'm working on so you will have to have to create a character first.

  3. #3
    QuietDean's Avatar
    Administrator

    Status
    Offline
    Join Date
    Oct 2000
    Location
    Bournemouth, UK
    Posts
    2,662
    In your onSubmit in the <form> tag, shouldn't it be doSubmit() rather than just doSubmit ?
    If one of our members helps you, please click the icon to add to their reputation!
    No support via email or private message - use the forums!
    Before you ask, have you Searched?

  4. #4
    Nesper's Avatar
    New User

    Status
    Offline
    Join Date
    Aug 2004
    Location
    Melbourne, Australia
    Posts
    9
    Yeah i tried onSubmit="return doSubmit()" and onSubmit ="doSubmit()" with both of these when you hit enter the text field in the form empties and refocuses but the form does not submit the info. When you hit the Chat button to submit it does work though.

    with onSubmit="return doSubmit" without the () when you hit enter the info in the text box gets submitted but the colour from the select box dosen't, and the text stays in the text box. Everything works when you hit the chat button instead of enter.

    It's got me boggled

  5. #5
    Nesper's Avatar
    New User

    Status
    Offline
    Join Date
    Aug 2004
    Location
    Melbourne, Australia
    Posts
    9
    ok here's the whole script
    PHP Code:
    <?php

    session_start
    (); 
    ?>
    <head><link rel="stylesheet" type="text/css" href="template.css" title="style" />

    <script language="JavaScript">

    function doSubmit() 

    {

    if(document.chatform.chat.value == '') { 

    alert('Please enter some text!'); 

    document.chatform.chat.focus(); 

    return false;

    }

    document.chatform.chat.value =document.chatform.chat.value.replace(/(<([^>]+)>)/ig,""); 

    document.chatform.chat.value = '<font color="'+document.chatform.col[document.chatform.col.selectedIndex].text+'">'+document.chatform.chat.value+'</font>';

    document.chatform.submit(); 

    document.chatform.chat.value = ''; 

    document.chatform.chat.focus();

    return true;

    }

    </script>

    </head>


    <?php

    function Login() {

    global 
    $chat

    $chat stripslashes($chat);

    $chat addslashes($chat);

    global 
    $nick

    if (
    $_SESSION['access_level'] >=10){

    $nick =("<img src=\'/images/mod2\.GIF\' border=0>".$_SESSION['playername']);

    }

    else{

    $nick =($_SESSION['playername']);

    }
    session_register("nick"$nick); 

    ?>

    <frameset rows="60,*" cols="*" border=0> 

    <frame name="form" src="chat.php?action=form&nick=<?php echo $nick?>"> 


    <frame name="posts" src="chat.php?action=posts&nick=<?php echo $nick?>&chat=<?php echo $chat?>"> 


    </frameset>

    <noframes> 

    <body>

    <p>This page uses frames, but your browser doesn't support them.</p>

    </body> 

    </noframes> 

    </frameset>

    <?php 

    }


    function 
    GetInput() {

    global 
    $HTTP_SESSION_VARS

    global 
    $chat

    $chat stripslashes($chat);

    $chat addslashes($chat);

    global 
    $nick;

    ?> 

    <form onSubmit="return doSubmit" name="chatform" method="post" action="chat.php" target="posts"> 

    <input type="text" name="chat" size="70" > 

    <script language="JavaScript">

    document.chatform.chat.focus();

    </script>

    <input type="hidden" name="nick" value="<?php echo $nick?>"> 

    <input type="button" onclick="doSubmit()" name="Submit" value="Chat"> 

    <select name="col"> 

    <option>Black</option> 

    <option>Red</option> 

    <option>Green</option> 

    <option>Blue</option> 

    <option>Magenta</option> 

    </select> 


    <a href="javascript:sendFace(1)"><img src="/forum/YaBBImages/smiley.gif" border=0 name="DoFace1"></a>

    <a href="javascript:sendFace(2)"><img src="/forum/YaBBImages/sad.gif" border=0 name="DoFace2"></a>

    <a href="javascript:sendFace(3)"><img src="/forum/YaBBImages/grin.gif" border=0 name="DoFace3"></a>

    <input type="hidden" name="action" value="posts"> 

    </form>

    <script language="JavaScript">

    function sendFace(faceNum) 



    switch(faceNum) 



    case 1: 

    document.chatform.chat.value = ':)'; 

    break; 

    case 2: 

    document.chatform.chat.value = ':('; 

    break; 

    case 3: 

    document.chatform.chat.value = ':D'; 

    break; 



    document.chatform.submit(); 

    document.chatform.chat.value = ''; 

    document.chatform.chat.focus();

    }


    </script>

    <?php 

    }

    function 
    ShowAddposts(){

    global 
    $HTTP_SESSION_VARS

    global 
    $chat


    global 
    $nick;

    print 
    '<meta http-equiv="refresh" content="10;URL=chat.php?action=posts&nick=<?php echo $nick; ?>">';

    $svrConn mysql_connect("localhost""username""password") or die("<b>Error:</b> Couldnt connect to database");

    $dbConn mysql_select_db("database"$svrConn) or die ("<b>Error:</b> Couldnt connect to database");

    if(!empty(
    $chat)) { 

    $strQuery "insert into chatScript values(0, '$chat', '$nick')"

    mysql_query($strQuery); 

    }

    $strQuery "select theText, theNick from chatScript order by pk_Id desc limit 50"

    $chats mysql_query($strQuery);

    while(
    $chatline mysql_fetch_array($chats)) { 

    print 
    "<b>" $chatline["theNick"] . ": " swapFaces($chatline["theText"]) . "<br></b>"

    }

    }

    function 
    swapFaces($chatLine) {

    $chatLine str_replace(":)""<img src='/forum/YaBBImages/smiley.gif'>"$chatLine); 

    $chatLine str_replace(":(""<img src='/forum/YaBBImages/sad.gif'>"$chatLine); 

    $chatLine str_replace(":D""<img src='/forum/YaBBImages/grin.gif'>"$chatLine);

    return 
    $chatLine

    }

    if (
    $action == "posts"

    ShowAddPosts(); 

    elseif (
    $action == "form"

    GetInput(); 

    else

    Login();

    ?>
    for the most part this script comes from a tutorial at http://www.devarticles.com/c/a/MySQL/A-MySQL-Driven-Chat-Script/ with some slight modifications.
    Last edited by Nesper; 08-25-2004 at 09:02 PM.



Tags for this Thread