jQuery的ajax发布 [英] ajax post with jQuery

查看:106
本文介绍了jQuery的ajax发布的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

单击元素时,是否可以使用POST方法提交ID?我认为jQuery具有内置的AJAX功能...

Is it possible to submit with POST method an ID when an element is clicked? I think jQuery has a built in AJAX fanction...

<span id="submitThisID"></span>

推荐答案

$('#submitThisId').click(function() {
  $.post("postTo.php", { id: $(this).attr('id') }, function(response) {
    alert(response);
  });
});

这会将您的ID发布到postTo.php,并且可以使用以下方法进行检索:$id = $_POST['id'];

This will post your id to postTo.php and can be retrieved using: $id = $_POST['id'];

postTo.php文件返回的所有内容都会显示在屏幕上(响应).

Whatever the postTo.php file returns will be alerted to the screen (response).

编辑:要提交到同一页面并执行一些服务器端操作(数据库查询等),您只需在页面顶部放置类似于以下代码的内容:

To submit to the same page and do some server-side actions (db query, etc) you would only need to put something similar to the following code at the top of your page:

<?php 
  if (isset($_POST['id'])) {
    $id = $_POST['id'];
    // Perform some db queries, etc here
    $response = 'Format a response here'; // Format a response

    // This will return your formatted response to the $.post() call in jQuery 
    return print_r($response);
  } 
?>

当响应"(文本,html等)返回给jQuery时,您可以使用简单的jQuery在必要时将其插入到页面中,并更改页面的外观.

When that "response" (text, html, etc) is returned to jQuery, you can use simple jQuery to insert it into the page where necessary and change the look of the page.

这篇关于jQuery的ajax发布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆