views:

1264

answers:

3

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.

A: 

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"&gt;&lt;/script&gt;
<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

jspcal
He specifically requested there to be no form submission required.
Dominic Barnes
No dear, There is no form at all.... is there any way to get value without form
diEcho
there is no form submit, but you need a <form> element if you want to catch events, even if you're not submitting
jspcal
how to do that?? please suggest me
diEcho
now how to show that session variable value in set.php page or how do i know that the value is really coming or not
diEcho
not changing any value from that input box, bcoz value in input box also coming from another javascript, and i m doing all these stuff on backend, i means not any user interference( it shoud done automaticaaly)
diEcho
which event? i want that it calculate automatically and show me the data of that input field
diEcho
you can call the `setVal()` function to store the value, in an event or when the doc loads.
jspcal
Thank you jspcal........................U saved my lifeThanx a ton..n keep in touch.....u r really great teacher
diEcho
A: 

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;
Dominic Barnes
how to call javascript on a input box? i m doing all thing between the login page and dashboard page( just after login) i mean where i doing all database stuffs
diEcho
let me know actaully what i m doing? i m using a google map to find distance from given input field from all distance that is in my database, and if distance is desired then who onlt those places name on dashboard , so i have to do all this stuff in beetween login and main page , do u have any other solution?? please suggest me
diEcho
A: 

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.

Kristoffer S Hansen
Dear i dont want to blue or any event on that input field on that page( eg x.php), bcoz value from that input field is coming through another javascript and that value i have to use on next page , bcoz the page(x.php) where that value is coming will not showm anywhere.actauuly i m signing in on a.php and after signing in i goes to b.php , but meanwhile a.php and b.php there is 'x.php' where i m doing database stuffs and i need that input field value on that page (x.php) want to use at 'b.php'
diEcho