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.

6
  • 3
    What is your goal? Do you want to prevent the page from being reloaded? Commented May 19, 2011 at 21:33
  • yup and another goal is I dont want to use AJAX Commented May 19, 2011 at 21:38
  • 1
    java\flash app only thing left i can think of Commented May 19, 2011 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. Commented May 19, 2011 at 21:44
  • 3
    regarding your edit- that's AJAX, as every one pointed out. Commented May 19, 2011 at 21:44

5 Answers 5

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;
      }
   });
Sign up to request clarification or add additional context in comments.

11 Comments

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
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.
Great point, it should read ... data:{text:$('#textbox').val()}... I'll update it now.
$_POST['text']. You can change the name of the key.. that isn't important to how it works.
@koool - Agreed. I think you'll need to post your question... or perhaps better, just make a new one at this point.
|
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.

3 Comments

@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?
@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.
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"
2

You have to use AJAX.

I highly recommend to use jquery :)

jQuery

Comments

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!

3 Comments

HI ... thanks for the answer but on click i want to call other 2 javascript function along with this is that possible
@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
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!
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.