我有简单的html,JavaScript和PHP代码。我试图通过ajax将一个变量从HTML传递给PHP。
屏幕上出现一个带有按钮的文本框,用户在文本字段中输入,然后单击该按钮。字段文本应显示在字段之前。但在我的例子中,单击按钮时没有任何反应。我在下面发布了我的代码。这是我制作的三个文件:
index.html
<!doctype html>
<html lang="en">
<body>
<input id="name" type="text" /> <input id="button" type="button" value="Load" />
<div id="content"></div>
<script type="text/javascript" src="ajax.js"></script>
</body>
</html>
ajax.js
$('#button').click(function() {
document.write("this is javascript");
var name = $('#name').val();
$.ajax({
url: 'page.php',
data: name,
success: function(data) {
$('#content').html(data);
}
});
});
page.php
<?php
if(isset($_GET['name'])) {
echo "lllllllllllllllll";
echo $name=$_GET['name'];
}
?>
发布于 2018-08-12 04:22:08
如上所述添加CDN。还要确保在将外部js文件ajax.js包含在index.html中之前加载jquery CDN或jquery.lib.js。
您的$.ajax代码中存在一个问题:
$.ajax({ url:‘. this’,页面数据:名称,页面成功:函数(数据){$(‘# ’).html(数据);} });修改为:
$.ajax( {类型:‘GET’,url:'page.php',data:"name=test“},成功: function(data) { $('#content').html(data);} );
发布于 2018-08-12 07:44:02
仅在axaj.js之前添加
<script type="text/javascript" src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
发布于 2018-08-12 09:18:42
在您的javascript ajax中添加以下内容:type : 'GET'
。在你的php代码中:
$name=$_GET['name'];
echo $name;
https://stackoverflow.com/questions/51803141
复制相似问题