我希望将Ionic 3中的表单数据传递给位于本地主机上的PHP文件。 以下是我的Ionic按钮代码:
createEntry(name, description)
{
let body : string = "testname=" + name + "&testval=" + description,
type : string = "application/x-www-form-urlencoded; charset=UTF-8",
url : any = this.baseURI + "manage-data.php",
method : 'POST',
headers : any = new Headers({ 'Content-Type': 'application/json' }),
options : any = new RequestOptions({ headers: headers });
alert(url); //Output: http://localhost:1432/ionic-php-sql/manage-data.php
alert(body); //Output: name=Test&description=Test
alert(options); //[object Object]
console.log(options);
this.http
.post(url, body)
.subscribe(
data => {
console.log(data);
alert(data);
alert('success');
if(data.status === 200)
{
this.hideForm = true;
this.sendNotification(`Congratulations the technology: ${name} was successfully added`);
}
},
err => {
console.log("ERROR!: ", err);
alert(err);
}
);
}
这是我的 PHP 文件代码:
<?php
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Headers: X-Requested-With');
header('Access-Control-Allow-Methods: POST, GET, OPTIONS');
$con = mysqli_connect("localhost","root","","ionic_test");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
else
{
echo "success !";
if($_SERVER['REQUEST_METHOD']=='post')
{
echo "posting";
$postdata = file_put_contents("php://input");
$request = json_decode($postdata);
var_dump($request);
$name = $_POST["testname"];
$desc = $_POST["testval"];
$sql = "INSERT INTO ionictest(testname, testval) VALUES ($name,$desc)";
$stmt = mysqli_query($con, $sql);
}
}
?>
请检查代码,如果有任何错误,请告诉我。 我想从Ionic传递数据到Php文件,然后将其存储到mysql数据库中。 我已经成功地建立了php和mysql数据库之间的连接,但是我无法将数据从Ionic表单传递到php中,虽然它没有显示任何错误。 谢谢。