将值从 html 表单传递给 php 而不提交表单

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6065101/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:15:54  来源:igfitidea点击:

Pass value from html form to php without submitting the form

phphtml

提问by koool

I have a text box. I want to take user input from there and pass it to PHP without using submit button. I would prefer a normal button and onclickshould pass data to PHP.

我有一个文本框。我想从那里获取用户输入并将其传递给 PHP,而不使用提交按钮。我更喜欢普通按钮,并且onclick应该将数据传递给 PHP。

Here is something I heard:

这是我听到的:

  • you can call a JS function onclickand then you can submit the form from there
  • 你可以调用一个 JS 函数onclick,然后你可以从那里提交表单

But I want to be on the same page after submitting the form. That's why I didn't want to submit the form in the first place. But looks like there is no other way.

但我想在提交表单后在同一页面上。这就是我一开始不想提交表单的原因。不过好像也没有别的办法了。

If anyone knows about the submit method, please, help.

如果有人知道提交方法,请帮忙。

回答by John Green

Yes, it is called AJAX. : )

是的,它被称为 AJAX。:)

In jQuery, for brevity:

在 jQuery 中,为简洁起见:

// or $('#textbox_autopost').blur if you want to do it when the box loses focus
$('#textbox_autopost').change(function(){
    $.ajax({
        type: "POST",
        url: "some.php",
       data: {text:$(this).val()}
    });
});


if you want to do it via button click

如果你想通过按钮点击来做到这一点

<button id="inlinesubmit_button" type="button">submit</submit>

$('#inlinesubmit_button').click(function(){
    $.ajax({
        type: "POST",
        url: "some.php",
       data: {text:$('#textbox').val()}
    });
});

You can also do it through an A HREF (or submit button, or or something else wacky:

您也可以通过 A HREF(或提交按钮,或其他古怪的东西:

<!-- backup if JS not available -->
<a href="handler.php" id="inline_submit_a">add comment</a>

$('#inline_submit_a').click(function(evt){
    $.ajax({
        type: "POST",
        url: "some.php",
       data: {text:$('#textbox').val()}
    });
    evt.preventDefault();
    return false;
});

If you want to do it on enter:

如果您想在输入时执行此操作:

$('#textbox_autopost_onenter').keydown(function(evt){
    if ((evt.keyCode) && (evt.keyCode == 13))
    {
      $.ajax({
        type: "POST",
        url: "some.php",
        data: {text:$(this).val()}
      });
      evt.preventDefault();
      return false;
    }
});


Final, site-ready code:

最终的站点就绪代码:

$(document).ready(function(){
   function submitMe(selector)
   {
        $.ajax({
          type: "POST",
          url: "some.php",
          data: {text:$(selector).val()}
        });

   }
   $('#textbox_button').click(function(){
      submitMe('#textbox');
   });
   $('#textbox').keydown(function(evt){
      if ((evt.keyCode) &&(evt.keyCode == 13))
      {
         submitMe('#textbox');
         evt.preventDefault();
         return false;
      }
   });

回答by Andy

You'll need to use Javascript to make an AJAX POST request back to the PHP on the server.

您需要使用 Javascript 向服务器上的 PHP 发出 AJAX POST 请求。

There's a good tutorial herethat uses jQuery to validate the form, send it to the server then display a message to the user based on the server's response.

这里有一个很好的教程它使用 jQuery 验证表单,将其发送到服务器,然后根据服务器的响应向用户显示一条消息。

回答by Oliver M Grech

You have to use AJAX.

你必须使用 AJAX。

I highly recommend to use jquery :)

我强烈建议使用 jquery :)

jQuery

jQuery

回答by JFitzDela

Several ways to do that...

有几种方法可以做到这一点......

It sounds like you're after a non-redirecting solution, so I'd recommend jQuery (it's my fave, but there are plenty other solutions to implementing AJAX) and doing something like the following:

听起来您在寻求非重定向解决方案,因此我建议使用 jQuery(这是我的最爱,但还有很多其他解决方案可以实现 AJAX)并执行以下操作:

Assume you have a text box and button like this:

假设你有一个这样的文本框和按钮:

<input id="txt_input" type="text" />
<input id="btn_send" type="button" value="Submit" />

Then do something like this

然后做这样的事情

$(document).ready(function () {
    $("#btn_send").click(function () {

        // Do stuff BEFORE sending... //
        callAFunction();
        var test = "asdfasdf";

        // Send the text to PHP //
        $.post("test.php", { input: $("#txt_input").val()},
             function(data) {
                 alert("test.php returned: " + data);
             }
        );

        // Do more stuff after sending.... //
        callAnotherFunction();
    });
});

I believe that'll get what your after. For more on jQuery and further options with $.post, check here: http://api.jquery.com/jQuery.post/

我相信那会得到你想要的。有关 jQuery 的更多信息以及 $.post 的更多选项,请查看此处:http: //api.jquery.com/jQuery.post/

Hope that helps!

希望有帮助!

回答by jeroen

I′m not sure what you mean by "normal button", but if you don′t want to use an <input type="submit">you can use a <button type="submit">.

我不确定你所说的“普通按钮”是什么意思,但如果你不想使用 an<input type="submit">你可以使用<button type="submit">.

A buttongives you more freedom in layout, but older versions of IE get confused if you use multiple buttonelements in one page.

Abutton为您提供了更多的布局自由,但如果您button在一个页面中使用多个元素,旧版本的 IE 会感到困惑。