I want to send a Hindi message from my custom php page. For this I have used a ajax request.
How to encode it in JavaScript before sending in ajax and decode it in php function?
EDIT
Here is my code :
...
var message = ''; // here I am getting content from form field dynamically
var ajaxBlockUrl = '/my.php?action=custom&mobile=999999999&message='+message;
Ajax.Request(ajaxBlockUrl,
{
parameters: {isAjax: 'true', form_key: FORM_KEY},
onSuccess: function(response)
{
//
}
});
...
Related
Okay so I need to parse two parts. Here is the code im trying to parse.
<input type="hidden" name="10528935" value="12-1-D33D19A3F048E845A9AA885220729B98" />
I would like it to parse and output like this once done.
10528935=12-1-D33D19A3F048E845A9AA885220729B98
Here is the site I'm trying to do this for
https://www.payqwiq.com/login?uid=582063bd-1973-42e4-8235-b28f5addf8bf
All I need is that data to be parsed and joined like above so I can continue with my program :)
Would appreciate some help if possible :)
I'm completely new in PHP.
Php is a back-end language. Since you are attempting to get data stored in a document object on the front-end, you should use a front-end language to parse the document object, then send the data to a php handler to do what you want with it on the back-end.
Using jQuery you can achieve the desired output with one line of code, then build an ajax call to send the output data to the desired php handler.
// when document is loaded
$(document).ready(function() {
// get data from document object to send to php handler
var combinedData = $('input').attr('name') + '=' + $('input').val();
// execute ajax method to send data to php handler
sendData(combinedData);
});
function sendData(data) {
// ajax method to send data to php handler
$.ajax({
url: 'pageToSendTo.php',
type: 'POST',
data: {
"data": JSON.stringify(data)
},
dataType: 'JSON',
success: function(data) {
console.log(data);
},
error: function(xhr) {
console.log('error: ' + xhr.responseText);
}
});
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="hidden" name="10528935" value="12-1-D33D19A3F048E845A9AA885220729B98" />
<!-- this is the "pageToSendTo.php" php handler. -->
<?php
if ($_POST['data']) {
//echo 'received data!';
$data = json_decode($_POST['data']);
// do stuff with data
print_r(json_encode($data));
}
?>
I'm trying to parse json data which is sent from a php page using jquery but what I get in result is : {
I don't know how to fix it,
I'm creating json data in php like below code :
$load_op_cm = $DBM -> RunQuery("SELECT * FROM at_ops_cmnts WHERE op_id='$op_id'",true,false);
$row = mysqli_fetch_assoc($load_op_cm);
$j_result = json_encode($row);
echo $j_result;
And I'm trying to parse this data using jquery which I'm using Jquery post method to send data and get response , And I'm trying to get this part of data which is useful for me : cm_1_1 : 1
not just this one but all of cm_ variables with their values.
when I show the response in html format it's like :
this is my jquery code :
$(function(){
var vop_id = localStorage.getItem('op_id');
$.post("Requests/OPS.php", //Required URL of the page on server
{ // Data Sending With Request To Server
Load_OP_CM : true,
op_id : vop_id
},
function(response){ // Required Callback Function
var result = response;
$.each(result, function (i,elem) {
$("#Response").text(elem);
});
});
Any suggestion how to get that data ?
Firstly you would need to parse your JSON string var result = JSON.parse(response);.
Then when you iterate through you would need to filter the results to only include the keys that contain 'cm_'
var filter = "cm_";
$.each(result, function (i,elem) {
if(i.indexOf(filter) !== -1) {
console.log(i, elem);
// Or whatever you need to do…
}
});
I have a page (page1)that calls another page (page2) via AJAX.
How do I include parameters to page2 response and parse them in success : function(data)?
is there any way to add parameters to http response object and then access those parameters?
Reason:
page2 generates some HTML. It could be good or bad html message. If it is a good message, I need to add parameters (hidden) and then add that good message to Success div. if message is an error, I need to add this message to 'Error div'.
I can do it bad way: In Page2 I can add hidden element; then in page1 I can create some temp hidden div and add response to that div. Then access hidden element in hidden div and get it's value. Then get message from that div and paste it to designated div. But this seems to be too unprofessional.
PAGE1:
function registeruser(){
jQuery(function($) {
$.ajax( {
url : "page2.php",
type : "POST",
data: {
registerFname : document.getElementById('registerFname').value,
registerLname : document.getElementById('registerLname').value,
registerLEmail : document.getElementById('registerEmail').value,
registerPassword : document.getElementById('registerPassword').value,
ts : (new Date().getTime())
},
success : function(data) {
//need to apply logic here based on the return parameters
//if (SOME_PARAMETER === 'success'){
// document.getElementById('registerPresentation').innerHTML = data;
//}
//else {
// document.getElementById('registerPresentationErrorDIV').innerHTML = data;
//}
//need to get rid of item below
document.getElementById('registerPresentation').innerHTML = data;
}
});
});
}
page2.php
$a=array('err'=>0,html=>'dfhsxdfhbcfvyhdgfr');
json_encode($a);
page1.php
success : function(data) {
if (data.err=='0'){...}else{...}
}
I am sending a JSON object to a PHP file, The PHP does some manipulation and returns a JSON string:
$('button#indexOpener').on('click', function() {
var aUsername = $('input#edUsername').val();
var aPassword = $('input#edPassword').val();
if (($.trim(aUsername) != '') && ($.trim(aPassword) != '')) {
var str = $("#form_login :input").serializeArray();
$.post("<?php echo URL; ?>ajax/checklogin", str, function(data) {
alert(data.edUsername);
});
}
else {
alert('Please insert a valid username and password');
alert("<?php echo URL; ?>/ajax");
}
});
the PHP echoes a JSON object:
echo json_encode($_POST);
but when I try to alert the data with jQuery:
function(data) {
alert(data.edUsername);
}
is displaying the message undefined. I am sure it is something stupid but I cannot see what I am doing wrong, can you help?
I see no dataType set for $.post(). jQuery will try to recognize returned content type, but you need to set correct headers. So, you need to add:
header("Content-Type: application/json");
before echo json_encode, or you should set dataType:"json" in JS code (fourth parameter of $.post()):
$.post("<?php echo URL; ?>ajax/checklogin", str, function(data) {
alert(data.edUsername);
}, "json");
This way, jQuery will know that the data returned is in JSON format and should be parsed. Without it, jQuery will check the Content-Type header and apply parser according to it. Suppose if no custom content type headers set, it will return return data as HTML. Actually, that is a usual string.
If I just alert(data) is returning {"edUsername":"qqq"
"edPassword":"qqq"} but if I alert alert(data.edUsername); I get
"undefined"?
JSON is a regular string which should be parsed on client side. jQuery detects your response as plain text or HTML and does not parse JSON to Javascript object. In case of data being an object, you would get [object Object] in alert window.
I think this should be this way using $.getJSON():
var str = $("#form_login").serialize();
$.getJSON("<?php echo URL; ?>ajax/checklogin", {data:str}, function(data){
alert(data.edUsername);
});
jquery
$("#go").click(function() {
$.post("i/go.php", $("#form").serialize());
var code= '';
$("section").empty().html(code);
});
go.php accepts POST data, then displays some text in <section>.
How to get <section> after sending POST data and display it on this page?
You are not handling the response at all, just sending the request:
$.post("i/go.php", $("#form").serialize(),
function(data) {
// do something with data
$("#some_div").html(data);
}
);