2019-04-27 01:21
浏览 390


I'm running my server on localhost using XAMPP, and I want to call a Javascript function which sends some data from the browser to a php script

The JS function is called when the a button on my html page is pressed:

function send_verification_code(){
    var xhttp = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHTTP');'GET','database.php?email='+email_address)

So I want to add, to the $_GET array, a key-value pair of email=>email_address, where email_address is a string, in the function above.

Immediately after the function above executes, I want to call another function, a PHP function which adds that data to a SQL database on my server by calling add_email_to_be_verified (the function belongs to the php script im sending the data to):


     $dbhost = "localhost";
     $dbuser = "root";
     $dbpass = "";
     $db = "example";

     function add_email_to_be_verified(){
         $sql = 'insert into emailstobeverified values(';
         $sql .= $_GET['email'];
         $sql .= ',';
         $sql .= generate_verification_code();
         $sql .= ',';
         $sql .= date("Y-m-d H:i:s");
         $sql .= ');';

     function generate_verification_code(){ //generates a random 6-digit verification code
        $code = mt_rand(0,999999);
        $padded_code = sprintf('%06d', $code);
        return $padded_code;
     function connect(){
        $conn = new mysqli($dbhost, $dbuser, $dbpass,$db) or die("Connect failed: %s
". $conn -> error);
     function query(sql){
     function disconnect(){
        $conn -> close();

How can I call the php function using AJAX?

1条回答 默认 最新

相关推荐 更多相似问题