简体   繁体   English

单击并使用jQuery提交按钮ID

[英]click and submit button id with jquery

my aim is to submit a button id using jquery into a database im only interested in the button not its value. 我的目的是使用jquery向数据库中提交仅对按钮感兴趣而不是其值的按钮ID。

$("#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 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 请配合

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 让jQuery处理数据对象的urlencoding

count($query) is wrong. count($query)是错误的。 You need to use mysql_num_rows($query) to get the number of rows returned. 您需要使用mysql_num_rows($query)来获取返回的行数。

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). 除此之外,对data使用{b: p}而不是字符串,以便jQuery正确编码p (尽管在这种特殊情况下很有可能没有必要,因为ID始终不应该包含任何特殊字符)。

Just 1 suggestion here: you definitely should take a look at jQuery API, which might give you answers you are looking for :) 这里只有1条建议:您绝对应该看看jQuery API,它可能会为您提供所需的答案:)

For example: (example taken from .ajax() API page) 例如:(示例取自.ajax()API页面)

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. 您已经在此处有了模板,剩下要做的就是将原始值修改为Web应用程序的值。

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

the problem is not in jquery, the problem lies in the php statement.. changed 问题不在jquery中,问题出在php语句中。

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

to

if(mysql_num_rows($query)==0){/../} and it works.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM