dsbezji539113152 2015-02-07 15:09
浏览 52

jQuery / AJAX和SQL查询在刷新浏览器时不会永久地在浏览器上显示html表单输入?

I want to display all my textarea inputs in my browser (because if I refresh my page all data is gone from what i have sent through my html form using jquery/ajax ). So currently using jquery/ajax information from homepage.php is being sent to data.php through my #textarea input and returned on success to be displayed is my #status div. Every time i hit the POST button the data is added to the browser but when I refresh the page data is all lost ( even though the information is stored in my sql table). I understand i need to use a while() loop and I have tried but none of my code works in respect to what I am trying to achieve.

Homepage.php

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

session_start();


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

        $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();


    }


      else {

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

?>

<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() {

 var data = $("#formpost").serialize();
 var posterid = <?php echo $sid; ?>;

$.ajax({

type: "POST",

url: "data.php",

data: data,

success: function(data) {

$("#statustext").append(data);
$("textarea#text1").val('');
}

});



});



});



</script>
</head>

<body>


<div id="global">

<form id="formpost" action="" method="post" onsubmit="return false">

<textarea id="text1" name="status" value="" ></textarea>


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

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

</form>

<br/>
<br/>


<div id="allstatus">

<!-- SKELETON -->


<div id="wholestatus">


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


<div id="statusinfo">

<div id="statusname"><?php echo " Welcome to VLE, {$fname} {$lname} ";?></div>
<div id="statustext"> </div>


</div>


</div>  


<!-- SKELETON -->


</div>


</div>


</body>


</html>

data.php

<?php



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 {

 }


if(isset($_POST['status']))
{
$var = $_POST['status'];

$sql = "INSERT INTO `question`(id, question) VALUES ('', '{$var}')";
$result = $conn->query($sql);
if($result) {  }

    else {

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


echo  "<br>{$var}";



}





 ?>
  • 写回答

2条回答 默认 最新

  • dongqiang2024 2015-02-07 15:26
    关注

    Your select is calling for a specific Session id but your insert is not storing anything inside id column.

    评论

报告相同问题?

悬赏问题

  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b