单击并使用jquery提交按钮ID

单击并使用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

我的目标是使用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 
} 
}); 
返回false; 
}); 
  code>  pre> 
 
 

subpage.php p> $ item = $ _POST ['b']; $ query = mysql_query(“SELECT * FROM table WHERE id = '25'AND bttnid ='$ item'”); if(count($ 查询)== 0){ mysql_query(“INSERT INTO table VALUES('','25','$ item')”); } code> pre> div>

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.

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

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).

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/