0

actually i'm new to web and php, i'm building a website, i would like to introduce chat session there but, when the user click the insert button that's not working (failing to pass the paramerts to another page)

Help me out...!!!

here is the code

chat_page.php

<input type="text" id="txtmsg" name="txtmsg" size="25"> <input type="button" value="send" onclick="sndmsg()">

jscript.js

function sndmsg() {
    msg=document.getElementById('txtmsg').value;
    $.post("insert.php",{u:un,m:msg},function(){ });
    document.getElementById('txtmsg').value=""; 
}

insert.php

include("connec.php");
$chatmsg=mysql_real_escape_string($_REQUEST['m']);
$uname=mysql_real_escape_string($_REQUEST['u']);
echo $chatmsg;
echo $uname;
mysql_query("INSERT INTO `mdi_chat_msg`(`uname`, `chatmsg` ) VALUES ( '$uname','$chatmsg')") or die(mysql_error());
rrk
  • 15,677
  • 4
  • 29
  • 45

2 Answers2

1

What is un in jscript.js. Maybe, that parameter is unknown.

$.post("insert.php",{u:un,m:msg},function(){ });

You should declare un and msg variables;

var un, msg;
Sherali Turdiyev
  • 1,745
  • 16
  • 29
0

Check you have declare and change the type="submit" instead type="button" you cant use the word msg as variable and cant pass the js value in ajax and check un variable.

<form>
   <input type="text" id="txtmsg" name="txtmsg" size="25"> <input type="submit" value="send" onclick="sndmsg()">
</form>


<script>
    function sndmsg() {
        msg1=$("#txtmsg").val();
        $.post("insert.php",{u:un,m:msg},function(){ });
        document.getElementById('txtmsg').value=""; 
    }
    $.ajax({
        type: "POST",
        url: "insert.php",
        data: { u: un,m:msg1},
    });
</script>
Abdallah Alsamman
  • 1,623
  • 14
  • 22
user3386779
  • 6,883
  • 20
  • 66
  • 134