1

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.

2
  • How are you supposed to have an input field without a form? Commented Jan 13, 2010 at 0:53
  • From the sounds of it, you are asking for AJAX. There are many great sources where you can (and should) read up on the subject. Here's an intro from w3c schools: w3schools.com/Ajax/ajax_intro.asp Commented Jan 13, 2010 at 3:55

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

8 Comments

He specifically requested there to be no form submission required.
there is no form submit, but you need a <form> element if you want to catch events, even if you're not submitting
how to do that?? please suggest me
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
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)
|
0

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;

2 Comments

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
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
0

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.

1 Comment

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'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.