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

    评论

报告相同问题?