为什么ajax没有将文本号传递给php?
html:
<!DOCTYPE html>
<html lang="en">
<head>
<!-- Jquery -->
<script
src="https://code.jquery.com/jquery-3.3.1.min.js"
integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="
crossorigin="anonymous"></script>
<!-- Ajax -->
<script>
$(document).ready(function() {
var text = 1;
$("#runAjax").click(function() {
text = text + 1;
$(".body").load("php.php", {textNumber: text});
});
});
</script>
</head>
<body>
<div class="body">
<div class="banner">
text 1
</div>
<button class="nxt" id="runAjax">Next</button>
</div>
</body>
</html>
这是php:
<?php
$n = $_POST['textNumber'];
$string1 = '<div class="banner">text 1</div>';
$string2 = '<div class="banner">text 2</div>';
if ($n === 1) {
echo $string1;
} elseif ($n === 2) {
echo $string2;
} else {
echo '<div class="banner">No more text</div>';
}
当我单击按钮时,它显示为“
注意:未定义的索引:textNumber在 第2行的C:\ xampp \ htdocs \ MyBanner \ php.php没有更多文本