0

How can i use AJAX to do something like:

<input type="text" name="test">

<?php
$test = $_POST['test']; //need to set the var "on the fly" 
echo $test;
?>

I need that the php var $test is auto-updated with the text/numbers that users set on the input named "test"

LIVE EXAMPLE OF USE: test

vonatar
  • 64
  • 1
  • 7
  • can you post what you have tried so far? – stackErr Aug 01 '13 at 19:55
  • Please elaborate, you question is not clear. Can you show what you've tried and what is it that you expect and what is not working? – vee Aug 01 '13 at 19:55
  • What javascript/jquery have you tried? – Jon Rubins Aug 01 '13 at 19:56
  • What's the reason you need to update a server-side variable on the fly? This would involve making an AJAX request on each keypress, which will get very tedious. There's most likely a better way to approach this situation. – tymeJV Aug 01 '13 at 19:57
  • What's with the rash of "i need to set a PHP variable from JS" questions as of late? – cHao Aug 01 '13 at 20:01
  • possible duplicate of [pass JS variable to php echo statement](http://stackoverflow.com/questions/17982295/pass-js-variable-to-php-echo-statement), and dozens of others. ( http://stackoverflow.com/q/17978326, http://stackoverflow.com/q/17932184, ...) – cHao Aug 01 '13 at 20:05
  • none of my case... Live example here: www.codetuts.altervista.org/test.php Users can set the width and height of a print. The max width of each piece is 60cm, so if user on width set 300cm i wanna to display "on the fly" the calc. See live example posted in the question – vonatar Aug 01 '13 at 20:29
  • They all have exactly the same answer as the question you have asked -- and the first is exactly *the very question* you have asked (minus the difference between echoing the variable itself and echoing it as part of another string). If you meant to ask a different question, then go right ahead. But this one has already been answered many times. – cHao Aug 01 '13 at 20:53

3 Answers3

2

Ideally you need to add more information to your question, but I'd start off by looking into jQuery Ajax.

$.ajax({
  type: "POST",
  url: "some.php",
  data: { name: "John", location: "Boston" }
}).done(function( msg ) {
  alert( "Data Saved: " + msg );
});

As per your linked example is this what you mean? JSFIDDLE.

<input type="text" name="test" id="test" value="" />
<input type="submit" id="submit" />

Base: <span id="base"></span>

$( document ).ready(function() {
    $('#submit').click(function(){
        $('#base').html($('#test').val());
    });
});
Adam
  • 1,957
  • 3
  • 27
  • 56
  • hmm, i'm reading the link you told, but i need to finish it before vacation (-2days)..can please make a working fast example? The result is like that: (http://www.codetuts.altervista.org/test.php) but instead of pressing ENTER button, need to calculate it with no page refresh – vonatar Aug 01 '13 at 21:03
  • Theres no ajax in the example you posted? I'll make something like what I think you want and update my answer – Adam Aug 01 '13 at 21:07
  • Corrected with what you want – Adam Aug 01 '13 at 21:41
  • Thanks, but js fiddle don't work. BTW i'll work on this, thanks! – vonatar Aug 02 '13 at 07:17
0

You could use AJAX method from Jquery library

$.ajax({
 type: "POST",
 url: "some.php",
 data: { test: "testData"}
 }).done(function( msg ) {
 alert( "Data Saved: " + msg );
});
Emilio Borraz
  • 516
  • 3
  • 17
0

I wouldn't use jQuery.. Just add an onkeypressed listener, this will update you data:

<input onkeypressed="updatestuff();" type="text">
<script type="text/javascript">
     function updatestuff(){
         //ajax here
     }
</script>
BenMorel
  • 34,448
  • 50
  • 182
  • 322
T_01
  • 1,256
  • 3
  • 16
  • 35