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条)

报告相同问题?

悬赏问题

  • ¥15 请问有用MZmine处理 “Waters SYNAPT G2-Si QTOF质谱仪在MSE模式下采集的非靶向数据” 的分析教程吗
  • ¥50 opencv4nodejs 如何安装
  • ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
  • ¥15 nginx反向代理获取ip,java获取真实ip
  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页
  • ¥15 376.1电表主站通信协议下发指令全被否认问题
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥15 复杂网络,变滞后传递熵,FDA
  • ¥20 csv格式数据集预处理及模型选择