简体   繁体   English

在php中的会话变量中输入字段值

[英]input field value in session variable in php

Can we use input type value in session variable without any form submitting 我们可以在不提交任何表单的情况下在会话变量中使用输入类型值吗

like there is an input field now i want to save above input field value in a $_session['anyname']; 就像现在有一个输入字段,我想将输入字段的值保存在$ _session ['anyname'];中; when there is no form in page. 页面中没有表单时。

in set.php: 在set.php中:

<?php

session_start();
$_SESSION['value'] = $_POST['value'];

?>

in the non-form form page.php: 非格式的 page.php中:

<?php session_start() ?>
<html>
<head>
<script type="text/javascript"
  src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
<script type="text/javascript">
function setVal()
{
  var val = jQuery('#value').val()
  alert('Setting the value to "' + val + '"')
  jQuery.post('set.php', {value: val})
  alert('Finished setting the value')
}
jQuery(document).ready(function() {
  setTimeout('setVal()', 3000)
})
</head>
<body>

<form>
<input id="value" type="text" name="value" value="value">
<input id="value2" type="text" name="value2" value="value2">
</form>

</body>
</html>

this will record the value (after a 3 second delay) 这将记录该值(延迟3秒后)

test it in test.php: 在test.php中对其进行测试:

<?php

session_start();
echo '<pre>';
echo htmlentities(var_export($_SESSION, true));
echo '</pre>';

?>

now it's in your session with no form submit 现在在您的会话中,无需提交表单

I suppose your best bet is to set up a separate PHP script that is called upon via Javascript with the input field information in the query string ( ajax.php?key=value ) and stores that information to a $_SESSION variable. 我想您最好的选择是设置一个单独的PHP脚本,该脚本通过Javascript调用,并在查询字符串( ajax.php?key=value )中输入字段信息,并将该信息存储到$_SESSION变量中。

That PHP would probably look something like this: 该PHP可能看起来像这样:

session_start();
foreach ($_GET as $key => $value)
   $_SESSION[$key] = $value;

Using jQuery and PHP 使用jQuery和PHP

The HTML: HTML:

<input type="text" id="autosend" />

The JavaScript JavaScript

$(function(){
   $('#autosend').blur(function(){
       $.post('receiver.php',{fieldval:$(this).val()},function(response){
          alert(response);
       });
   })
})

The PHP 的PHP

$_SESSION["somename"] = $_POST["fieldval"];
echo $_SESSION["somename"];

Now, whenever input field looses focus, the session variable will be updated with the current value. 现在,只要输入字段失去焦点,会话变量将使用当前值进行更新。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM