我们可以在 session 变量中使用输入类型值而不提交任何表单
就像有一个输入字段 现在我想将上面的输入字段值保存在 $_session['anyname'] 中;当页面中没有表单时。
最佳答案
在 set.php 中:
<?php
session_start();
$_SESSION['value'] = $_POST['value'];
?>
在非表单 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>
这将记录该值(3秒延迟后)
在test.php中测试它:
<?php
session_start();
echo '<pre>';
echo htmlentities(var_export($_SESSION, true));
echo '</pre>';
?>
现在它在您的 session 中,没有表单提交
关于php - php中 session 变量中的输入字段值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2053523/