小弟最近在寫網頁,想將資料傳送到php並得到參數
php部分已寫好(手機端也用同樣的方式傳送近來得到參數,想說都用相同的php比較方便,不用維護兩隻程式)
但遇到一個問題,始終無法在傳送後的畫面得到值
有一個主頁(index.html)
<form class="form" method="post" action="login.php">
<input type="text" oninvalid="setCustomValidity('Please enter email');" oninput="setCustomValidity('');" id="Email" name="Email" placeholder="Email" required>
<div class="tab"></div>
<input type="text" oninvalid="setCustomValidity('Please enter password');" oninput="setCustomValidity('');" id="Pswd" name="Pswd" placeholder="Password" required>
<div class="tab"></div>
<button type="submit" onclick = "location.href='loginpage.php'">Submit</button>
<!-- <input type="submit" value="Log in" class="submit">-->
</form>
<script>
const formEL = document.querySelector('.form');
formEL.addEventListener('submit', event =>{
event.preventDefault();
const formData = new FormData(formEL);
const data = Object.fromEntries(formData);
//console.log(formData.get('Email'));
fetch('./loginpage.php',{
method: 'POST',
headers:{
'content-Type': 'application/json'
},
body: JSON.stringify(data)
}).then(res => res.json())
.then(data => console.log(data))
.catch(error => console.log(error));
});
</script>
login.php
<?php
header('Content-Type:application/json');
$json = file_get_contents('php://input');
$array = json_decode($json, true);
$Email = $array["Email"];
$Pswd = $array["Pswd"];
include("Connect_Database.php");
$sql1 = "select * from signup_ios where Email = '$Email' AND Pswd = '$Pswd';";
$res1 = mysqli_query($con,$sql1);
$result = array();
while($row = mysqli_fetch_array($res1)){
array_push($result,array(
'FirstName' => $row["FirstName"],
"LastName" => $row["LastName"],
'Pswd' => $row["Pswd"],
'Cellphone' => $row["Cellphone"],
'Email' => $row["Email"]
));
}
$data = array(
'data'=> $result
);
echo json_encode($data,JSON_UNESCAPED_UNICODE);
mysqli_close($con);
?>
點選submit後會到loginpage.php
<!DOCTYPE html>
<html>
<body>
<h1>Welcome to my home page!</h1>
<?php include 'login.php';
echo "data $data ";
?>
</body>
</html>
但卻無法顯示任何資料出來,第一次寫非_POST方式的php,希望各位大大能幫忙解惑
留言處已有回應
wiseguy
去掉onclick = "location.href='loginpage.php'"
請改成已解答, 或者提出你後續的疑問喔