Last active
December 26, 2015 19:59
-
-
Save chokonet/7205620 to your computer and use it in GitHub Desktop.
enviar variables por ajax a php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function ajax_favoritos(post_id, user_id) { | |
return $.post(ajax_url, { | |
post_id: post_id, | |
user_id: user_id, | |
action: 'marcar_favoritos' | |
}, 'json'); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//localisar la variable para enviar datos por ajax | |
wp_localize_script('functions', 'ajax_url', admin_url('admin-ajax.php')); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Crea campos en la tabla wp_post_favorito. | |
* | |
* @param $_POST['post_id'] | |
* @param $_POST['id_user'] | |
*/ | |
function marcar_favoritos(){ | |
$post_id = isset($_POST['post_id']) ? $_POST['post_id'] : false; | |
$favorito = isset($_POST['favorito']) ? $_POST['favorito'] : false; | |
$facebook_user = get_facebook_user(); | |
if ( ! $facebook_user AND ! $post_id ) return; | |
$existe = check_if_actividad_exists($facebook_user, $post_id); | |
if ($existe){ | |
$result = update_actividad($facebook_user, $post_id, $favorito); | |
} else { | |
$result = create_new_actividad($facebook_user, $post_id, $favorito); | |
} | |
echo json_encode($result); | |
exit; | |
} | |
add_action('wp_ajax_administrar_favoritos', 'administrar_favoritos'); | |
add_action('wp_ajax_nopriv_administrar_favoritos', 'administrar_favoritos'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment