javascript - Call the php function using ajax -
i have editor embeded in html page
<div id="editor"> problem statement goes here… </div>
i want store contents written in editor file(preferably in rich text format). used script (given below) storing contents in string.(this used referring html div text save , display )
<script type='text/javascript' src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"> var storeeditorcontent; //declare variable save content document.getelementbyid('save').addeventlistener("click", savetext); // adding event listner onclick saving text function savetext(){ storeeditorcontent = document.getelementbyid('editor').innerhtml; // save content document.getelementbyid('editor').innerhtml = ""; // blank editor content window.onload = function(){ $.ajax({ url: "submit_request.php", type: "get", success: function writemsg(storeeditorcontent){ //not sure if part right. } }); } } </script>
this of course of study storing in contents in string storeeditorcontent
. want pass string php function write this(storeeditorcontent
) file.the php file contains function write given below
<?php function writemsg($msg){ $file = "myfile.txt"; file_put_contents($file, $msg); } ?>
i know need utilize ajax here, cannot figure out how? help appreciated.
you need provide data:
alternative $.ajax()
:
$.ajax({ url: "submit_request.php", type: "post", data: { content: storeeditorcontent }, success: function (response) { // response sent php } });
then php can do:
writemsg($_post['content']);
you should utilize post
rather get
big requests because limit on get
parameters relatively small.
javascript php jquery html ajax
No comments:
Post a Comment