duanfei1930 2018-08-11 19:40
浏览 75
已采纳

JavaScript和PHP代码无法正常工作

I have simple code with html, JavaScript and PHP. I am trying to pass a variable from HTML to PHP through ajax.

A text box appears on screen with button, user inputs in the text field and clicks the button. The field text should appear before the field. But in my case, nothing happens when clicking the button. I am posting my code below. These are the three files I have made:

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'];
    } 
?>
  • 写回答

3条回答

  • dongluanguang4774 2018-08-11 20:22
    关注

    Add CDN as mentioned above. Also make sure you load the jquery CDN or jquery.lib.js before including external js file, ajax.js in index.html.

    There is an issue in your $.ajax code:

    $.ajax({ url: 'page.php', data: name, success: function(data) { $('#content').html(data); } }); change this to:

    $.ajax( { type:'GET', url: 'page.php', data:"name=test" }, success: function(data) { $('#content').html(data); } );

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥100 三相直流充电模块对数字电源芯片在物理上它必须具备哪些功能和性能?
  • ¥30 数字电源对DSP芯片的具体要求
  • ¥20 antv g6 折线边如何变为钝角
  • ¥30 如何在Matlab或Python中 设置饼图的高度
  • ¥15 nginx中的CORS策略应该如何配置
  • ¥30 信号与系统实验:采样定理分析
  • ¥100 我想找人帮我写Python 的股票分析代码,有意请加mathtao
  • ¥20 Vite 打包的 Vue3 组件库,图标无法显示
  • ¥15 php 同步电商平台多个店铺增量订单和订单状态
  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别