Md Ghouse Saqlain
Md Ghouse Saqlain

Reputation: 39

how to get parameters in php page with ajax and $.post

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());

Upvotes: 0

Views: 89

Answers (2)

user3386779
user3386779

Reputation: 7185

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>

Upvotes: 0

Sherali Turdiyev
Sherali Turdiyev

Reputation: 1743

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;

Upvotes: 1

Related Questions