gpt4 book ai didi

php - 如何将表单数据从 Ionic 3 传递到 PHP 文件?

转载 作者:行者123 更新时间:2023-12-01 13:28:04 26 4
gpt4 key购买 nike

我想将表单数据从 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,尽管它没有显示任何错误。提前致谢。

最佳答案

我解决了我的问题! ionic 按钮点击代码:

submitEntry(name, description)
{
var link = this.baseURI + "manage-data.php";
var body = JSON.stringify({testname: name, testval: description});

alert("DATA: "+body);

this.http.post(link, body)
.subscribe(data => {
console.log("DATA:", data);
this.hideForm = true;
this.sendNotification(`Congratulations the technology: ${name} was successfully added`);
},
err => {
console.log("ERROR!: ", err);
alert(err);
});
}

PHP文件代码:

<?php
if(isset($_SERVER['HTTP_ORIGIN']))
{
header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}");
header('Access-Control-Allow-Credentials: true');
header('Access-Control-Max-Age: 86400'); // cache for 1 day
}

//Access-Control headers are received during OPTIONS requests
if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS')
{
if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD']))
header("Access-Control-Allow-Methods: GET, POST, OPTIONS");
if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']))
header("Access-Control-Allow-Headers: {$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}");
exit(0);
}

$postdata = file_get_contents("php://input");
if(isset($postdata))
{
$request = json_decode($postdata);
$name = $request->testname;
$desc = $request->testval;

if($name != "" && $desc != "")
{
$con = mysqli_connect("localhost","root","","ionic_test");

// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
else
{
echo "Name: " .$name;
echo "Desc: " .$desc;

$sql = "INSERT INTO ionictest(testname, testval) VALUES ('$name', '$desc')";
$stmt = mysqli_query($con, $sql) or die ("MySQL Error:".mysqli_error($con));

echo "successfully inserted !";
}
}
else
{
echo "Empty name and description parameter!";
}
}
else
{
echo "Not called properly with name and description parameter!";
}
?>

如果有人需要,您可以引用代码...:)

关于php - 如何将表单数据从 Ionic 3 传递到 PHP 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47466814/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com