douhuangjie4503 2014-10-23 04:05
浏览 110
已采纳

使用ajax调用php函数

I have a editor embeded in the html page

<div id="editor">
Problem Statement goes here&hellip;
</div>

I basically want to store the contents written in editor to a file(preferably in rich text format). I used a script (given below) for storing the contents in a string.(This I used by referring to HTML div text to save and display )

<script type='text/javascript' src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"> 
     var StoreEditorContent;  //declare a variable to save Content

      document.getElementById('save').addEventListener("click", SaveText);  // adding event listner for onclick for Saving text

    function SaveText(){

       StoreEditorContent = document.getElementById('editor').innerHTML; // Save the Content into 
       document.getElementById('editor').innerHTML = ""; // to blank the editor Content
       window.onload = function(){

                $.ajax({
                    url: "submit_request.php",
                    type: "GET",
                    success: function writeMsg(StoreEditorContent){
                       //Not sure if this part is right.
                    }
                });   
        }
    }

    </script>

This of course is storing in the contents in a string StoreEditorContent. I now want to pass this string to a php function which will write this(StoreEditorContent) to a file.The php file that contains function to write is given below

<?php

function writeMsg($msg){
    $file = "myfile.txt";
    file_put_contents($file, $msg);
}
?>

I know I need to use ajax here, but cannot figure out how? Any help appreciated.

  • 写回答

1条回答 默认 最新

  • douyue7536 2014-10-23 04:11
    关注

    You need to provide a data: option to $.ajax():

    $.ajax({
        url: "submit_request.php",
        type: "POST",
        data: { content: StoreEditorContent },
        success: function (response) {
            // Do something with the response sent from PHP
        }
    });
    

    Then your PHP can do:

    writeMsg($_POST['content']);
    

    You should use POST rather than GET for large requests because the limit on GET parameters is relatively small.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 logisim中设计一个位于十字路口的交通信号灯控制系统
  • ¥15 DispatcherServlet.noHandlerFound No mapping found for HTTP request with URI[/untitled30_war_e
  • ¥15 使用deepspeed训练,发现想要训练的参数没有梯度
  • ¥15 寻找一块做为智能割草机的驱动板(标签-stm32|关键词-m3)
  • ¥15 信息管理系统的查找和排序
  • ¥15 基于STM32,电机驱动模块为L298N,四路运放电磁传感器,三轮智能小车电磁组电磁循迹(两个电机,一个万向轮),怎么用读取的电磁传感器信号表示小车所在的位置
  • ¥15 如何解决y_true和y_predict数据类型不匹配的问题(相关搜索:机器学习)
  • ¥15 PB中矩阵文本型数据的总计问题。
  • ¥15 MATLAB卫星二体模型仿真
  • ¥15 怎么让数码管亮的同时让led执行流水灯代码