how to run php code on submit button without refreshing/ reloading the page

巧了我就是萌 提交于 2019-12-02 04:28:43

Well, you need to use the javascript / ajax.

Example: on your submit link (a href for exaple), add call-in-to js function submitMe and pass on whatever variables you need

function submitMe() {
    jQuery(function($) {    
        $.ajax( {           
            url : "some_php_page.php?action=getsession",
            type : "GET",
            success : function(data) {
                alert ("works!"); //or use data string to show something else
                }
            });
        });
    }

IF you want to change some content dynamically, it is easy- you just need to create tags, and assign ID to them : <div id="Dynamic"> </div>

Then you load ANYTHING between those two tags using

document.getElementById("Dynamic").innerHTML = "<b>BOOM!</b>";

Meaning that you calling area between two tags and loading something into them. The same way you GET data from that place:

alert(document.getElementById("Dynamic").innerHTML);

Please read this: http://www.tizag.com/javascriptT/javascript-getelementbyid.php

In addition, play and experiment with DOM elements and learn how they interact. It is not hard, just takes some time to grasp all concepts.

Whenever you send request ajax (with plain js anyway) from a html form, make sure you add the return false statement to prevent redirection: something like:

<form method="post" ... onsubmit="ajax_post(); return false;">

You have to use ajax, but you can do it in plain javascript (without jquery). Jquery makes it easier.

plain javascript example: This function will trigger an ajax, via get, without parameter: you can tweak it so it run in POST and be able to send some parameter: file represent the php file to request and html represent the container whre the data will be displayed:

function plain_ajax(file,html){
    if (window.XMLHttpRequest){ 
          r=new XMLHttpRequest();   //For Most BRowser
    } else{ 
          r=new ActiveXObject("Microsoft.XMLHTTP");  //for Explorer
    }

    //When the state change
    r.onreadystatechange=function(){ 
       //ReadyState 4 = Loaded     see: http://www.w3.org/TR/2006/WD-XMLHttpRequest-20060405/
       //status 200 = ok           see: http://en.wikipedia.org/wiki/List_of_HTTP_status_codes
       if(r.readyState==4 && r.status==200){
           //Replace the content with the response, Using creatDocumentFragment is more efficient 
           //at the cost of a few more lines But it  would alos allow you to append the data
           //instead of replacing it using innerHTML;
           document.getElementById(html).innerHTML = r.responseText;
        }
    }

    //Opening the connection and sending the request.
    r.open("GET",file,true); 
    r.send();
}

Your HTML or PHP form

<form action="submit.php"  method="post">
Name: <input name="name" id="name" type="text" /><br />
Email: <input name="email" id="email" type="text" /><br />
Phone Number: <input name="phone" id="phone" type="text" /><br />
<input type="button" id="searchForm" onclick="SubmitForm();" value="Send" />
</form>

Your JavaScript

 <script>
 function SubmitForm() {
 var name = $("#name").val();
 var email = $("#email").val();
 var phone = $("#phone").val();
 $.post("submit.php", { name: name, email: email, phone: phone },
 function(data) {
 alert("Data Loaded: " + data);
 });
 }
 </script>

Your PHP page to do some activity

 <?php
 echo $_POST['name']."<br />";
 echo $_POST['email']."<br />";
 echo $_POST['phone']."<br />";
 echo "All Data Submitted Sucessfully!"
 ?>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!