在一个页面中使用jQuery将变量发送到PHP页面


Sending variable to PHP page with jQuery within one page

在一页上,我有这段代码,它获取每个链接中的文本并提醒我文本中的内容。所以我知道变量工作正常:

<ul class="subpages-menu">
<li ><a class="clienttext" href="<?php bloginfo( 'template_url' ); ?>/incs/clients.php">All</a></li>
<li ><a class="clienttext" href="<?php bloginfo( 'template_url' ); ?>/incs/clients.php">Accountancy</a></li>
<li ><a class="clienttext" href="<?php bloginfo( 'template_url' ); ?>/incs/clients.php">Education</a></li>
<li ><a class="clienttext" href="<?php bloginfo( 'template_url' ); ?>/incs/clients.php">Financial</a></li>
<li ><a class="clienttext" href="<?php bloginfo( 'template_url' ); ?>/incs/clients.php">Hospitality</a></li>
<li ><a class="clienttext" href="<?php bloginfo( 'template_url' ); ?>/incs/clients.php">Insurance</a></li>
<li ><a class="clienttext" href="<?php bloginfo( 'template_url' ); ?>/incs/clients.php">Marketing / PR / Branding</a></li>
<li ><a class="clienttext" href="<?php bloginfo( 'template_url' ); ?>/incs/clients.php">Other</a></li>
<li ><a class="clienttext" href="<?php bloginfo( 'template_url' ); ?>/incs/clients.php">Recruitment</a></li>

<script type="text/javascript">jQuery(document).ready(function() {
jQuery('#clients-placeholder').html('<img class="preloader" src="<?php bloginfo( 'template_url' ); ?>/images/ajax-loader.gif" alt="Loading" title="Loading" />');
jQuery('#clients-placeholder').load('/all-clients .boxes-pages');
jQuery('.clienttext').click(function(e) {
    var clientText = jQuery(e.target).text();
    var url = jQuery(this).attr('href');
    alert (clientText);
    jQuery.post('<?php bloginfo( 'template_url' ); ?>/incs/clients.php', {clientText: clientText});
    jQuery('#clients-placeholder').html('<img class="preloader" src="<?php bloginfo( 'template_url' ); ?>/images/ajax-loader.gif" alt="Loading" title="Loading" />');
    jQuery('#clients-placeholder').load(url);
    e.preventDefault();
    return false;  
    });
});
</script>

在其他页面客户端上.php我有以下内容:

<ul class="boxes-pages">
    <?php
        $whichCat = $_post['clientText'];
        // The Query
        $the_query = new WP_Query( array('post_type' => 'ourclients', 'clientcategory' => $whichCat));
        // The Loop
        while ( $the_query->have_posts() ) : $the_query->the_post();
            $content = get_the_content();
            $content = strip_tags($content); ?>
            <li>
                <div class="box">
                    <a href="<?php the_permalink(); ?>">
                        <?php
                            if ( has_post_thumbnail() )
                                the_post_thumbnail( 'client-logos' );
                            else
                                echo '<img src="default-image.png" alt="Example Image" title="Example" />';
                        ?>
                    </a>
                    <h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
                    <p><?php $options = get_option( 'thetech_text' ); echo $options; ?></p>
                    <div class="tabs-readmore">
                        <a href="/the-cloud/the-technology/">Read More</a>
                        <img class="readmore-bt" src="<?php bloginfo( 'template_url' ); ?>/images/readmore-bt.png" alt="Read More" title="Read More" />
                    </div><!-- end hero-readmore-->
                </div><!-- end box -->
            </li>

        <?php endwhile;
        // Reset Post Data
        wp_reset_postdata();
    ?>
    </ul>

发生的情况是,当我单击链接时,它会重新加载我想要的div,但似乎没有发送变量 clientText。我收到警报说它已设置,但似乎没有将其传递给客户端.php

任何帮助将不胜感激。

谢谢

让我们重做 post 和 load 函数,我什至认为你不需要使用 load 函数。

jQuery.post('<?php bloginfo( 'template_url' ); ?>/incs/clients.php', {clientText: clientText}, function(data){
  //function(data) gets the data you want to return from the page.
  jQuery('#clients-placeholder').html('<img class="preloader" src="<?php bloginfo( 'template_url' ); ?>/images/ajax-loader.gif" alt="Loading" title="Loading" />');
  //maybe you want a setTimeout() here so your Loading image sticks around for awhile.
  $('#clients-placeholder').html(data);
});