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条)

报告相同问题?

悬赏问题

  • ¥15 linux驱动,linux应用,多线程
  • ¥20 我要一个分身加定位两个功能的安卓app
  • ¥15 基于FOC驱动器,如何实现卡丁车下坡无阻力的遛坡的效果
  • ¥15 IAR程序莫名变量多重定义
  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助