-2
<input type="text" user" id="nick" />
<input type="text" user" id="message" />
<a href="#">Send</a>

让我们保持简单。我有两个输入框和一个发送链接。我想将昵称和消息发送到shoutbox.php,我将在数据库中插入这些值,并希望从数据库中获取所有结果并将其显示在前端。

现在我已经实现了数据库部分的保存,但我无法将值从数据库取回到前端。

我迫切需要一个 jquery 函数,我可以在其中发送参数,它会为我完成所有工作。我希望你们自己也有这样的功能。

4

2 回答 2

1

使用 jQuery Ajax 方法将数据发送到shoutbox.php

$.ajax({
  type: "POST",
  url: "shoutbox.php",
  data: { nick: "val_of_nick", msg: "val_of_msg" },
  success: function(data) {
    alert('Loaded: ' + data);
  }
});

现在在你的shoutbox.php

//read the sended data
$nickname = $_POST['nick'];
$msg = $_POST['msg'];

//to send data back, just use echo/print
echo 'You sended nickname: ' . $nickname . ' and msg: "' . $msg . '"';

如果您运行此代码,那么您的 js 警报将显示echo来自shoutbox.php.

希望这可以帮助!

更多关于 jQuery ajax 的信息:info

于 2012-07-29T09:08:44.697 回答
0

只是一个例子:

HTML

<form id="myform">
  <input type="text" user" id="nick" name="nickname" /> <!-- use name ->
  <input type="text" user" id="message" name="message"/> <!-- use name -->
  <a href="#" id="send">Send</a>
</form>

jQuery

$('#send').on('click', function(e) {
   e.preventDefault(); // prevent page reload on clicking of anchor tag
   $.ajax({
     type: 'POST',
     url: 'url_to_script',
     data: $('#myform').serialize(),
     dataType: 'json', // if you want to return JSON from php
     success: function(response) {
      // you can catch the data send from server within response
    }
   });
});

现在在您的 PHP 端,您可以通过 ajax 捕获发送值,例如:

<?php
  ...
  $nickname = $_POST['nickname'];
  $message = $_POST['message'];
  ......
?>

相关参考:

于 2012-07-29T09:07:27.480 回答