dsf5989 2015-11-05 10:44
浏览 59

从html表单发送数据到PHP JSON方式

I'm trying to send POST data using JavaScript. I have data in HTML form :

<form name="messageact" action="">
    <input name="name" type="text" id="username" size="15" />
    <input name="massage" type="text" id="usermsg" size="63" />
    <input name="submitmsg" type="submit"  id="submitmsg" value="Send" />
</form>

after press submit key i want to send name and massage to my PHP file ! a wrote this for my JavaScript in html : post.php , with post method

$(document).ready(function(){
    //If user submits the form
    $("#submitmsg").click(function(){   
        var clientmsg = $("#usermsg").val();
        $.post("post.php", {text: clientmsg});              
        $("#usermsg").attr("value", "");
        return false;
    });

and for my php i wrote this :

if(isset($_SESSION['name'])){
    $text = $_POST['text'];
    ....
    ....

but in PHP i couldn't receive anything ! please help me to solve this problem ! and how can i change it to JSON way ?

  • 写回答

4条回答 默认 最新

  • douhe8981 2015-11-05 10:47
    关注

    Why do you need to store name in the $_SESSION? If you have used $_SESSION by mistake change this

    if(isset($_SESSION['name'])){
    $text = $_POST['text'];
    ....
    ....
    

    to this and it should work

    if(isset($_POST['text'])){
        $text = $_POST['text'];
        ....
        ....
    

    The $_POST and $_SESSION are 2 different variables. You might wanna take a look at this and this

    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看