I'm trying to call a function using ajax in a wordpress admin submenu. Here is my code.
jQuery('#deleteProj').submit(ajaxSubmit);
function ajaxSubmit(){
var tobeDeleted = jQuery(this).serialize();
alert(tobeDeleted);
jQuery.ajax({
type:"POST",
url: ajaxurl,
data: tobeDeleted,
success:function(){ alert(tobeDeleted);},
error:function(errorThrown){alert(errorThrown);}
});
return false;
}
However, the code opens a new page querying the admin.php file looks like this:
wp/wp-admin/admin.php?q=id&action=deleteproj
I'm really at a loss for why this is happening. I'm calling the function from inside my plugin's admin menu
Update
The issue turned out to be that I had to define the php functions on the main file of my plugin. I also made sure to absolutely define the location of admin-ajax.php this allowed the jQuery to properly execute.
Try this:
jQuery('#deleteProj').submit(function() {
var ajaxurl = "<?php echo admin_url('admin-ajax.php'); ?>";
var formData= jQuery(this).serialize();
jQuery.ajax({
url:ajaxurl,
action:'function_name',
data:formData,
type: "POST",
success: function(data) {
// return
}
});
return false;
});
Regards:)
It seems like you are redirecting instead of ajaxing ,try changing you submit event to a click event
jQuery('#deleteProj').find('[type="submit"]').click(function(e){
e.preventDefault();
var tobeDeleted = jQuery(this).serialize();
alert(tobeDeleted);
jQuery.ajax({
type:"POST",
url: ajaxurl,
data: tobeDeleted,
success:function(){ alert(tobeDeleted);},
error:function(errorThrown){alert(errorThrown);}
});
});
If you have written the code in side your php file, then the answer from Gulshan is correct, but if you have written this code in a different .js file you can not access the ajax url into it. Then you need to localize the script to add the ajax url.
Please follow the example code :
wp_enqueue_script( 'mainscript', get_template_directory_uri() . '/js/main.js', array(), 'v1', true );
wp_localize_script( 'mainscript', 'sitesettings', array('ajaxurl' => admin_url( 'admin-ajax.php' )));
for both the function you need place same handle [ 'mainscript' ], now in your main.js file you need to write the following code :
jQuery('#deleteProj').submit(function() {
var ajaxurl = sitesettings.ajaxurl; // here you are accessing the admin-ajax.php
var formData= jQuery(this).serialize();
jQuery.ajax({
url:ajaxurl,
action:'function_name',
data:formData,
type: "POST",
success: function(data) {
// return
} });
return false;
});
Hope this work for you
Related
The AJAX request goes through correctly, I checked with chrome's developer tools, there is a request on quiz.php page, but when I check for $_POST['risultato'] it looks doesn't exist. I noticed though that in Chrome's dev tools there's 2 quiz.php elements (one xhr the other document)
I tried changing the code in several ways, but it seems like it doesn't work
<?php
if(isset($_POST['risultato'])){
print($_POST['risultato']);
}
?>
<script>
function inviaRisultati(ris){
$.ajax({
url: "quiz.php",
type: "POST",
cache: false,
data: {risultato: ris},
success: function(){
alert("INVIATI");
}
})
}
The program is expected to return the result on quiz.php page (the same page where ajax request is fired), and it's supposed to print it somewhere
EDIT: I fixed it
<?php
file_get_contents('php://input');
if(isset($_POST['risultato'])){
print($_POST['risultato']);
}
?>
function inviaRisultati(param) {
return $.ajax({
url:"quiz.php",
method:"POST",
data:{action: "SLC", risultato :param},
dataType:"text"
});
}
inviaRisultati(1).done(function(response){``
document.open();
document.write(response);
});
In Ajax, the data attribute is in JSON format.
your data attribute will be like this
data: {risultato: ris}
Hi you can do it this way:
your php script:
if (isset($_POST["action"])) {
$action = $_POST["action"];
switch ($action) {
case 'SLC':
if (isset($_POST["risultato"])) {
$response = $_POST["risultato"];
echo $response;
}
}
break;
}
Where action is a command you want to do SLC, UPD, DEL etc and risultato is a parameter
then in your ajax:
var param = $('#yourinputid').val();
function getInfo(param) {
return $.ajax({
url:"quiz.php",
method:"POST",
data:{action: "SLC", risultato :param},
dataType:"text"
});
}
call it like this:
getInfo(param).done(function(response){
alert(response);
//do something with your response here
})
Hope it helps
HTML CODE
<script>
jQuery(document).ready(function(){
ris = 'abcd';
jQuery.ajax({
url: "quiz.php",
type: "POST",
cache: false,
data: {risultato: ris},
success: function(){
}
})
});
</script>
PHP CODE
<?php
file_get_contents('php://input');
print($_POST['risultato']);
Console Output
From everything I've read on the internet, the way of returning HTML, JSON, etc., from a PHP script is simply by echoing it. I can't get it to work, however.
My JS is
jQuery('#new-member').submit(
function()
{
var formUrl = jQuery(this).attr('action');
var formMethod = jQuery(this).attr('method');
var postData = jQuery(this).serializeArray();
console.log(postData); // test for now
jQuery.ajax(
{
url: formUrl,
type: formMethod,
dataType: 'json',
data: postData,
success: function(retmsg)
{
alert(retmsg); // test for now
},
error: function()
{
alert("error"); // test for now
}
}
);
return false;
}
);
and I've verified that it is correctly calling my PHP script, which as a test is simply
<?php
echo "Yo, dawg.";
?>
but all that does is open "Yo, dawg." in a new page. The expected behavior is for it to alert that message on the same page I was on. What am I missing here?
i want to get a var from a php script and use it in a function .. so if i call the var simply with $.get (and document.write) i got an result but how can i integrate this into a function ?
$.get( 'http://www.domain.de/content/entwicklung/verdienst.php', function(verdienst_php) {
//document.write(verdienst_php);
});
function sendview () {
var datastring = {uid : uid_clear, verdienst : verdienst_php};
$.ajax({
type: 'POST',
url: 'http://www.domain.de/content/entwicklung/view_succeed.php',
data: datastring,
});
}
only put the function into the $.get part didnt work
if i didnt use $.get and write in datastring like
verdienst: 1000
it works
any suggestions ?
kind regards Dave
Assuming you are returning exactly what you want from the server, store it in a variable and reference the variable in the other Ajax call.
var verdienst_php;
$.get( 'http://www.domain.de/content/entwicklung/verdienst.php',
function(response) {
verdienst_php = response;
});
function sendview () {
var datastring = {uid : uid_clear, verdienst : verdienst_php};
$.ajax({
type: 'POST',
url: 'http://www.domain.de/content/entwicklung/view_succeed.php',
data: datastring,
});
}
If you want the GET call to happen when the click happens, than you just need to put the post code inside of the GET success callback.
You have to make an extra ajax call to get php variable, you can do it this way :-
function sendview () {
var datastring = {uid : uid_clear, verdienst : getPHPVar('verdienst_php')};
$.ajax({
type: 'POST',
url: 'http://www.domain.de/content/entwicklung/view_succeed.php',
data: datastring,
});
}
function getPHPVar(varname){
var returnValue = null;
$.ajax({
url: 'yourphpurl.php',
async: false,
type: 'post',
data:{
task:'getvar',
varname: varname
},
success: function(response){
returnValue = response;
}
});
return returnValue;
}
and in PHP it will look like:
<?php
if($_POST['task'] == 'getvar'){
echo $$_POST['varname'];
}
Also, I think it's actually not needed cause you are getting php variable using ajax, and again using it in another ajax call. so Why don't you just do it in php ?
Hi guys this script was working fine when I add a console.log but as soon as I replaced the console.log with the $.ajax() function it wont give me the result back from the php file the ajax function I used was working from my other projects but I cant seem to find out why it wont work on this snippet
Here is my js code :
$(document).ready(function(){
$("#qs").find(".chs").each(function(i,obj){
$(this).addClass("chs"+i);
$(".chs"+i).on("click",function(){
var s = $(this).data("lvs"),carrier= {"vars":s};
$.ajax({
url: aScript.php,
type: "POST",
data: carrier,
dataType: "json"
success: function(data) {
console.log(data) }
});
});
});
});
my php file looks like this
<?php
$json = $_POST['carrier'];
$data = json_decode($json);
$d = $data->vars;
echo $d;
?>
<input type="hidden" id="ss" value="<?=$d?>" />
can someone review this file for me cause I cant seem to find whats wrong please help me find out whats wrong with this script
You should wrap the filename inside quotes, as it is a string variable
$.ajax({
url: 'aScript.php',
type: "POST",
data: carrier,
dataType: "json",
success: function(data) {
console.log(data) }
});
});
There are some issues with your code
in this line url: aScript.php, the url string is not quoted, it should be url: 'aScript.php',
you set dataType: "json" but aScript.php returns html instead of json, remove that line
the data you're passing is not json, it will be serialized into key=value pairs and you'll be able to access it via $d = $_POST['vars'];
i have almost got to load the sidebar in wordpress with Ajax, using this example (https://wordpress.stackexchange.com/questions/61830/use-ajax-request-to-load-sidebar)
but instead of loading my new sidebar-ajax.php, it always loads the index.php instead in the div called "sidebar".
I have the Feeling, that my function (get_template_part('sidebar-ajax');
) is not executed properly, but cannot find the error
in my functions.php i have inserted:
add_action('wp_enqueue_scripts', 'theme_enqueue_scripts');
function theme_enqueue_scripts() {
wp_enqueue_script('jquery');
/* load your js file in footer */
wp_enqueue_script('theme-script', get_stylesheet_directory_uri() . '/your-js-file.js',
false, false, true);
}
add_action('wp_ajax_get_ajax_sidebar', 'check_ajax');
add_action('wp_ajax_nopriv_get_ajax_sidebar', 'check_ajax');
function check_ajax() {
?>
get_template_part('sidebar-ajax');
<?php
} ?>
my js. file:
jQuery.ajax({
type: 'POST',
url: location.href,
data: { get_ajax_sidebar: 1 },
success: function(data){
jQuery('#sidebar').html(data);
}
});
and in my index.php i have added:
<div id="sidebar"></div>
any help is really appreciated. thanks!
get_template_part() is outside of php tags... why? I like to use get_sidebar(). Try this:
add_action( 'wp_ajax_get_ajax_sidebar', 'check_ajax' );
add_action( 'wp_ajax_nopriv_get_ajax_sidebar', 'check_ajax' );
function check_ajax() {
get_sidebar( 'ajax' );
}
A bit late to the party but, the reason this isn't working is because you need the correct url to call the ajax function. On the back end of wordpress this is done automatically and defined in a javascript variable called ajaxurl. On the front end you have to define this yourself, it's quite straight-forward. In your functions.php:
add_action('wp_head','yourfunctionname_ajaxurl');
function yourfunctionname_ajaxurl() {
?>
<script type="text/javascript">
var ajaxurl = '<?php echo admin_url('admin-ajax.php'); ?>';
</script>
<?php
}
Now you can change the javascript code to:
jQuery.ajax({
type: 'POST',
url: ajaxurl,
data: { action:'get_ajax_sidebar' },
success: function(data){
jQuery('#sidebar').html(data);
}
});
More information can be found on the codex here: wp_ajax