我想将数据从.ejs文件(即javascript ..因为.ejs文件embedds标记)发送到mysql数据库,我想出了做到这一点的方法,就是通过PHP文件作为中介。但是在这里我得到了错误-
POST http://localhost:8080/storeSqlData.php 404 (Not Found)
在浏览器-chrome浏览器上加载我的localhost:8080后。
我已将.php文件放在项目文件夹中,项目文件夹内以及项目文件夹的每个文件夹中,但无济于事。请帮我找到解决方案,因为互联网没有帮助。
app.ejs -
<html>
<head>
<script async defer src="https://maps.googleapis.com/maps/api/js?key=AIzaSyBwgU_zhAo8cKy6THHRrcpwtaM&callback=initMap">
</script>
</head>
<body>
<div id="floating-panel">
<input id="address" type="textbox" value="Sydney, NSW">
<div>
<button id="submit" type="submit" value="Insert and View Location">Insert and View Location</button>
</div>
</div>
<div id="map"></div>
<script>
// NEW CODE
function initMap() {
loadXMLDoc();
}
function loadXMLDoc() {
console.log("Inside loadXMLDoc()");
var address = document.getElementById('address').value;
var xmlhttp;
if (window.XMLHttpRequest) {
// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp = new XMLHttpRequest();
}
else {
// code for IE6, IE5
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("address").innerHTML = xmlhttp.responseText;
}
}
data = "name=" + address;
console.log("PHP Data = ", data);
xmlhttp.open("POST", "storeSqlData.php", true);
xmlhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xmlhttp.send(data);
}
</script>
</body>
</html>
** storeSqlData.php-**
<?php
if(isset($_POST)){
$value = $_POST['name'];
// $website = $_POST['website'];
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "locationsData";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO locations (value) VALUES ($value)";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
}
?>