javascript 使用 jQuery 的 .get() 检索 PHP 数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5739782/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Using jQuery's .get() to retrieve PHP data
提问by altrier
I'm using jQuery's .ajax() to post to a PHP file called process.php. Process.php has a lot of code in it, but for simplicity's sake, let's just say it contains <?php echo 'hello'; ?>
.
我正在使用 jQuery 的 .ajax() 发布到名为 process.php 的 PHP 文件。Process.php 中有很多代码,但为了简单起见,我们只说它包含<?php echo 'hello'; ?>
.
Is this the proper jQuery to insert process.php's results into div.results
? :
这是将 process.php 的结果插入到的正确 jQuerydiv.results
吗?:
$.get('process.php', function(data) {
$('.results').html(data);
});
So far it doesn't seem to be working.
到目前为止它似乎没有工作。
Here's the HTML/Javascript file
这是 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>
Here's process.php
(greatly simplified):
这是process.php
(大大简化):
<?php
/* get info from ajax post */
$username = htmlspecialchars(trim($_POST['username']));
echo $username;
?>
回答by alex
If you simply want to place the resulting string back into an element, use load()
.
如果您只是想将结果字符串放回元素中,请使用load()
.
$('.results').load('process.php');
However, looking at your code...
但是,查看您的代码...
$.ajax({
type: 'POST',
url: 'process.php',
data: 'username=' + username,
success: function() {
$('form#form').hide(function() {
$.get('process.php', function(data) {
$('.results').html(data);
});
});
}
});
...shows you have misunderstood something. The correct anonymous function to assign to the success
callback would be...
...表明你误解了一些东西。分配给success
回调的正确匿名函数是......
function(data) {
$('form#form').hide()
$('.results').html(data);
}
回答by rm-
You could try something like this.
你可以尝试这样的事情。
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();
}
Then in process.php just echo out some text if the post sucesses.
然后在 process.php 中,如果帖子成功,只需回显一些文本。
process.php =>
进程.php =>
if (isset($_POST['username'])
{
echo 'hello '.$_POST['username'];
}