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']; when there is no form in page.
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']; when there is no form in page.
in set.php:
<?php
session_start();
$_SESSION['value'] = $_POST['value'];
?>
in the non-form form 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)
test it in 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.
That PHP would probably look something like this:
session_start();
foreach ($_GET as $key => $value)
$_SESSION[$key] = $value;
Using jQuery and PHP
The HTML:
<input type="text" id="autosend" />
The JavaScript
$(function(){
$('#autosend').blur(function(){
$.post('receiver.php',{fieldval:$(this).val()},function(response){
alert(response);
});
})
})
The PHP
$_SESSION["somename"] = $_POST["fieldval"];
echo $_SESSION["somename"];
Now, whenever input field looses focus, the session variable will be updated with the current value.