Get content with ampersand through javascript - javascript

I have an editable table cell
<td class="center" contenteditable="true" onblur="saveToDB(this, 'docTitle', '<?= $row->id; ?>')"><?= $row->docTitle ?></td>
and here the function
function saveToDB(editableObj,column,id) {
$(editableObj).css("background","#B2DDF2 url(../img/AjaxLoader.gif) no-repeat right");
$.ajax({
url: "<?php echo base_url();?>documents/inline_update",
type: "POST",
data:'column='+column+'&editval='+editableObj.textContent+'&id='+id,
success: function(data){
$(editableObj).css("background","#FDFDFD");
}
});
}
Whenever user enter data with an ampersand (Eg. Forms & Format), only Forms will be inserted into database.
I have tried innerText but still get the same result.
What are the options to achieve this without escape / encode ampersand?

You can just provide an object as data and jQuery will take care of proper serialization and escaping for you (it will use utility $.param internally which will apply escaping to data values):
$.ajax({
url: "/",
type: "POST",
data: {
column: column,
editval: editableObj.textContent,
id: id,
},
success: function (data) { },
});

Related

How to send multiple data fields via Ajax to php page?

i have a problem, I want to send 2 posts to my author php page by using ajax
thats my code :
$.ajax({
type: "POST",
url: "includes/get_competitions.php",
data:'sport=<?php echo $_GET['sports']; ?>',
success: function(data){
$("#competitions-list").html(data);
}
and this what i want to do :
$.ajax({
type: "POST",
url: "includes/get_competitions.php",
data:'sport1=<?php echo $_GET['sports1']; ?>, sport2=<?php echo $_GET['sports2']; ?>',
success: function(data){
$("#competitions-list").html(data);
}
but it didn't work
Multiple parameters in a data string are separated by &, not , (it's just like the syntax of parameters in a URL).
However, if you're using jQuery it's best to use an object. $.ajax will automatically convert it to the properly encoded query string. And you can use json_encode() to convert a PHP value to the corresponding JavaScript literal.
$.ajax({
type: "POST",
url: "includes/get_competitions.php",
data: {
sport1: <?php echo json_encode($_GET['sports1']); ?>,
sport2: <?php echo json_encode($_GET['sports2']); ?>
},
success: function(data){
$("#competitions-list").html(data);
}
You could also make a single sports array, rather than numbering the parameters.
$.ajax({
type: "POST",
url: "includes/get_competitions.php",
data: {
sports: [<?php echo json_encode($_GET['sports1']); ?>,
<?php echo json_encode($_GET['sports2']); ?>]
},
success: function(data){
$("#competitions-list").html(data);
}
Then in get_competitions.php, $_POST['sports'] will be an array.
You can try this
data: $('#myForm').serialize() <---- your form name
This will send all the form data

How to intercept POST data with Javascript

I am using this javascript function to send data to my php script (via POST) I am then using this information to retrieve data from my database and I would like to reuse the information retrieved in my JavaScript code.
Here is my code :
$(document).on("click", "#validerChoixDeCours", function() {
jQuery.ajax({
type: "POST",
url: 'myFunctions.php',
dataType: 'json',
data: {
functionname: 'proposePA',
arguments: clickedButtons
},
success: function() {
// HERE I would like to inctercept the data that my php script put in myFunctions.php produces and use it to generate content;
}
});
});
So basically, when clicking on the button #validerChoixDeCours, my code sends some data to myFunctions.php which generates a response stored in a php variable and I want to use that response in my JS code.
Thank you in advance for your help :)
Its actually quite easy!
$(document).on("click", "#validerChoixDeCours", function() {
jQuery.ajax({
type: "POST",
url: 'myFunctions.php',
dataType: 'json',
data: {
functionname: 'proposePA',
arguments: clickedButtons
},
success: function(e) {
e contains the response from the php script
}
});
});
The first parameter of success contains the response from the request. so, in this case, the 'e' variable contains the output which you can use.
Add data variable
$(document).on("click", "#validerChoixDeCours", function() {
jQuery.ajax({
type: "POST",
url: 'myFunctions.php',
dataType: 'json',
data: {
functionname: 'proposePA',
arguments: clickedButtons
},
success: function(data) {
alert(data);
}
});
});
make sure your server side code look like this
<?php
$output = "Any String or Array";
echo json_encode($output);
?>

Illegal invocation for sending data with POST with AJAX

I am trying to get the excelPrintarea and post that data to my Employees controller. But when I add the data and datatype attribute it tells me it's an illegal invocation. I don't understand why it is saying that.
Also when I leave the data, datetype and POST out the downloadExcel from my employees controller does fire.
function printExcel() {
var excelPrintarea = document.getElementById('printarea');
$.ajax({
url: "<?php echo $this->webroot; ?>employees/downloadExcel",
data: {myPrintArea: excelPrintarea},
dateType: 'json',
type: 'POST',
cache: false,
success: function(excelPrintarea) {
alert("printing excel worked")
}
});
}

Select function in ajax

My php file
$option1 = filter_input(INPUT_POST, 'key', FILTER_SANITIZE_SPECIAL_CHARS);
if(isset($option1) && !empty(option1)) {
$sql = "SELECT * FROM fantasy WHERE os_id='$option1'";
}
My ajax
function filter(){
var str = $("#advanced-search1").val();
$.ajax({
dataType: 'html',
url: "filter.php",
type: "POST",
data: {"key": str},
success: function(response){
$('#result').html(response);
}
});
}
From the above, stated that my select function id is #advanced-search1 whereas my submit button would be onclick="filter()". But it still doesn't work for me to execute the sql command in my php file. From my url browser, it stated the required select value, but it doesn't show anything in my #result div
Add event.preventDefault(); at the beginning of filter function :
function filter(event){
event.preventDefault();
var str = $("#advanced-search1").val();
$.ajax({
dataType: 'html',
url: "filter.php",
type: "POST",
data: {"key": str},
success: function(response){
$('#result').html(response);
}
});
}
However, you need to change the call also to pass the event argument :
<button onclick="filter(event);" >..</button>
Instead of :
<button onclick="filter();" >..</button>
If does not work , you have to report your PHP log as well as your HTML skeleton.

how to send $(this).atta('id') using cake php js helper

HIi Folks i am having one issue please help me i want to send some data to my controller using ajax call i have written all code but for data field
<?php echo $this->Js->get('.menu')->event('click',$this->Js->request(array('controller' => 'restaurants', 'action' => 'getItem'),array('async' => true,'method'=>'POST','update' => '#HFNames','data'=>'$(this).attr(id)')),false); ?>
when ajax call hits it take "$(this).attr(id)" as a params but i need the value of cureent click
js helper genrate this
if we remove that double quote from this genrated script then its working
data: "$(this).attr(id)",
why this get quoted
<script type="text/javascript">
$(".menu").bind("click", function (event) {
$.ajax({
async: true,
data: "$(this).attr(id)",
dataType: "html",
success: function (data, textStatus) {
$("#HFNames").html(data);
},
type: "POST",
url: "\/foodkingkong\/restaurants\/getItem"
});
return false;
});
try this instead.
echo $this->Js->buffer('$(".menu").bind("click", function (event) {
$.ajax({
async: true,
data: $(this).attr(id),
dataType: "html",
success: function (data, textStatus) {
$("#HFNames").html(data);
},
type: "POST",
url: "/foodkingkong/restaurants/getItem"
});
return false;
});'
);
Hi Himanshu we had planned to do the same directly using jquery but wanted to do the same via cakephp bind function only.
What will be the difference if we pass the js in buffer.

Categories

Resources