且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

无需提交表单即可将值从html表单传递到php

更新时间:2023-09-26 21:31:58

是的,它被称为AJAX。 :)



在jQuery中,为简洁起见:

  //或$('#textbox_autopost')。如果你想在盒子失去焦点的时候做模糊处理,那么模糊
$('#textbox_autopost')。change(function(){
$ .ajax({
类型:POST,
url:some.php,
data:{text:$(this).val()}
});
}) ;






如果您想通过按钮点击

 < button id =inlinesubmit_buttontype =button> submit< / submit> 

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

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

 <! - 备份如果JS不可用 - > 
< a href =handler.php id =inline_submit_a>添加评论< / a>

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

你想要输入:

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















$ b
  $(document).ready(function(){
function submitMe (选择器)
{
$ .ajax({
type:POST,
url:some.php,
data:{text:$(选择器).val()}
});

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


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 onclick should pass data to PHP.

Here is something I heard:

  • you can call a JS function onclick and then you can submit the form from there

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.

Yes, it is called AJAX. : )

In jQuery, for brevity:

// 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:

<!-- 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;
      }
   });