I'm sorry I do not know english
Short'll try to explain short
I don't know Javascript at all, php is very little
I'm trying to use the "Responsive WYSIWYG Text Editor with jQuery and Bootstrap - LineControl Editor" editor
No problem getting data from database like this
$(document).ready(function() {
$("#txtEditor").Editor();
$("#txtEditor").Editor("setText", "<?php echo $my_database?>");
});
<textarea id="txtEditor" name="message"></textarea >
The problem is a: I can't submit, echo $_POST['message'];
Second problem:
Image code in editor does not send ajax
Sample code: <div>Hello World</div><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAR0AAABMCAYAAABQzSrQI...=">
No problem sending "Hello World" text, but does not send image code
var message = $("#txtEditor").Editor("getText");
$.ajax({
type: "POST",
url: url,
data: message,
success: function(dataResult){
}
});
Can you help with these two issues?
Thank you from now
Typically these WYSIWYG editors replace the <textarea> with divs and other markup which makes it not a form field anymore. You'll need to use javascript to get the editor content on submit and either submit it via ajax or add it to another form field:
<textarea id="txtEditor" name="txtEditor"></textarea>
<textarea id="txtEditorContent" name="txtEditorContent" style="display:none;"></textarea>
<input type="submit" value="Submit">
<script>
$("input:submit").click(function(){
$('#txtEditorContent').text($('#txtEditor').Editor("getText"));
});
</script>
Thanks for your reply
Yes, it worked
It is for PHP and writing to text database and rearranging
<textarea id="txtEditor"></textarea>
<textarea id="txtEditorContent" name="txtEditorContent" style="display:none;"></textarea>
<script language="javascript" type="text/javascript">
$(document).ready( function() {
$("#txtEditor").Editor();
$("#txtEditor").Editor("setText", "<?php echo addslashes($my_database); ?>"); // From the database into the editor
$("input:submit").click(function(){
$('#txtEditorContent').text($('#txtEditor').Editor("getText")); // PHP, echo $_POST['txtEditorContent']
});
});
</script>
Submitting with this ajax
$(document).ready(function() {
$("#txtEditor").Editor();
$("#txtEditor").Editor("setText", "Hello World");
});
var message = window.btoa($("#txtEditor").Editor("getText"));
// Javascript decode
$.post( "test.php", { message: message })
.done(function( data ) {
alert( "Data Loaded: " + data );
});
<textarea id="txtEditor"></textarea>
test.php
php encode
$message = base64_decode($_POST['message']);
OR
Javascript encode
var message = window.atob(message);
For beginners like me, detail is needed
Thank you again to everyone
Related
I have a website where you can edit and format text, then you can save it on to the server. I am using jquery to send the data to a PHP page where it will be saved. My site won't send the name of the file and the formatted text to PHP.
Here is my html code:
<div id="editor" contenteditable="true">
This is the editable text.
</div>
<input type="text" id="name" placeholder="File Name">
<input type="submit" id="save" value="Save">
<script>
$(document).ready(function() {
$("#save").on("click", function(event) {
var formData = {text: $("#editor").html(), name: $("#name").val()};
event.preventDefault();
$.ajax({
url: 'freewordsave.php',
type:'POST',
data: formData,
success: function(msg)
{
alert('Your file was saved!');
}
});
});
});
</script>
Here is also my PHP code:
$name = $_POST['name'];
$text = $_POST['data'];
$file = fopen("./location/" . $name . ".html", "w") or die("<script> alert('Error'); </script>");
fwrite($file, $text);
fclose($file);
My code won't even bring up the alert in javascript.
Thank you.
I fixed my code after Simone Rossaini posted a comment.
I forget a } closing bracket at the end of formData.
Fixed JQuery Code.
$(document).ready(function() {
$("#save").on("click", function(event) {
var formData = {text: $("#editor").html(), name: $("#name").val()};
event.preventDefault();
$.ajax({
url: 'freewordsave.php',
type:'POST',
data: formData,
success: function(msg)
{
alert('Your file was saved!');
}
});
});
});
I know my PHP said $_POST['data'] but that was a mistake I made when I asked my question it wasn't actually like that in my PHP file.
Thank you
I am trying to send js variables from my js file to another php file when the user hits "FINISH" on the main php page. Here is my code so far:
map.php
<form action="./finalmap.php">
<input class="finish-button" type="submit" value="FINISH" onclick="sendData();" />
</form>
map.js
function sendData() {
$.ajax({
method: "POST",
url: "../finalmap.php",
data: {
selectedLoc: selectionArray,
startLoc: start,
endLoc: end,
dist: distance,
locTypes: allLocations
},
beforeSend : function(http) { },
success : function(response,status,http) {
alert(response);
},
error : function(http,status,error) {
$('.response').html("<span class='error'>Something went wrong</span>");
$(".response").slideDown();
}
});
}
finalmap.php
<?php
$data = $_POST['data'];
echo $data;
?>
Post is successful and I'm able to see the contents(my code) in my finalmap.php from the alert command. When I try to console.log $data in finalmap.php, it is empty/null.
My goal is to send the data to finalmap.php and redirect to it.
To solve this problem, you must reduce what you're testing to one thing at a time. Your code has errors and is incomplete. So let's start with the errors first: If you're using AJAX, you don't want HTML to submit the form in the regular way. If you get a page refresh, your AJAX didn't work.
<button type="button" id="submit-button">FINISH</button>
Note, no <form> is needed; you're submitting through AJAX.
Next, you need to be sure that your ajax function is being executed (since you're using $.ajax, I presume you have JQuery loaded):
<button type="button" id="submit-button">FINISH</button>
<script>
// all listener functions need to wait until DOM is loaded
$(document).ready(function() {
// this is the same idea as your onclick="sendData();
// but this separates the javascript from the html
$('#submit-button').on('click', function() {
console.log('hello world');
});
});
</script>
You use your web console to see the console.log message.
Now, try out the ajax command with a simple post:
<button type="button" id="submit-button">FINISH</button>
<script>
// all listener functions need to wait until DOM is loaded
$(document).ready(function() {
$('#submit-button').on('click', function() {
$.ajax({
method: "POST",
// "./finalmap.php" or "../finalmap.php"?
url: "../finalmap.php",
data: {foo: 'bar'},
success: function(response){
console.log('response is: ');
console.log(response);
}
});
});
});
</script>
finalmap.php
<?php echo 'This is finalmap.php';
If you see This is finalmap.php in the web console after pressing the button, then you can try sending data.
finalmap.php
<?php
echo 'You sent the following data: ';
print_r($_POST);
See where we're going with this? The way to eat an elephant is one bite at a time.
./finalmap.php is not a thing.
Instead the code must look like this:
<form action="/finalmap.php">
<input class="finish-button" type="submit" value="FINISH" onclick="sendData();" />
</form>
Try using this instead.
EDIT: OOPS SORRY, I JUST CPED AND PASTED.
I'm trying to post data on my HTML code to CI with Ajax. But I got no response?
Here is my JS Code
$(document).ready(function(){
$("#simpan").click(function(){
nama_pelanggan = $("#nama_pelanggan").val();
telp = $("#telp").val();
jQuery.ajax({
type: "POST",
url: "http://192.168.100.100/booking_dev/booking/addBookingViaWeb/",
dataType: 'json',
data : {
"nama_pelanggan":nama_pelanggan,
"telp":telp,
},
success: function(res) {
if (res){
alert(msg);
}
}
});
});
});
And here is my form
<form>
Nama Pelanggan <br>
<input type="text" name="nama_pelanggan" id="nama_pelanggan"><br>
Telepon<br>
<input type="text" name="telp" id="telp"><br>
<input type="button" name="simpan" id="submit" value="Simpan">
</form>
and here is my contoller function code
public function addBookingViaWeb(){
$data = array(
'nama_pelanggan' => $this->input->post('nama_pelanggan'),
'telp'=>$this->input->post('telp')
);
echo json_encode($data);
}
Here is my post param
But I got no response
any idea?
add method in from if you use post then
<form method="post" action ="" >
Try using JQuery form serialize() to declare which data you want to post. It automatically put your form input into ajax data. Example :
first set ID to your form tag
<form id="form">
then
$.ajax({
type:'POST',
url : 'http://192.168.100.100/booking_dev/booking/addBookingViaWeb/',
data:$('#form').serialize(),
dataType:'JSON',
success:function(data){
console.log(data);
}
});
First problem I see is in your ajax submission code. Change
$("#simpan").click(function(){
to
$("#submit").click(function(event){
Notice that I added the event parameter. You now need to prevent the default submission behavior. On the first line of your click method add
event.preventDefault();
Now I'm assuming that your url endpoint http://192.168.100.100/booking_dev/booking/addBookingViaWeb/ can handle POST requests. Usually this is done with something like PHP or Ruby on Rails. If I was doing this in PHP I would write something like the following:
<?php
$arg1 = $_POST["nama_pelanggan"];
$arg2 = $_POST["telp"];
// do something with the arguments
$response = array("a" => $a, "b" => $b);
echo json_encode($response);
?>
I personally don't know anything about handling POST requests with js (as a backend) but what I've given you should get the data over there correctly.
I got solution for my problem from my friend xD
just add header("Access-Control-Allow-Origin: *"); on controller function
Thank you for helping answer my problem.
I am trying to create a simple webapp sort of thing that will send push notifications to my clients on button click. Here is a sample page that i have created
I have a file named as sendPush.php
On button click i want to send a push notification which will be echoed as
Notifications sent:
"Notification sent to userId : xxxX"
"Notification sent to userId : xxxX"
"Notification sent to userId : xxxX"
"Notification sent to userId : xxxX"
I want to send notifis to all 147 users. Now here is my php code for button click
<script type="text/javascript">
function sendNotif()
{
alert('ok');
}
</script>
<div class="content">
<input type="button" value="Click to Send" onClick="sendNotif();">
<br />
<br />
<label for="push">Notifications sent: </label>
</div>
The problem here i am facing is, i have php function in same app named as sendNotification() that will send notification and echo the result. But I am not sure how can i make a loop of this php function in javascript inside javascript function
function sendNotif()
{
// LOOP HERE
}
If $clients is the list of my clients, how can i send notif to all in a loop using php function in same page as sendNotification($client)
MOdified
<script type="text/javascript">
var lastIdCount = 0;
function sendNotif()
{
var clients = "<?php echo $clients; ?>";
var getPath = "push.php?clientId=".concat(clients['lastIdCount']);
$.ajax({
type: "POST",
url: getPath,
task: "save",
data: {
ajax: "true",
},
dataType : 'json'
}).done(function( msg )
{
alert('ok');
if( msg.status=="1")
{
alert('okasdf');
lastIdCount++;
sendNotif();
}
else
{
alert("Error : "+msg.error);
}
});
}
</script>
In push.php
sample
$resp = array();
$resp['error'] = 'Invalid Request';
$resp['status'] = '0';
$resp['data'] = '0';
You can try first to get all clients you want to send notification and use them ID's for setInterval or setTimeout functions which would repeat your queries. Probably you should
get_clients.php
<?php
$clients_array = array(1,2,6,15,29); /// let's say ID's you got from SQL or w/e you need.
echo json_encode($clients_array); // [1,2,6,15,29]
?>
send_for_client.php
<?php
$id = isset($_POST['id'])?$_POST['id']:false;
if($id){
// do some code you need
echo "Notification sent for id: ".$id;
}
?>
index.html
...
<head>
<script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
<script>
$(window).load(function(){
$("#send").click(function(){
$.post('get_clients.php',{},function(cid){
obj = JSON.parse(cid);
var cids = obj;
/// cids is array of clients. so, you can do like:
var i = 0;
var interval = setInterval(function(){
if(cids.length > i){
$.post('send_for_client.php',{id:cids[i]},function(resp){
$("#result").append(resp+"<br />");
i++;
});
} else {
clearInterval(interval);
}
},100);
});
});
});
</script>
</head>
<body>
<input id="send" type="submit" name="button" value="Send notifications" />
<div id="result">
</div>
</body>
...
I'm not tested this think, however it should work or simply show idea how you could try to find a solution for your problem. Have in mind this code can have mistakes so.. don't be lazy to check them out, not even do copy/paste :)
I hope it helped even a bit.
javascript and php are run in 2 different places. Your javascript runs in a browser while your php runs on the server. You cant really mix those two.
The way you probably want to do this is, on button click capture the click with javascript and send ajax request to your php script sitting on the server. Than have the php perform push notifications. Once php script is done, return result back to javascript to show it to the user.
You should also use javascript library like jquery which makes things much easier (especially the ajax call).
I have a contenteditable div like this:
<form>
<div name="new_post" class="post" contenteditable="true"></div>
<button type="submit">Submit</button>
</form>
The contenteditable div allows bold and italic tags, but no other tag.
My issue is that if the user types something like Hello there <u>world</u>!, it will save in the database as Hello there. It seems to remove everything after the tags and I don't know why.
I'm using AJAX and PHP to handle posts, so here's the rest of the code.
Ajax:
$(document).ready(function() {
$(document).on("submit", "form", function(event) {
event.preventDefault();
alert($('.post').html()); // added this to debug it. This prints "Hello there <u>world</u>!"
$.ajax({
url: 'php/post.php',
type: 'POST',
dataType: 'json',
data: $(this).serialize() + "&post_field=" + $('.post').html(),
success: function(data) {
alert(data.message);
}
});
});
});
PHP:
<?php
$post = $_POST["post_field"];
// query to insert post into database goes here
$array = array('message' => $post);
echo json_encode($array);
?>
Please help!
Change:
$('.post').html()
to:
encodeURIComponent($('.post').html())
The HTML characters need to be encoded in a URL parameter.