我在 index.php 取得參數 a 和 b 後
想使用 ajax 傳送到 runtest.php
但失敗了
程式如下
index.php
<script>
function submitInput() {
//javascript 取得 a 和 b ...
alert(a); //成功
alert(b); //成功
//send POST request to runtest.php
$.ajax({
type: "POST",
url: "runtest.php",
data:{Text1:a,Text2:b},
success:function(result) {
alert(result);
},
error:function(xhr) {
alert('Ajax request 發生錯誤');
}
});
}
</script>
runtest.php
<?php
$Text1 = $_POST["Text1"];
$Text2 = $_POST["Text2"];
echo $Text1."\n".$Text2;
?>
謝謝
我測是都可以正常啦
front.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<script src = "https://code.jquery.com/jquery-3.4.1.min.js"></script>
<title>Document</title>
</head>
<body>
</body>
<script>
$(document).ready(() => {
const a = 'demo';
const b = 'ithelp';
$.ajax({
url: 'back.php',
data: {
Text1 : a,
Text2 : b,
},
method: 'POST',
success(res) {
console.log(res);
},
error(xhr) {
console.log(xhr);
}
})
});
</script>
</html>
back.php
<?php
$Text1 = $_POST["Text1"];
$Text2 = $_POST["Text2"];
echo $Text1."\n".$Text2;
result
demo
ithelp