0

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.

Samvel Aleqsanyan
  • 2,495
  • 4
  • 18
  • 27
koool
  • 13,917
  • 11
  • 43
  • 60
  • 3
    What is your goal? Do you want to prevent the page from being reloaded? – Pekka May 19 '11 at 21:33
  • yup and another goal is I dont want to use AJAX – koool May 19 '11 at 21:38
  • 1
    java\flash app only thing left i can think of –  May 19 '11 at 21:39
  • 3
    Ajax is generally taken to mean "Communicating with the server without leaving the page". If you aren't submitting the form normally and you are sending data from it to the server, then you are using Ajax. – Quentin May 19 '11 at 21:44
  • 3
    regarding your edit- that's AJAX, as every one pointed out. –  May 19 '11 at 21:44
  • For the sake of comprehensiveness, also see [How to make an AJAX call without jQuery?](https://stackoverflow.com/questions/8567114/how-to-make-an-ajax-call-without-jquery). – showdev Jul 28 '19 at 08:48

5 Answers5

10

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;
      }
   });
John Green
  • 12,706
  • 3
  • 25
  • 50
  • Hi John ... I just want to ask one thing ... will this even send data when the user has not completed typing or it will send the data after user completes typing .... coz it sounds like value will be sent after almost everykeystroke ..... is there anyway I can make sure user has completed typing – koool May 19 '11 at 21:48
  • 1
    yes, this sends on every keypress. How do you want to detect if a user has completed typing? There are a couple of options: User hits Enter, users clicks out of form. If the latter, just change the '.change(function' to '.blur(function'. The former is doable, but requires another line or two of code. – John Green May 19 '11 at 21:57
  • enter or I have a button on form clicking that could notify that user has completed ... thanks for reply – koool May 19 '11 at 22:04
  • Regarding this – koool May 19 '11 at 22:18
  • 1
    Great point, it should read ... data:{text:$('#textbox').val()}... I'll update it now. – John Green May 19 '11 at 22:20
  • Thank you very much I'll try it out... thanks for you patience and quick reply ... I appreciate it – koool May 19 '11 at 22:24
  • 1
    $_POST['text']. You can change the name of the key.. that isn't important to how it works. – John Green May 19 '11 at 22:30
  • HI I could not get this to work ..... can you please help ....... I will put my code in your Answer ... please help – koool May 19 '11 at 23:16
  • @koool, you should place your code in your question, not in @John's answer. :) – sarnold May 19 '11 at 23:39
  • 1
    @koool - Agreed. I think you'll need to post your question... or perhaps better, just make a new one at this point. – John Green May 20 '11 at 01:23
  • I did ... but many replied that what I want to achieve is not possible .... please take a look if you can its here .... http://stackoverflow.com/questions/6066061/passing-a-value-to-php-from-textbox-using-jquery-doesnt-work-please-help – koool May 20 '11 at 01:32
3

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

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

Andy
  • 16,265
  • 9
  • 48
  • 69
  • 1
    @koool: AJAX is really just a name for all the technologies that can connect to a server without posting a form. Without AJAX, you have nothing. Is there a particular brand of AJAX that you want to avoid? – mellamokb May 19 '11 at 21:36
  • 1
    @koool: With AJAX, it happens in the background, and the user doesn't know anything happened. The data is sent, and the server receives it, and the page doesn't redirect, or anything. In fact, unless you have a popup message or something, the user will probably think it's broken, because there will be absolutely no feedback. Is that not the behavior you are wanting? That's what AJAX does. – mellamokb May 19 '11 at 21:45
  • great insight thanks ... yes You can assume something like that ..... its like the user is sending a message and if everything is successful then I use PHP to diplay the subject and "sent" – koool May 19 '11 at 21:54
2

You have to use AJAX.

I highly recommend to use jquery :)

jQuery

Oliver M Grech
  • 2,484
  • 1
  • 16
  • 30
2

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:

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/

Hope that helps!

JFitzDela
  • 71
  • 6
  • HI ... thanks for the answer but on click i want to call other 2 javascript function along with this is that possible – koool May 19 '11 at 21:50
  • @JFitzDela Hi I am trying to do this but I think I am doing something wrong ... how should I access the value .. I use $_POST['input'] ... is that correct ... please help – koool May 19 '11 at 22:15
  • 2
    Sorry I'm so late getting back to you! $_POST['input'] should work fine in this case -- let me know if you're still having trouble! – JFitzDela Jun 02 '11 at 18:41
1

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">.

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

jeroen
  • 88,615
  • 21
  • 107
  • 128