如何使用ajax传递按钮的变量参数名称? [英] How can I pass a variable parameter name of a button using ajax?

查看:349
本文介绍了如何使用ajax传递按钮的变量参数名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 AJAX POST 方法将一个按钮的名称参数传递给PHP文件。



我在使用Ajax提交表单后发现错误,表明索引未在页面2上定义。



我尝试过的内容:



HTML表格:



< form class =upVote action =method =post> 
< button id =plusname =plusvalue =foo>按钮< / button>
< / form>';





AJAX:



 $(document).ready(function(){
$(。upVote)。submit(function(){
$ .ajax({
url:../inc / handler / up-vote.php,
data:$(。upVote)。serialize(),
type:POST,
dataType:'json',
success:function(e){
console.log(JSON.stringify(e));
},
error:function(e){
console.log(JSON.stringify(e));
console.log('error');
}
});
返回false;
});
});







PHP POST:



 $ _ POST ['plus']; 

解决方案

(文件)。就绪(函数(){

( 给予好评)。提交(函数(){

.ajax的({
url:../inc /handlers / up-vote.php,
数据:


I want to pass a name parameter of a button using AJAX POST method to a PHP file.

I am getting an error after submitting the form with Ajax that the index is undefined on the page 2.

What I have tried:

The HTML form:

<form class="upVote" action=""  method="post">
<button id="plus" name="plus" value="foo">button</button>
</form>';



The AJAX:

$(document).ready(function(){
  $(".upVote").submit(function(){
      $.ajax({
          url: "../inc/handlers/up-vote.php",
          data: $(".upVote").serialize(),
          type: "POST",
          dataType: 'json',
          success: function (e) {
              console.log(JSON.stringify(e));
          },
          error:function(e){
              console.log(JSON.stringify(e));
              console.log('error');
          }
      });
      return false;
  });
});




The PHP POST:

$_POST['plus'];

解决方案

(document).ready(function(){


(".upVote").submit(function(){


.ajax({ url: "../inc/handlers/up-vote.php", data:


这篇关于如何使用ajax传递按钮的变量参数名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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