【问题标题】:Send POST value using Ajax from a button in HTML/PHP使用 Ajax 从 HTML/PHP 中的按钮发送 POST 值
【发布时间】:2013-08-31 21:40:06
【问题描述】:

** 我找到了一种可行的方法 - 将其粘贴在所有这些文本下方**

我想使用 Ajax 发送一个隐藏值,其中包含一个来自 PHP 的变量,使用一个按钮。

如果我定义它,我可以发送值没问题:

    <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
    <script>
    $(document).ready(function(){
    $("button").click(function(){
    $.post("results.php",
    {
    id:"1000"
    },
    function(data,status){
    alert("Data: " + data + "\nStatus: " + status);
    });
    });
    });
    </script>

然后我可以处理页面results.php 上的$_POST['id'] 变量。

但是如何指示 Ajax 在 HTML 表单按钮中发送隐藏值?我的 HTML 包含在 PHP print_r 指令中,如下所示:

    print("<form id='form' method='post' action=''>
    <input type='hidden' name='id' value=$strTweet2>
    <input type='submit' value='RT'></form>");

感谢溢出者的任何帮助。我一直在阅读其他人的问题,并学会了如果我定义它就发送一个值,我只是不知道如何从一个按钮发送值。

** 所以我有这个工作,但只在 HTML 中工作,当我在 foreach 中应用它时,它只会给我数组中的第一个值,但我想我会问另一个问题。此脚本将从表单按钮发送单个隐藏值 **

    <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
    <script>
    function submitForm() {
    $.ajax({type:'POST', url: 'results.php', data:$('#ContactForm').serialize()});
    return false;
    }
    </script>

HTML

    <form id='ContactForm' onsubmit='return submitForm();'>
    <input type='hidden' name='id' value='1000'>
    <input type='submit' value='Post it!'>

【问题讨论】:

  • 对于初学者,不要使用 print_r。使用 echo,并将 HTML 放在“双引号”中
  • id 是表单中唯一的字段吗?
  • 是的,安德鲁,这只是我想放入按钮隐藏字段的一个值。我看到人们在有多个字段时使用 serialise,但我不知道如何修改它以让我只发送一组数据。

标签: php ajax forms post button


【解决方案1】:

首先,不要使用 print_r() 因为这个函数是用来调试的。请改用 print()。

在 jQuery 中,您可以通过字段名称访问隐藏字段值,如下所示:

$('input[name=id]').val()

但你可以给它 id 并这样做:

$('#id').val()

... 并将您的字符串放在双引号中:

print("<form id='form' method='post' action=''>
<input type='hidden' name='id' value=$strTweet2>
<input type='submit' value='RT'></form>").

编辑:工作示例如何按名称选择隐藏字段值并使用 jQuery .post() 发布它

<?php

if (!empty($_POST['id'])){
    print(json_encode(array('id' => $_POST['id'])));
    exit();
}

?>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript">

$(document).ready(function(){ 
    $('input[type=submit]').click(function(){ 
        $.post("", { id:$('input[name=id]').val() }, function(data,status){ 
            alert("Data: " + data + "\nStatus: " + status); 
        });
        return false;
    }); 
});

</script>

<form id="form" method="post" action="">
    <input type="hidden" name="id" value="hidden_value">
    <input type="submit" value="RT">
</form>

只需将其复制到 .php 文件中,然后将您的浏览器指向那里 :)

【讨论】:

  • 感谢 Rauli,我已更改为 print(),就像您和 Jeffman 提到的那样。所以我在想我必须使用 Ajax 的原因是将变量发布到另一个页面,但是使用 .val 如何将内容发布到该页面?我正在阅读api.jquery.com/val,但这对于将数据发布到我的 results.php 页面没有意义。
  • 将 id:"1000" 替换为 id:$('input[name=id]').val() ,因此它使用输入字段值(而不是静态字符串“1000”)。
  • 再次感谢劳利。我试过了,根本没有从 results.php 页面得到任何响应。代码现在看起来像: $(document).ready(function(){ $("button").click(function(){ $.post("results.php", { id:$('input[name=id) ]').val() }, function(data,status){ alert("Data: " + data + "\nStatus: " + status); }); }); }); HTML 与您的示例完全相同。
  • 抱歉,我昨天没有注意到,但是您的 .click() 选择器没有选择任何内容,因为页面上没有
猜你喜欢
  • 2021-02-22
  • 2015-06-17
  • 2021-06-13
  • 1970-01-01
  • 2019-07-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多