douzhao9608
douzhao9608
2012-06-01 16:35

单击并使用jquery提交按钮ID

已采纳

my aim is to submit a button id using jquery into a database im only interested in the button not its value.

$("#button").click(function() {
    var p = $(this).attr("id");

    $.ajax({
        type: "POST",
        url: "subpage.php",
        data: 'b=' + p,
        cache: false,
        success: function(html) {
            //process live
        }
    });
    return false;
});

subpage.php

$item = $_POST['b'];
$query = mysql_query("SELECT * FROM table WHERE  id = '25' AND bttnid = '$item'");
if(count($query)==0){
    mysql_query("INSERT INTO table VALUES ('','25','$item') ");
}

it doesn't submit. please assit

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

4条回答

  • douzongluo7542 douzongluo7542 9年前

    the problem is not in jquery, the problem lies in the php statement.. changed

    if(count($query)==0){/../} 
    

    to

    if(mysql_num_rows($query)==0){/../} and it works.
    
    点赞 评论 复制链接分享
  • dsfvsdfv23599 dsfvsdfv23599 9年前

    Just 1 suggestion here: you definitely should take a look at jQuery API, which might give you answers you are looking for :)

    For example: (example taken from .ajax() API page)

    var menuId = $("ul.nav").first().attr("id");
    var request = $.ajax({
      url: "script.php",
      type: "POST",
      data: {id : menuId},
      dataType: "html"
    });
    
    request.done(function(msg) {
      $("#log").html( msg );
    });
    
    request.fail(function(jqXHR, textStatus) {
      alert( "Request failed: " + textStatus );
    });
    

    You have already got the template here and all you are left to do is just modify original values into those of your web app.

    Link: http://api.jquery.com/jQuery.ajax/

    点赞 评论 复制链接分享
  • dongliyan9190 dongliyan9190 9年前

    count($query) is wrong. You need to use mysql_num_rows($query) to get the number of rows returned.

    Besides that, use {b: p} for data instead of a string so jQuery properly encodes p (even though it's most likely not necessary in this particular case since IDs are not supposed to contain any special characters anyway).

    点赞 评论 复制链接分享
  • douyinjiao9351 douyinjiao9351 9年前

    Fix quotes and Do it like,

    $.ajax({
      type: "POST",
      url: "subpage.php",
      data: {b: p},
      cache: false,
      success: function(html)
      {
        //process live
    
      }
     });
    

    Let jQuery handle the urlencoding of data object

    点赞 评论 复制链接分享

相关推荐