我如何使用AJAX执行以下操作:
<input type="text" name="test">
<?php
$test = $_POST['test']; //need to set the var "on the fly"
echo $test;
?>
我需要使用用户在名为“test”的输入上设置的文本/数字自动更新php var $ test
使用实例:test
答案 0 :(得分:2)
理想情况下,您需要在问题中添加更多信息,但我首先考察jQuery Ajax.
$.ajax({
type: "POST",
url: "some.php",
data: { name: "John", location: "Boston" }
}).done(function( msg ) {
alert( "Data Saved: " + msg );
});
根据您的链接示例,这是什么意思? 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());
});
});
答案 1 :(得分:0)
您可以使用Jquery库中的AJAX方法
$.ajax({
type: "POST",
url: "some.php",
data: { test: "testData"}
}).done(function( msg ) {
alert( "Data Saved: " + msg );
});
答案 2 :(得分:0)
我不会使用jQuery .. 只需添加一个onkeypressed监听器,这将更新您的数据:
<input onkeypressed="updatestuff();" type="text">
<script type="text/javascript">
function updatestuff(){
//ajax here
}
</script>