javascript - 如何使用ajax和$.post获取php页面中的参数

标签 javascript php jquery ajax .post

实际上我是 web 和 php 新手, 我正在建立一个网站, 我想在那里引入聊天 session ,但是,当用户单击插入按钮时,该按钮不起作用(无法将参数传递到另一个页面)

救救我......!!!

这是代码

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=""; 
}

插入.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());

最佳答案

jscript.js 中的 un 是什么。也许,该参数是未知的。

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

您应该声明 un 和 msg 变量;

var un, msg;

关于javascript - 如何使用ajax和$.post获取php页面中的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32284450/

相关文章:

php - 与货币和国家相关的产品的数据库结构

javascript - JQuery 使用 mouseX 百分比隐藏/显示

javascript - 使用 div 的值传递数据(安全)

javascript - Underscorejs 对象到集合

javascript - 识别 bootstrap touch spin 中的点击事件

php - 我使用 GET 方法而不是 POST 来传递变量。如何解决这个问题?

jQuery 将升序数字添加到 ID

javascript - Bootstrap 不工作,但包含 js 和 css

javascript - 什么时候真正使用 ChannelUrl?

php - 从 Javascript 同一文件调用 php 函数