簡體   English   中英

將發布數據設置為會話(PHP / jQuery)

[英]Set post data as a session (PHP/jQuery)

我正在嘗試創建一種表單,用戶可以將其帶到其他頁面上並拖動(例如Twitter的提交表單)。 因此,一旦您點擊“寫入”,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'];
?>

點擊提交按鈕可以正常工作,但是當我按下退出按鈕時,會話不會保存。 因此,當我再次單擊“寫入”時,表單將加載為空。 我希望在這個問題上清楚明了。 如果需要更多信息,請發表評論,我將確保進行更新。

發生錯誤。 您試圖在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.');
   }
);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM