使用ajax调用php函数
Call the php function using ajax
我在html页面中嵌入了一个编辑器
<div id="editor">
Problem Statement goes here…
</div>
我基本上想把编辑器中编写的内容存储到一个文件中(最好是富文本格式)。我使用了一个脚本(下面给出)将内容存储在一个字符串中。(我通过引用HTMLdiv文本来保存和显示)
<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>
这当然是将内容存储在字符串StoreEditorContent
中。我现在想把这个字符串传递给一个php函数,该函数将把这个(StoreEditorContent
)写入一个文件。下面给出了包含要编写的函数的php文件
<?php
function writeMsg($msg){
$file = "myfile.txt";
file_put_contents($file, $msg);
}
?>
我知道我需要在这里使用ajax,但不知道如何使用?感谢您的帮助。
您需要向$.ajax()
:提供data:
选项
$.ajax({
url: "submit_request.php",
type: "POST",
data: { content: StoreEditorContent },
success: function (response) {
// Do something with the response sent from PHP
}
});
然后你的PHP可以做:
writeMsg($_POST['content']);
对于大型请求,应该使用POST
而不是GET
,因为GET
参数的限制相对较小。
相关文章:
- 通过Ajax将JavaScript函数传递给PHP文件
- 在javascript函数中使用php变量
- 调用php数组中的JS函数
- 执行php函数的onclick事件的其他替代方案
- 使用ajax用jquery调用php函数
- 如何通过ajax增加/减少PHP变量值并重新加载函数
- php函数的ajax html$_POST值返回null
- 如何检查这个代码点火器php函数是否工作,该函数是否在ajax url中使用
- 无法识别从php创建的Js函数
- PHP/JS-EchoJs函数PHP curl发布后的通知
- 如果函数PHP中的条件为true,则禁用链接
- Jquery在Wordpress中不起作用;函数php
- Ajax 中的函数 php
- 函数.php当变量未硬编码时脚本中断
- 可以't调用函数php,ajax
- 获取我的数据库值作为字符串传递给我的javascript函数PHP
- 从下拉菜单中发送和设置3个选定的值到JavaScript函数PHP
- 通过触发AJAX函数——PHP
- 如何从ajax函数(PHP)为js变量赋值
- 通过参数将php变量传递给javascript触发器函数PHP,JAVASCRIPT