Execute PHP without leaving page

2020-03-06 03:10发布

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.

3条回答
▲ chillily
2楼-- · 2020-03-06 03:52

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.

查看更多
太酷不给撩
3楼-- · 2020-03-06 04:06

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.

查看更多
干净又极端
4楼-- · 2020-03-06 04:06

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.

查看更多
登录 后发表回答