Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

223
Views
La función hace eco de la variable correctamente en el archivo ajax pero en html no muestra nada

La variable Php $testing repite correctamente el archivo ajax, pero en html <?php echo $testing;?> no muestra nada.

html:

 <?php get_header(); ?> <script type="text/javascript"> var hrefValue; jQuery(document).ready(function($) { $('#bio-box').find('a').mouseover(function() { hrefValue = ($(this).attr('href')); $.ajax({ url: '/jakubtrz-portfolio/wp-admin/admin-ajax.php', data: { 'action': 'php_tutorial', 'php_test': hrefValue }, success: function(data){ console.log("sent") } }); }); }); </script> <?php echo $testing;?> <main id="primary" class="site-main"> <div class="container position-relative my-7"> <div class="row"> <div class="col-lg-6" id="bio-box"> <a href="http://localhost:8888/jakubtrz-portfolio/quod-si-ita-se-habeat-non-possit/">Example post link</a> <a href="http://localhost:8888/jakubtrz-portfolio/quid-ergo-aliud-intellege/">Example post link2</a> </div> <div class="col-lg-6"> <div class="featured-image"> <?php $post_id = url_to_postid($testing); echo get_the_post_thumbnail($post_id); ?> </div> </div> </div> </div> </main><!-- #main --> <?php get_footer();

funciones.php:

 function our_tutorial(){ if(isset($_REQUEST)){ $testing = $_REQUEST['php_test']; echo $testing; global $wpdb; $wpdb->insert( $wpdb->prefix.'lms_enroll', [ 'ID' => $testing ] ); } die(); } add_action('wp_ajax_php_tutorial', 'our_tutorial');
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

La solución es agregar $('#featured-image').html(data); al script, que pasa la salida de datos a html.

 var hrefValue; jQuery(document).ready(function($) { $('#bio-box').find('a').mouseover(function() { hrefValue = ($(this).attr('href')) //console.log(hrefValue) $.ajax({ url: frontendajax.ajaxurl, type: 'POST', data: { 'action': 'image', 'php_test': hrefValue }, success: function(data){ $('#featured-image').html(data); //console.log(data); } }); }); });

también movió líneas php de html a function.php:

 function fimg() { if ( isset( $_POST['php_test'] ) ) { $testing = sanitize_text_field( wp_unslash( $_POST['php_test'] ) ); $post_id = url_to_postid( $testing ); echo get_the_post_thumbnail( $post_id ); } die(); } add_action( 'wp_ajax_image', 'fimg' ); add_action( 'wp_ajax_nopriv_image', 'fimg' );

Entendí mal cómo funciona ajax. La respuesta más simple, en mi opinión, es este diagrama: ingrese la descripción de la imagen aquí

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!