I have a problem with contenteditable .
Before i used textarea for sending posts for text. I want to change it now to use contenteditable.
So contenteditable doesn't allow to send php strings from data. For example
DEMO
In this demo i will show you my html and ajax codes. That is working perfectly, if user send normal text. But if user want to share php codes from his friends like (<?php echo 'Hi There.';?>) then problem will be come here. If user write normal text like (Hi There.) then not have any problem.
Normaly we are using val(); if we use textarea like this
var updateText = $('#post-text-area').val();
I think problem will be come here but i am not sure.
use with encodeURIComponent() .it will prevent the tag's passing from html to php .And retrieve the data from php use with decodeURIComponent()
$(document).ready(function() {
$("body").on("click", ".sendPost", function() {
var updateText = $('#post-text-area').html();
var dataString = 'update=' + encodeURIComponent(updateText);
$.ajax({
type: 'POST',
url: '/posttext.php',
data: dataString,
beforeSend: function() {},
success: function(html) {
decodeURIComponent(html);
}
});
});
Related
I've got text that is 'ajaxed' in a textarea which is inside an iFrame (the source is from the same website).
If I set the content directly as an argument of one of the functions I tried it works fine and newlines get interpreted, but not when using the variable holding the data coming from the MySQL database.
I've tried multiple ways as I've seen in a few posts on stack but nothing works.
let content = $('#myFrame').contents(); // Get the content of the iFrame
let description = response.description; // Ajax data : text
content.find('#description').val('foo\r\nbar'); // Works
content.find('#description').html('foo\r\nbar'); // Works
content.find('#description').text('foo\r\nbar'); // Works
content.find('#description').val(description); // Doesn't work
content.find('#description').val(String(description)); // Doesn't work
content.find('#description').html(description); // Doesn't work
content.find('#description').html(String(description)); // Doesn't work
content.find('#description').text(description); // Doesn't work
As per request I'm adding more information however I can't put all the HTML, there's a lot of stuff.
The table settings in MySQL for that particular row :
Name : description
Type : text
Collation : utf8_general_ci
A php script uses mysqli to retrieve the data :
$description = $row['description'];
$arrayAr['description'] = $description;
Then sends the data to jquery as a JSON :
echo json_encode($arrayAr);
The ajax part with jQuery :
function ajaxStuff(link, num) {
$.ajax ({
url: link,
type: "POST",
data : {id: num},
dataType: "json",
cache: false,
success: function(response) {
let content = $('#myFrame').contents();
// the rest of this code is in the snippet above
}
The HTML, the actual page and the page called with the iFrame both have UTF-8 charset :
<iframe src='../forms/form2.php'
id='myFrame'
frameborder='0'
width=''
height='800'
scrolling=''
></iframe>";
I have a problem with encoding/decoding some text.
On my page I have a picture:
<img id ="pcs" src="xxx.jpg" game="Hellblade: Senua’s Sacrifice ">
Then use ajax to send data to php server:
$('#pcs').click(function(){
if($(this).attr('src') == "xxx.jpg" ) {
$(this).fadeOut(100, function() {
var cookie = "dummy";
var game = $(this).attr('game');
$.ajax({
url: "http://example/notif_games.php",
type: "POST",
data: ({'cook_':'cookie', 'game_': game}),
});
The problem is the data are somehow encoded and recieved as: Hellblade: Senua’s Sacrifice
Instead, I need it to be recieved as: Hellblade: Senua’s Sacrifice
Even where I try
var game = $(this).attr('game');
alert(game);
it returns the 'encoded' value.
It's needed with the ’ part because I then put it in DB and compare it with another tables where the value is with the ’.
This is a browser issue and not a jquery one. Your browser is rendering the text already before jquery touches it. You need to decode it on the server side when you ajax it up.
With php you can do $data = htmlentities($data);
I'm trying to implement a custom suggestion engine using jquery.
I take the user input, call my codeigniter v2 php code in order to get matches from a pre-built synonyms table.
My javascript looks like this:
var user_str = $("#some-id").val();
$.ajax({
type: "POST",
url: "http://localhost/my-app/app/suggestions/",
data: {"doc": user_str},
processData: false
})
.done(function (data)
{
// Show suggestions...
});
My PHP code (controller) looks like this:
function suggestions()
{
$user_input = trim($_POST['doc']);
die("[$user_input]");
}
But the data is NOT posted to my PHP :( All I get as echo is an empty [] (with no 500 error or anything)
I've spent 2 days looking for an answer, what I've read in SO / on google didn't help. I was able to make this work using GET, but then again, this wouldn't work with unicode strings, so I figured I should use POST instead (only this won't work either :()
Anyone can tell me how to fix this? Also, this has to work with unicode strings, it's an important requirement in this project.
I'm using PHP 5.3.8
Try using the $.post method, then debug. Do it like this:
JS
var user_str = $("#some-id").val();
var url = "http://localhost/my-app/app/suggestions";
var postdata = {doc:user_str};
$.post(url, postdata, function(result){
console.log(result);
});
PHP
function suggestions(){
$user_input = $this->input->post('doc');
return "MESSAGE FROM CONTROLLER. USER INPUT: ".$user_input;
}
This should output the message to your console. Let me know if it works.
For those interested, this works for me, even with csrf_protection being set to true
var cct = $.cookie('csrf_the_cookie_whatever_name');
$.ajax({
url: the_url,
type: 'POST',
async: false,
dataType: 'json',
data: {'doc': user_str, 'csrf_test_your_name': cct}
})
.done(function(result) {
console.log(result);
});
I'm using a javascript shopping cart on a store, and I want to send an order confirmation on checkout. The problem is that the cart isn't saved in database of any kind, and is printed with javascript. How would I attach it to the email? I've included the shopping cart script on the page that sends the mail.
<table class="simpleCart_items"></table> would print the cart, but how would I attach the printed cart to email?
Hidden input or something?
UPDATE
My ajax call looks like this:
var data = $('#yhteystiedot').serialize();
data.cartContent = $('.simpleCart_items').html();
//alert (dataString);return false;
$.ajax({
type: "POST",
url: "order.php",
data: data,
dataType: "text",
error: function(){ alert("Jotakin meni pahasti pieleen! Yritä uudelleen?");
},
success: function() {
$(document).html("Tilaus lähti.");
}
});
You can make an ajax call to a php function that sends an email. The argument is the content generated by javascript.
You'll need to post the cart values to serverside PHP script and recreate the HTML for the cart in order to be able to send it through email. You can do direct form post or ajax post based on your need.
I asume your $.ajax() call looks something like this:
$('form').submit(function(){
var dataTrunk = $(this).serializeArray();
dataTrunk.push( { name: 'cartContent', value: $(your_table_selector).html()});
$.ajax({
url: 'mail.php', // your mail script
data: dataTrunk,
type: 'post'
});
return false;
});
In php you would trap $_POST['cartContent'] and render it in email and send it.
If you are sending email with html and plain text body, then it would probably be a good idea to strip html elements and replace them with chars that are compatible with plain text.
// edited: I've fixed the error
What I am trying to do only using XMLHttpRequest, is this:
The script downloads a web page, that has only one form in it.
The script inserts text into a field.
The script submits the form, while keeping all details about input tags.
I did the first part, but I have no idea how to finish with the next two steps.
Note: I do not have control over the page downloaded, and it is not well-formed XML/HTML.
Could someone explain to me how I can get this done?
This is for a Google Chrome extension, so I have all permissions needed.
EDIT: this is my current code:
$.ajax({ url: "http://www.mysite.com/my/testpage.aspx",
type: "POST",
data: {
html: http0.responseText.between("<body>", "</body>")
},
success: function(data) {
var dom = $(data),
form = dom.filter('form');
form.attr('onsubmit', 'document.getElementById("error").value = "I\'m done!"; document.getElementById("helpmebutton").disabled = false;');
form.attr('action', 'http://www.mysite.com/my/testpage.aspx');
$('#tempdiv').append(form);
form.find('input[name="ctl00$ctl00$cphSite$cphMySiteContent$linkLocation"]').val(document.getElementById("result").value);
form.submit();
}
});
I would really use jQuery to save yourself time and headaches.
Create a temporary div with id tempdiv and put everything in that div. Then, fill in appropriate elements and submit the form, like this:
$.ajax({ url: "http://...",
success: function(data) {
var dom = $(data),
form = dom.filter('form');
$('#tempdiv').append(form);
form.find('input:text').val(123);
// all input[type=text] get value 123
form.submit();
}
});