Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Execute PHP without leaving page

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

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

like image 598
Dylan Taylor Avatar asked Jan 28 '26 08:01

Dylan Taylor


2 Answers

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.

like image 80
aswintyv Avatar answered Jan 30 '26 22:01

aswintyv


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.

like image 23
ozruiz Avatar answered Jan 30 '26 21:01

ozruiz