javascript - intel xdk-将 html 连接到 php 以使用 mysqli 将值保存到数据库

标签 javascript php html mysql mysqli

我有 2 个文件,一个名为“index.html”,另一个名为“student.php”。 index.html代码如下所示:

<!DOCTYPE html>
<html>
<head>
<title>insert data in database using mysqli</title>
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>

<div id="main">
<h1>Insert data into database using mysqli</h1>
<div id="login">
<h2>Student's Form</h2>
<hr/>



    <Form Name ="form1" Method ="POST" ACTION = "student.php">


<!--<form action="" method="post"> -->
<label>Student Name :</label>
<input type="text" name="stu_name" id="name" required="required" placeholder="Please Enter Name"/><br /><br />
<label>Student Email :</label>
<input type="email" name="stu_email" id="email" required="required" placeholder="john123@gmail.com"/><br/><br />
<label>Student City :</label>
<input type="text" name="stu_city" id="city" required="required" placeholder="Please Enter Your City"/><br/><br />
<input type="submit" value=" Submit " name="submit"/><br />
</form>
</div>
<!-- Right side div -->

</div>

</div>

</body>
</html>

学生.php:

<?php
if(isset($_POST["submit"])){
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "college";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

$sql = "INSERT INTO students (student_name, student_email, student_city)
VALUES ('".$_POST["stu_name"]."','".$_POST["stu_email"]."','".$_POST["stu_city"]."')";

if ($conn->query($sql) === TRUE) {
echo "<script type= 'text/javascript'>alert('New record created successfully');</script>";
} else {
echo "<script type= 'text/javascript'>alert('Error: " . $sql . "<br>" . $conn->error."');</script>";
}

$conn->close();
}
?>

输入学生姓名、电子邮件和地址后,我单击“提交”按钮。下一页上出现以下错误:无法 POST/http-services/emulator-webserver/ripple/userapp/x/C/xampp/htdocs/xampp/please/www/student.php。我之前测试过数据库连接,应用程序能够连接到数据库,因此这不是问题。我的代码有问题吗?

最佳答案

您需要通过 AJAX 将 html 代码连接到 PHP 脚本。使用 JSON 来传输数据。

本视频介绍了如何使其发挥作用。

https://www.youtube.com/watch?v=NxCeoiouaLs

关于javascript - intel xdk-将 html 连接到 php 以使用 mysqli 将值保存到数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36103784/

相关文章:

javascript - 谷歌加一个按钮

html - 响应式 CSS 表单字段

html - 在我的下拉菜单上添加第三层

javascript对象变量键

c# - 有没有一种简单的方法可以将 Javascript 解释器嵌入到 C# 应用程序中?

javascript - AngularJS 代码中没有发生继承

php - 如何打印(导出)到 MS-Word 搜索结果 (PHP)

javascript - 如何在我的进度条达到 100% 后自动切换表单

php - 我们可以在不等于的情况下使用 whereIn 子句吗

php - 在 mysql、php 中搜索年龄范围