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

4

3 回答 3

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());
    });
});
于 2013-08-01T19:56:49.403 回答
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 );
});
于 2013-08-01T19:58:21.053 回答
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>
于 2013-08-01T20:02:17.050 回答