views:

156

answers:

3

Okay. I have a form - textarea (named "su") and submit button.

When the form is submitted, I need to

  1. run a PHP script without refreshing/leaving page
  2. "echo" or somehow print a return on the screen

I'm pretty sure this works via some kind of ajax request thing. but I have no idea what I'm talking about. I'm not big on ajax or javascript, but this is a function i use very frequently and I'd like to learn how it works so I can implement it when i need to now and in the future.

Like i said I'm uneducated with ajax or java. a quick example would be wonderful.

thanks for taking the time to read!

A: 

It is usually best to use a Javascript library for doing AJAX.

See jQuery.get() for one way to send a request to a PHP web page using the jQuery library. You can have your PHP page output Javascript to be executed, or output plain text or data.

phsource
OKaay thanks. I'm working on my own here trying to piece things together. Implemented one of the examples on the linked page you gave me, but i have one q. How do I tie my form action to the function i just created?function: $.get("back/update.php", { name: "John", time: "2pm" }, function(data){ alert("Data Loaded: " + data); });
Dylan Taylor
There's a jQuery plugin called jQuery forms for that:http://plugins.jquery.com/project/formThe syntax is fairly similar and the examples explain most of it, I think.
phsource
A: 

Simple , that is what is called AJAX. The solution is more of Javascript, than PHP. Using Jquery, you can do it with a simple function call.

      <script src="jquery.js"></script>

    <script type="text/javascript">
     $(document).ready(function(){
            $('#ID_Of_Your_Button').change(function(){
                 $.ajax({
                       type: "GET",
                       url: "send.php",
                       data: "query="+document.form.textarea.value,
                       success: function(msg){
                        document.getElementById("Div_Where_you_want_the_response").innerHTML = msg                         }
                     })
            });
        });


    </script>

Does the trick.

aswintyv
Alright dude just what i was looking for. I filled in all the placeholders you gave me and my jquery file is the latest and available, but for some reason this isn't working. I've also change the "query" in data to just "su", changed TYPE to POST (in your script) and adjusted my PHP page to grab the "su" data via POST.<br><br>It's still not working. I have no clue what the problem is - everything corresponds and matches up perfectoly between the form, script and PHP page..
Dylan Taylor
Can you paste the exact code ?
aswintyv
A: 

If your just now getting into ajax, PHP, and JQuery I would highly suggest using firefox and installing Firebug. Learn to use it and it will tell you all sorts of great things. There is not enough space to tell you everything it does, but it is ,at this point, one of my best debugging tools. Learn it, Love it and good luck.

ozruiz