如何使用 AJAX 从 php 获取变量 [英] how to get variables from php using AJAX
本文介绍了如何使用 AJAX 从 php 获取变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想将一个值发送到一个 php 脚本并执行它,然后我想从 php 脚本中获取变量并使用 AJAX 显示它
这是 PHP 脚本
'.$req['名字']."</div>";}回声 $result;?>这里是 html 和 AJAX
<input type="text" name="username" id="username"/><input type="button" value="Find" id="ajaxButton"/><div id="结果"></div>
在 <div id="result"></div>
我想显示 $result
.我该怎么办,我对我的英语很抱歉...
解决方案 您需要使用 success
回调将结果发回以显示 $result
,如下所示:
I want to send a value to a php script and execute it, after i want to get variables from the php script and show it using AJAX
this is the PHP script
<?php
include('config.php');
$_POST['username'];
$username = $_POST['username'];
$sql = mysql_query('SELECT * FROM users WHERE username="'.$username.'" ');
if ($req = mysql_fetch_array($sql) ) {
$result = '<div class"result">' . $req['firstname']. "</div>";}
echo $result;
?>
and here is html and AJAX
<script>
$(document).ready(function() {
$("#ajaxButton").click(function() {
$.ajax({
url:'toAction.php',
data:{username: $('#username').val()},
type:'POST',
});
});
});
</script>
<input type="text" name="username" id="username"/>
<input type="button" value="Find" id="ajaxButton"/>
<div id="result"></div>
in <div id="result"></div>
I want to show $result
.
How can I do this, and I m very sorry about my English ...
解决方案 You will need to send the result back using the success
callback to display $result
like this:
<script>
$(document).ready(function() {
$("#ajaxButton").click(function() {
$.ajax({
url:'toAction.php',
data:{username: $('#username').val()},
type:'POST',
success: function(data) {
$('#result').html(data);
}
});
});
});
</script>
这篇关于如何使用 AJAX 从 php 获取变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文