dongmu2026 2014-03-27 15:35 采纳率: 100%
浏览 46
已采纳

ajax运行php文件

I have this script in a .html file on my page and im trying to just run the getip.php file from here rather than to rename the file to .php. I tried this ajax call but its not working and not logging the ip in the logfile.txt. Please let me know what I am doing wrong.

Ajax in the head of the .html file:

 <script>
    $(document).ready(function() {
         $.ajax({

                type: "GET",
                url: "getip.php",

            }); // Ajax Call
        }); //event handler
   </script>

Code from getip.php:

<?php
// location of the text file that will log all the ip adresses
$file = 'logfile.txt';

// ip address of the visitor
$ipadress = $_SERVER['REMOTE_ADDR'];

// date of the visit that will be formated this way: 29/May/2011 12:20:03
$date = date('d/F/Y h:i:s');

// name of the page that was visited
$webpage = $_SERVER['SCRIPT_NAME'];

// visitor's browser information
$browser = $_SERVER['HTTP_USER_AGENT'];

// Opening the text file and writing the visitor's data
$fp = fopen($file, 'a');

fwrite($fp, $ipadress.' - ['.$date.'] '.$webpage.' '.$browser."
");

fclose($fp);
?>
  • 写回答

2条回答 默认 最新

  • drfb52000 2014-03-27 15:39
    关注

    Have you tried $.post() function instead? Example:

    <script>
        $(document).ready(function() {
             $.post('getip.php'); // Ajax Call
            }); //event handler
    </script>
    

    You can also add a callback as a second parameter.

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

报告相同问题?

悬赏问题

  • ¥15 孟德尔随机化结果不一致
  • ¥20 求用stm32f103c6t6在lcd1206上显示Door is open和password:
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法