I am trying to call php function through Ajax in same php file.
User enter voucher_id then call ajax that ajax call php function that function is in same file.
How to call that function.
function of PHP code:-
function voucherExist($voucherNo, $sCID, $type){
global $pncon;
$uRow = $pncon->query("SELECT * FROM transactions WHERE voucher_no = '{$voucherNo}' AND company_ID = '{$sCID}' AND type = '{$type}'");
return $uRow;
}
Ajax code:-
<script type="text/javascript">
$(document).ready(function (){
$('#voucher_no').on('change', function () {
var voucher_no = $('#voucher_no').val();
$.ajax({
url: "bankVoucherAddEdit.php",
type: "post",
data: "voucherNo"
});
});
});
</script>
You can get an idea from the below:
<? php
function yourFunction($data){
return $data;
}
if (isset($_POST['voucherNo'])) {
echo yourFunction($_POST['voucherNo']);
}
?>
<script>
$.ajax({
url: 'bankVoucherAddEdit.php',
type: 'post',
data: { "voucherNo": voucher_no },
success: function(response) { console.log(response); }
});
</script>
The thing is that you cannot directly call a PHP function from JS. You can achieve this by RESTful API. They are quite simpler and Standard.
In your case, Send any post value as a flag from JS to PHP, if that POST value exists then call PHP function
Related
function my_action_javascript($val1, $val2) {
?>
<script type="text/javascript" >
jQuery(document).ready(function($) {
var data = {
'email': '<?php echo $val1?>',
'password': '<?php echo $val2?>'
};
jQuery.post({
url: 'dummyurl',
method: "POST",
data: data,
success: function (data) {
console.log(data);
}
})
});
</script>
<?php
}
I got this function in my Wordpress Plugin. I parse in some data in the function and then i do a ajax request in Javascript. That all works just fine and i get the data array as response.
The Question is, how do i get the data from the Array in Javascript into my Variable in PHP so i can put the Data into my Wordpress Options?
Try this.
you need to parse the response.
function my_action_javascript($val1, $val2) {
?>
<script type="text/javascript" >
jQuery(document).ready(function($) {
var data = {
'email': '<?php echo $val1?>',
'password': '<?php echo $val2?>'
};
jQuery.post({
url: 'dummyurl',
method: "POST",
data: data,
success: function (data) {
console.log(data);
var obj = jQuery.parseJSON( data);
console.log(obj.somedata);
}
})
});
</script>
<?php
}
You need to have a PHP script that gets called by the AJAX request that will write the data into your options table. There's a specific way of doing this with Wordpress: https://codex.wordpress.org/AJAX_in_Plugins
In a nutshell, you need to add an action parameter to your POST data and then hook your PHP callback function to this parameter. In your callback, you can then use update_option().
This is my OpenCart code I don't know what the problem in this code is, but it cant return the value from php ajax page. I tried many ways it wont work so please help me for this problem .
Here is my ajax in OpenCart:
<script type="text/javascript">
$('.list-group-item').on('click', function () {
var href = $(this).attr("href");
var arr = href.split('path=');
var cat_id = arr[1];
//alert("hai");
$.ajax({
type: 'post',
url: 'index.php?route=ajax/ajaxcat/index',
data: format_data,
dataType: 'json',
success: function (json) {
if (json['success']) {
//$('#messages').html(json['success']);
alert(json['success']);
}
}
});
});
</script>
Here is my php code:
<?php
class ControllerAjaxAjaxcat extends Controller {
function index() {
$json['success'] = 'This is the returned results color_adjust.php';
$this->response->setOutput(json_encode($json));
}
}
?>
I don't know what is wrong with that code it cant return the value.
I am trying to pass a js array to a php function using jquery ajax.
I don't understand why the data doesn't update on the page.
this is my code:
js:
$('table tr').click(function(){
var id = $(this).attr('id');
storeId(id); // this function creates the id.
var selected_lp = localStorage.getItem('reportArray');
console.log(selected_lp);
var query = 'selected_lp=' + selected_lp;
$.ajax({
type: "POST",
url: "../inc/updatelponadvdash.php", //this is where i call the function.
data: { selected_lparr : selected_lp },
cache: false,
success: function(data) {
return true;
}
});
});
This is what i get in the console.log(selected_lp):
["97","96","51","48","45","50","33"]
This is the content of /inc/updatelponadvdash.php
<?php
require_once 'inc.php';
$lploop = new User();
$lploop->adv_lploops($_POST['selected_lparr']);
?>
this is the function
public function adv_lploops($selected_lp){
echo ' x '.$selected_lp. ' x ';
}
The final step is to call the function to the frontend. I require the /inc/updatelponadvdash.php to thefrontend.
At the console I see the array updated on click.
For some reason the $selected_lp var in not updating in the front end.
Maybe I am not calling the function as I should?
Aray vaues we need to use:
print_r($array);
How can I load data from my PHP response via ajax into a panel?
My PHP outputs correctly and I can see a table in the response, but I can;t get it to build the data on my webpage.
Here is my jquery/ajax so far. It passed the value to PHP correctly and PHP builds the table via its echo, but what am I missing for AJAX to display the table?
PHP:
<?php
foreach ($lines as $value) {
echo "<input name='data[]' value='$value'><br/>";
}
?>
JQUERY:
$(function () {
$('#rotator').change(function (e) {
var rotator = $("#rotator").val();
$.ajax({
type: "POST",
url: "tmp/JFM/National/national.php",
data: {
rotator: rotator
},
success: function (result) {
$('#panel').load(result);
}
})
return false;
});
});
The answer to this was two fold.
I was attempting to append to my main div, which apparently can't happen. I created a new empty div and was able to load the results there.
Beyond that, the comments to change .load(results) to .html(results) were needed.
The correct jquery code is below.
$(function () {
$('#rotator').change(function (e) {
var rotator = $("#rotator").val();
$.ajax({
type: "POST",
url: "tmp/JFM/National/national.php",
data: {
rotator: rotator
},
success: function (result) {
console.log(result);
$('#test').html(result);
}
})
return false;
});
});
move your function from:
$.ajax({...,success: function(){...}});
to
$.ajax({..}).done(function(){...});
if it doesn't work, try to add async:false into the ajax object...
$.ajax({...,async:false}).done(function(){...});
Hope it helps... =}
I am dynamically generating a web page via scriptA.php. Within this page,
I have a div element #WatchContainer that needs to be updated every 5 minutes. The content for #WatchContainer is created by scriptB.php. To accomplish this, I have used "include" to embed scriptB.php in scriptA.php. The variable $sum is defined in scriptA.php and used by scriptB.php to update the content in #WatchContainer.
On initial page load, $sum is correctly passed from scriptA.php to scriptB.php. However, when #WatchContainer is updated via AJAX request, $sum is no longer passed to scriptb.php. The jQuery function is as follows:
function updateWatch() {
$.ajax({
url:"scriptB.php",
success: function(data) {
$("#WatchContainer").html(data);
}
});
}
var WatchInterval = setInterval("updateWatch()", 300000);
i don't see you passing any variable to scriptB.php via ajax.
i thinnk you should pass your $sum variable to scriptB.php via ajax using POST OR GET method
POST
function updateWatch(sum) {
$.ajax({
type:"POST",
url:"scriptB.php",
data:"sum="+sum,
success: function(data) {
$("#WatchContainer").html(data);
}
});
}
GET
function updateWatch(sum) {
$.ajax({
type:"GET",
url:"scriptB.php?sum="+sum,
success: function(data) {
$("#WatchContainer").html(data);
}
});
}
Try this with 3 seconds interval time, and make sure you include the library and other related stuff.
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
$(document).ready(function() {
var WatchInterval = setInterval(function() {
$.ajax({
url:"scriptB.php",
success: function(data) {
console.log(data);
}
});
},3000);
});
<script>