简体   繁体   中英

Why won't my php file run from my javascript?

I'm building a website that contains a form, with a submit button at the bottom. When I click this button it is suppose to call a function in my javascript that sends a request to a php file that then sends information to a mysql server that is running on my laptop. This is the javascript file:

function registerUser()
{

    if (window.XMLHttpRequest)
    {
        // Code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    }
    else
    {
        // Code for IE6, IE5
        xmlhttp = ActiveXObject("Microsoft.XMLHTTP");
    }

    xmlhttp.open("POST",
                 "RegisterUser.php",
                 true);

    xmlhttp.send(); 
}

and this is the php file:

<?php

    $connection = mysqli_connect('127.0.0.1', 'root', '')
    or die("Unable to connect to database");


    $database = mysqli_select_db($connection, "database")
    or die("Could not select database");

    $sql = "INSERT INTO test VALUES ('12', 'test');";
    $execute = mysqli_query($connection, $sql);

?>

I have tried running the php file on my local host and that works fine, so there is no problem with the mysql server. However the website isn't running on a server, it is just opened in my browser from my laptop. I don't know if that is a problem. In the browser under developer, I can see that the php file is loaded, but it doesn't seem to be executed. So how do I execute the php file properly?

Can anyone please help me?

The problem was that I was running my website from my file system. I wasn't aware I needed to deploy it to a web server. So I deployed it to my apache web server and now it is working :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM