The ajax request is working but the problem I'm having is how to encrypt the data with my PHP function before the request is made?
Here is the JS I'm trying
$("#sensitive_data").blur(function() {
alert($(this).val());
$.ajax({
url: 'db.php?check=<?php echo $object->encrypting_data('+$(this).val()+') ?>',
type: 'POST',
error : function (){ document.title='error'; },
success: function (data) {
alert(data);
}
});
});
It works if I pass a static value in the php function
If I understand right, you're trying to use a PHP function to encrypt client-side data before it's sent to the server.
That's impossible, because you can't call a PHP function on the client-side. I'm also not sure what you want to accomplish.
Related
my ajax code
$.ajax({
url : '<?php echo base_url('mobile_plan/getStates')?>',
type: 'POST',
data:{operator:operator},
dataType:'json',
success: function(response)
{
alert(response);
}
});
I am getting response in array for from controller now this ajax call is done from the same page in which I want response back in PHP array for further operating could you please guide me how it is possible. I am using codeigniter 3.1.
If you want to send this response again to the controller in php you should create another ajax call with POST method and pass the response array as a data parameter.
When using ajax it's recommended to pass data in JSON format.
In PHP you should do
echo json_endoce($data);
And in Javascript, in your success function
response = JSON.parse(response);
Hi I'm using jquery ajax function with php. Here is the my problem;
Firstly I'm using datatype:"html" my problem is php variables not returning.
js
$.ajax({
type: "POST",
dataType: "html",
url: ajaxurl,
data:dataajax,
success:function(data) {
var $data = $(data);
$(".list").append($data);
},
error : function(jqXHR, textStatus, errorThrown){
}});
php
echo "<div>".$_POST['value']."</div>";
if I use like this it's working but when I remove the html tags ajax return nothing.
broken php
echo myfunction($_POST['value']);
echo $_POST['value'];
How can I fix this or can I use return $output with jquery ajax?
The problem is with your success callback function.
data is a html string, and as such you don't need to wrap it in jquery.
Use this.
$.ajax({
type: "POST",
dataType: "html",
url: ajaxurl,
data:dataajax,
success:function(data) {
$(".list").append(data);
},
error : function(jqXHR, textStatus, errorThrown){
}});
Ajax uses JSON data for your request and response. what you are going to want to do is return a JSON variable so that the javascript response function can access elements properly.
Luckily php is designed to do just that thing.
$ajaxData = $_POST['value'];
echo json_encode(array('response' => $ajaxData));
what are you actually trying to do with this data? if all you need to do is wrap some html in a div you can use functions like wrap() in javascript and you wont actually have to send an ajax to the server.
Either way you should really consider your data needs and try to send a json object in the ajax request so that there are actual variables to send. html markup is probably not going to be useful for the php code.
I want to save returned data from success function of ajax to a PHP variable.
Instead of saving data to '#showThis' I want to save it in a PHP variable. This is my code.
function getId(id) {
$.ajax({
url: '<?php echo base_url() ?>index.php/feeController/test',
data: {studentId: id},
type: 'post',
success: function (data) {
$("#showThis").html(data);
}
});
}
This is not possible. Javascript is not able to write to a PHP variable.
You could send another ajax call to a php file, and simply save those variables sent by javascript in the PHP file itself. However, you cannot write directly from javascript to php without ajax.
Is it possible to call a php function on jquery ajax call.When i tried to do this i get function not defined error on php function
$.ajax({
type:"POST",
url:"x.php?z=" + id,
cache:false,
success: function(data)
{
<?php xcz(); ?>
}
});
$.ajax({
type:"POST",
url:"x.php?z=" + id,
cache:false,
success: function(data)
{
anotherFunction();
}
});
function anotherFunction(){
$.ajax({
type:"POST",
url:"anotherFile.php",
cache:false,
success: function(data)
{
//do something else;
}
});
}
That's not possible. PHP and javascript run on different computers. PHP runs on the server, and javascript runs in the browser. At the time the javascript is being executed, that server has already executed the PHP code, and has sent that the the browser.
The only way you can achieve such a thing is making an additional Ajax request.
Not possible. Because jQuery (javascript) runs on client side, php runs on server side.
When page loaded on browser, php's job is finished.
You can call just javascript's function like this approach.
Is it possible to refer an AJAX POST to a specific function within a PHP file?
$.ajax({
type: "POST",
url: "functions.php", //somehow specify function?
data: "some data...",
success: function(){
alert('Success!');
}
});
Or is there a way to have functions.php receive data and know what to do with it? If not, are there any other suggestions for getting data over to mySQL (using PHP/jQuery)? Thanks!
The data sent to the php file using POST can be accessed in php using:
$datasent = $_POST['name'];
Given that you sent data as:
$.ajax({
type: "POST",
url: "functions.php", //somehow specify function?
data: {name:"Jesse"}, //data goes here
success: function(){
alert('Success!');
}
});
Not directly. You'd need to post certain data, and have PHP check the POST variables to choose the correct function.
Perhaps have a look at some tutorials (unfotunately the jQuery links for php tutorials are broken).
Is it possible to refer an AJAX POST to a specific function within a PHP file?
No. jQuery doesn't know what PHP is, even less what a PHP function is. jQuery talks to server side urls. Whether those urls are static html files, PHP scripts, Java servlets, Python I don't know what, CGI scripts, is not really important.
So you could use the data setting to pass parameters to this server side url which based on the values of those parameters could invoke one or another function.
If you want to call a specific function, change ur jquery:
$.ajax({
type: "POST",
url: "functions.php", //somehow specify function?
data: {function:"doSomething",name:"Jesse"}, //data goes here
success: function(){
alert('Success!');
}
});
In your php add:
call_user_func($_POST['function']); // This will call what ever function name is passed as parameter
function doSomething(){
echo $_POST['name'];
}