将发布数据设置为会话(PHP / jQuery)

时间:2014-04-03 15:35:55

标签: php jquery ajax

我正在尝试创建一个用户可以将其带到其他页面上并随意拖动的表单(如Twitter的提交表单)。所以一旦你点击'#34;写," div将以可拖动的形式出现。我希望它设置为当我在div上点击X时,它将提交到一个页面,将其设置为一个会话,所以当我点击"写"再次,表单将包含其中的所有信息。以下是我如何设置它的形式:

<div id="writeOverlay"><form action="process.php" id="writeForm">
   <input type="text" name="title" value="<?php if(isset($_SESSION['writeTitle"])){
      echo $_SESSION['writeTitle'];?>">
   <a href="#" id="writeExit">X</a>
   <button type="submit">Submit</button>
</form></div>

当你点击X时,jQuery会这样处理它:

$('#writeExit').click(function(){
   $('#writeOverlay').hide();
   event.preventDefault();
   $.post('site.com/writeHandle.php', $('#writeForm').serialize(),function(){ // the $('#writeForm') was originally just $(this) but I changed it to see if I'd get different results. I did  not.
      console.log('Closed.');
   });
});

writeHandle.php的工作原理如下

<?php
   session_start();
   $_SESSION['writeTitle'] = $_POST['title'];
?>

点击提交按钮可以正常工作,但是当我点击退出时,会话不会保存。所以当我点击&#34;写&#34;再次,表单将加载为空。我希望对这个问题保持清晰和简洁。如果需要更多信息,请发表评论,我们一定要更新。

1 个答案:

答案 0 :(得分:0)

出现了错误。您尝试在PHP中获取的title变量未正确发送。它应该发送类似的东西:

 ...,{title: "this is my title"}, function(){...}

因此,请编辑输入标记:

<input id="title" ...... >

然后你的邮政编码:

$.post(
   'site.com/writeHandle.php', 
   {
      title: $("#title").val()
   },
   function(){ // the $('#writeForm') was originally just $(this) but I changed it to see if I'd get different results. I did  not.
       console.log('Closed.');
   }
);