【问题标题】:Pass data to post.php in the background form fields在后台表单字段中将数据传递给 post.php
【发布时间】:2023-03-11 10:00:03
【问题描述】:

我有这个脚本,可以很好地通过帖子从表单发送消息。但是我想从表单中添加更多信息以通过脚本发送但无法正常工作?!我必须有一个 $.post("post.php" .. 用于每个输入吗?谢谢一百万。

    --- THIS ONE IS SENT FINE TO post.php --
<input name="usermsg" type="text" id="usermsg" />

    --- WOULD LIKE TO ADD THESE TO THE SCRIPT --
<input type="hidden" name="request" value="1" />
<input type="hidden" name="sentby" value="2" />
<input type="hidden" name="sentto" value="3" />

$(document).ready(function () {
        $("#submitmsg").click(function () {
        var clientmsg = $("#usermsg").val();
        $.post("post.php", { text: clientmsg });
        $("#usermsg").val("");
        return false;
       });
});

【问题讨论】:

  • 您想通过邮寄方式发送更多数据吗?您可以通过在 json 中包含该数据来发送更多数据,例如: $.post("post.php", { text: clientmsg, sendby: $("input[name=sentby]").val(), ... . });
  • 好的,谢谢。所以我必须有 $.post("post.php", { text: clientmsg, sentby: $("input[name=sentby]").val(), .... });每个输入.. 然后最后只返回一个 false?
  • 不,它是一个json文件,没有返回它
  • 好的,谢谢..所以所有这些都是应该为每个输入重复的 "var clientmsg = $("#usermsg").val(); $.post("post.php", { text: clientmsg }); $("#usermsg").val("");"

标签: javascript php html jquery forms


【解决方案1】:
$(document).ready(function () {
        $("#submitmsg").click(function () {
        var clientmsg = $("#usermsg").val();
        var request = $("#request").val();
        var sentby = $("#sentby").val();
        var sentto = $("#sentto").val();    
        $.post("post.php", 
              { 
                 text: clientmsg,
                 request: request,
                 sentby: sentby,
                 sentto: sentto,
              },
              function(result){
                  // you can do anything you want with result
              });
        $("#usermsg").val("");
        return false;
       });
});

【讨论】:

  • 非常感谢 Behzad。我很感激。我正在尝试让它工作,但我在 post.php 插入值时遇到了一些问题。我会尽快回复您!
  • 很奇怪,但是post.php中唯一获取数据的是第一个($text = $_POST['text'];) ....其他都没有获取数据在 ($requestid = $_POST['request']; $sentby = $_POST['sentby']; 等等..)
  • 再次感谢,找到了我的问题。我忘记在表单字段中输入 id="x" 了!!现在可以了!
猜你喜欢
  • 2013-03-24
  • 2021-05-20
  • 1970-01-01
  • 2014-08-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-26
相关资源
最近更新 更多