使用jQuery's .get()检索PHP数据
Using jQuery's .get() to retrieve PHP data
我使用jQuery的.ajax()发送到一个名为process.php的PHP文件。Process.php中有很多代码,但为了简单起见,我们就说它包含<?php echo 'hello'; ?>
。
这是适当的jQuery插入process.php的结果到div.results
?:
$.get('process.php', function(data) {
$('.results').html(data);
});
到目前为止,它似乎没有工作。
这是HTML/Javascript文件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<script type="text/javascript" src="http://code.jquery.com/jquery-1.5.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$("form#form").submit(function() {
var username = $('#username').attr('value');
$.ajax({
type: 'POST',
url: 'process.php',
data: 'username=' + username,
success: function() {
$('form#form').hide(function() {
$.get('process.php', function(data) {
$('.results').html(data);
});
});
}
});
return false;
});
});
</script>
</head>
<body id="body">
<form id="form" method="post">
<p>Your username: <input type="text" value="" name="username" id="username" /></p>
<input type="submit" id="submit" value="Submit" />
</form>
<div class="results"></div>
</body>
</html>
process.php
(大大简化):
<?php
/* get info from ajax post */
$username = htmlspecialchars(trim($_POST['username']));
echo $username;
?>
如果您只是想将结果字符串放回元素中,请使用load()
。
$('.results').load('process.php');
然而,看看你的代码…
$.ajax({
type: 'POST',
url: 'process.php',
data: 'username=' + username,
success: function() {
$('form#form').hide(function() {
$.get('process.php', function(data) {
$('.results').html(data);
});
});
}
});
…说明你误解了什么。分配给success
回调的正确匿名函数应该是…
function(data) {
$('form#form').hide()
$('.results').html(data);
}
你可以试试这样做。
function ajax_login() {
if ($("#username").val()) {
$.post("/process.php", { username : $("#username").val() }, function(data) {
if (data.length) {
$("#login_form").hide();
$("#login_result").html(data);
}
})
} else {
$("#login_result").hide();
}
然后在process.php中,如果post成功,则回显一些文本。
process.php =>
if (isset($_POST['username'])
{
echo 'hello '.$_POST['username'];
}
相关文章:
- 如何通过php页面将数据库值检索到jquery自动完成框中
- 通过ajax将坐标传递到php服务器端,并在处理后检索到javascript
- 如何从php返回的JSON中检索值
- PHP无法检索request.send
- Php无法检索动态生成的文本字段
- 在php中的同一网页上检索所选下拉项的值,并使用其他数据将其发布到其他表单
- 尝试显示使用 php 和数据库检索的缩略图中的全尺寸图像
- 检索php值并在javascript中使用它
- 使用JSON检索PHP数组,并使用javascript中的数组来填充播放列表
- 从ajax中检索php数组
- 在 JSON 中使用和检索 PHP 变量
- 如何在特定的时间跨度使用 AJAX 检索 php 表单
- 如何避免在调试时检索PHP脚本的文字内容
- 检索PHP变量到Bootstrap Modal
- 使用隐藏输入是在外部JavaScript文件中检索php变量的有效方法吗?
- 如何使用JavaScript检索PHP$_GET
- 使用javascript onload()和ajax来检索php数组
- 使用 JavaScript Ajax JSON 检索 PHP 数组时出错
- 使用jQuery's .get()检索PHP数据
- 通过AJAX检索PHP JSON数据-正确的数据请求结构