大家好,我是Web编程的新手,我在这个问题上苦苦挣扎了四天,上下搜索,但是我不知道该怎么做。 当我转到“ http://localhost:3000/backend/roombook.php”时,它下载的文件不执行。
我找到了此修复程序,但似乎无法正常工作here
我正在使用带有实时服务器和xampp的php的PHP运行angularjs。
这是我的html
<form name="form" ng-submit="insertData()">
<label class="text-succes" ng-show="succesInsert">{{succesInsert}</label>
<div class="row">
<div class="col-md-5 col-sm-5">
<div class="panel panel-primary">
<div class="panel-heading">
PERSONAL INFORMATION
</div>
<div class="panel-body">
<div class="form-group">
<label>Title*</label>
<select name="title" ng-model="title" class="form-control" required>
<option value selected></option>
<option value="Dr.">Dr.</option>
<option value="Miss.">Miss.</option>
<option value="Mr.">Mr.</option>
<option value="Mrs.">Mrs.</option>
<option value="Prof.">Prof.</option>
<option value="Rev .">Rev .</option>
<option value="Rev . Fr">Rev . Fr .</option>
</select>
</div>
<div class="form-group">
<label>First Name</label>
<input name="fname" ng-model="fname" class="form-control" required>
<span class="text-danger" ng-show="errorFirstname">{{errorFirstname}}</span>
</div>
<div class="col-md-12 col-sm-12">
<div class="well">
<button class="btn btn-primary" ng-click="login()">Rezervare</button>
</div>
</div>
</div>
</form>
我的angularjs控制器
app.controller('contactCtrl', function ($scope, $http) {
$scope.insert = {};
$scope.insertData = function () {
$http.post('http://localhost:3000/backend/roombook.php', {
data: $scope.insert
}).then(function succes(res) {
console.log(res);
alert('works')
}, function (res) {
console.log(res);
alert('doesn't work')
$scope.insert = null;
$scope.errorFirstname = null;
$scope.errorLastname = null;
}, );
};
});
和/backend/roombok.php
<?php
header('Content-Type: application/json');
$con = mysqli_connect("localhost","admin","admin","projectz") or die( 'Connection failed:'.mysql_error());
$form_data = json_decode(file_get_contents("pgp://input"),true);
$data=array();
$error=array();
if(empty($form_data->fname)){
$error["fname"] = "need first name";
}
if(empty($form_data->lname)){
$error["lname"] = "need last name";
};
if(!empty($error)){
$data["error"]= $error;
}
else{
$fname= mysqli_real_escape_string ( $con,$form_data->fname);
$lname= mysqli_real_escape_string ( $con,$form_data->lname);
$query="INSERT INTO `roombook`(`Title`, `FName`, `LName`, `Email`, `National`, `Country`, `Phone`, `TRoom`, `Bed`, `NRoom`, `Meal`, `cin`, `cout`,`stat`,`nodays`) VALUES ('$title','$fname','$lname','$email','$nation','$country','$phone','$troom','$bed','$nroom','$meal','$cin','$cout','$new',datediff('$cout','$cin'))";
if(mysqli_query($con,$query)){
$data["message"] = "Data inserted.."
}
}
echo json_encode($_POST);
echo json_encode($data);
?>
如果您能使它工作,我将不胜感激:(