dream_high1026 2015-02-06 04:54
浏览 48
已采纳

JQuery / AJAX脚本不发送数据到php文件?

I am making a question/status posting system using JQuery and Ajax however an error is being displayed "Notice: Undefined index: status in C:\xampp\htdocs\deadline\data.php on line 5" which is my line of code "$var = $_POST['status']; "

My system works as you have to log in to post a question(using sessions) BUT the issue is with my JQuery/AJAX script as the data that I have on homepage.php is not being sent to data.php (from my understanding which is why my index 'status' is undefined).

MY CODE

    <?php
    include("connection.php");

    session_start();

    if(isset($_SESSION['user_id']) && $_SESSION['user_id'] != "") {
            
        }

          else {

            header("location:login.php");
        }

    $sid = $_SESSION['user_id'];

    $conn = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);

    $sql = "SELECT * FROM `users` WHERE id='{$sid}'";

     $query = $conn->query($sql);

    $result = $query->fetch_assoc();

    $fname = $result['fname'];

    $lname = $result['lname'];

    $time = time();

    ?>

    <html>
    <head>

    <script type="text/javascript" src="jquery.js"></script>

    <script type="text/javascript">

    //daddy code
    $ (document).ready(function() {

    //mama code
    $("button#postbutton").click(function() {
    //children code
    var status = $("textarea#status").value();

    if(status == "") {
        return false;
    }

    var data = 'status='+status;

    $.ajax({

    type: "POST",

    url: "data.php",

    data: data,

    success: function(data) {

    $("#statustext").html(data);

    }

    });


    });

    });

    </script>
    </head>

    <body>


    <div id="global">

    <form action="" onsubmit="return false">

    <textarea id="status"></textarea>


    <button id="postbutton">POST</button>

    <a href="logout.php">LOGOUT</a>

    </form>

    <br/>
    <br/>


    <div id="allstatus">



    <!-- SKELETON -->


    <div id="status">


    <div id="statuspic">
    </div>


    <div id="statusinfo">

    <div id="statusname">JOnathan</div>
    <div id="statustext"> this is the question</div>
    <div id="statusoption"><button id="likestatus">LIKE</button></div>
    <div id="statusoption"><button id="commentstatus">COMMENT</button></div>
    <div id="statusoption"><button id="sharestatus">SHARE</button></div>
    <div id="statusoption"><button id="statustime">TIME</button></div>

    </div>

    </div>    

    <!-- SKELETON -->

    </div>

    </div>

    </body>

    </html>

    <?php

    $var = $_POST['status'];

    const DB_HOST = 'localhost';
    const DB_USER = 'root';
    const DB_PASS = '';
    const DB_NAME = 'forum';
    //connecting 

    $conn = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);

    if($conn->connect_error) {
        die("Connection Failed: " . $conn->connect_error);
    } else {

    echo " success";

    }


    $sql = "INSERT INTO `question`(id, question) VALUES ('', '{$var}')";

    $result = $conn->query($sql);

    if($result) {

        echo "inserted successfuly";
    }
     else {

        echo "failed: " . $conn->error;
     }


    echo " the text: " .$var. " has been added to database.";


    ?>


HOW IT SHOULD WORK

I want to use JQuery/AJAX on my homepage.php to send data to data.php and that data gets returned back to homepage.php. On success to be displayed in my div #statustext.

Meaning when i write a question in textarea input field the data gets stored in database and retrieved and displayed in my div on the browser. Please help me thanks..

</div>
  • 写回答

4条回答 默认 最新

  • dprlv04662 2015-02-06 05:10
    关注

    you should change your script like below. Ajax sends data to url in a serialize manner either you have to use form serialize which will send all the fileds info to your php url other wise if you have to send only one field value then you can do this change

    <script type="text/javascript">
        //daddy code
        $ (document).ready(function() {
            //mama code
            $("button#postbutton").click(function() {
                //children code
                var status = $("textarea#status").value();
                if(status == "") {
                    return false;
                }
                var data = {'status='+status};
                $.ajax({
                    type: "POST",
                    url: "data.php",
                    data: data,
                    success: function(data) {
                        $("#statustext").html(data);
                    }
                });
            });
        });
    </script>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料