2014-02-12 94 views
0

我有這個JS/jQuery代碼傳遞PHP變量使用AJAX另一個變量?

window.onload = function(){ 

    var request = $.ajax({ 
    url: "../wp-content/plugins/woocommerce/admin/test.php", 
    type: "GET",    
    dataType: "html" 
    //data: {$lastid}, 
    }); 

    request.done(function(msg) { 
     $(".recent-orders").append(msg); 
    }); 

    request.fail(function(jqXHR, textStatus) { 
     alert("Request failed: " + textStatus); 
    }); 
EDIT: This is the method where I get the $lastid. 


<?php 
function woocommerce_dashboard_recent_orders() { 

    $args = array(
     'numberposts'  => 8, 
     'orderby'   => 'post_date', 
     'order'   => 'ASC', 
     'post_type'  => 'shop_order', 
     'post_status'  => 'publish' 
    ); 
    $orders = get_posts($args); 
    if ($orders) : 
    echo '<ul class="recent-orders">'; 
     foreach ($orders as $order) : 

      $this_order = new WC_Order($order->ID); 

      echo ' 
      <li> 
       <span id = "order-$order->ID" class="order-status '.sanitize_title($this_order->status).'">'.ucwords(__($this_order->status, 'woocommerce')).'</span> <a href="'.admin_url('post.php?post='.$order->ID).'&action=edit">' . get_the_time(__('l jS \of F Y h:i:s A', 'woocommerce'), $order->ID) . '</a><br /> 
       <small>'.sizeof($this_order->get_items()).' '._n('item', 'items', sizeof($this_order->get_items()), 'woocommerce').' <span class="order-cost">'.__('Total:', 'woocommerce') . ' ' . woocommerce_price($this_order->order_total).'</span></small> 
      </li>'; 

     endforeach; 
     $lastid = $order->ID; 
     echo '</ul>'; 
    else: 
     echo '<p>' . __('There are no product orders yet.', 'woocommerce') . '</p>'; 
    endif; 
} 
?> 

這調用名爲test.php的PHP文件。

test.php的

<?php 
//woocommerce_dashboard_recent_orders_realtime(); 

/** 
* Init the dashboard widgets. 
* 
* @access public 
* @return void 
*/ 

function filter_where($where = '') { 
    $oid = 2100; 
    $where = " AND ID > $oid"; 

    return $where; 

} 


add_filter('posts_where', 'filter_where'); 

    $args = array(
     'numberposts'  => 8, 
     'orderby'   => 'post_date', 
     'order'   => 'DESC', 
     'post_type'  => 'shop_order', 
     'post_status'  => 'publish', 
     'suppress_filters' => FALSE 
    ); 

    $orders = get_posts($args); 

    if ($orders) : 
     foreach ($orders as $order) : 
      //echo " $order->ID"; 
      $this_order = new WC_Order($order->ID); 
      echo ' 
      <li> 
       <span id = "order-$order->ID" class="order-status '.sanitize_title($this_order->status).'">'.ucwords(__($this_order->status, 'woocommerce')).'</span> <a href="'.admin_url('post.php?post='.$order->ID).'&action=edit">' . get_the_time(__('l jS \of F Y h:i:s A', 'woocommerce'), $order->ID) . '</a><br /> 
       <small>'.sizeof($this_order->get_items()).' '._n('item', 'items', sizeof($this_order->get_items()), 'woocommerce').' <span class="order-cost">'.__('Total:', 'woocommerce') . ' ' . woocommerce_price($this_order->order_total).'</span></small> 
      </li>'; 

      //echo (gettype($time3)); 
     endforeach; 

    endif; 
//} 
?> 

我想要做的就是從JavaScript來的test.php的文件傳遞$ lastid並接受它作爲類似$也lastid。

我知道我應該張貼,但我使用它遇到問題。任何人都可以引導我採用正確的方法嗎?

我的代碼現在

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script> 
<script type="text/javascript" > 
window.onload = function(){ 
    //setInterval(function(){ 
     //var lastid = '<?php echo $lastid; ?>'; 
     //alert(lastid); 
     var request = $.ajax({ 
     url: "../wp-content/plugins/woocommerce/admin/test.php", 
     type: "POST",  
     dataType: "html", 
     data: { lastid : '<?php echo $lastid; ?>'}, 
     }); 

     request.done(function(msg) { 
      $(".recent-orders").append(msg); 
     }); 

     request.fail(function(jqXHR, textStatus) { 
      alert("Request failed: " + textStatus); 
     }); 
     //addElement(); 

    //},1000); 
setInterval(function(){ 


},1000); 

} 

</script> 

<?php 
function woocommerce_dashboard_recent_orders() { 

    $args = array(
     'numberposts'  => 8, 
     'orderby'   => 'post_date', 
     'order'   => 'ASC', 
     'post_type'  => 'shop_order', 
     'post_status'  => 'publish' 
    ); 
    $orders = get_posts($args); 
    if ($orders) : 
    echo '<ul class="recent-orders">'; 
     foreach ($orders as $order) : 

      $this_order = new WC_Order($order->ID); 

      echo ' 
      <li> 
       <span id = "order-$order->ID" class="order-status '.sanitize_title($this_order->status).'">'.ucwords(__($this_order->status, 'woocommerce')).'</span> <a href="'.admin_url('post.php?post='.$order->ID).'&action=edit">' . get_the_time(__('l jS \of F Y h:i:s A', 'woocommerce'), $order->ID) . '</a><br /> 
       <small>'.sizeof($this_order->get_items()).' '._n('item', 'items', sizeof($this_order->get_items()), 'woocommerce').' <span class="order-cost">'.__('Total:', 'woocommerce') . ' ' . woocommerce_price($this_order->order_total).'</span></small> 
      </li>'; 

     endforeach; 
     $lastid = $order->ID; 
     echo '</ul>'; 
    else: 
     echo '<p>' . __('There are no product orders yet.', 'woocommerce') . '</p>'; 
    endif; 
} 
?> 

<?php 
function filter_where($where = '') { 
    $oid = 2110; 
    $where = " AND ID > $oid"; 

    return $where; 

} 

$lastid = $_GET['lastid']; 

add_filter('posts_where', 'filter_where'); 

    $args = array(
     'numberposts'  => 8, 
     'orderby'   => 'post_date', 
     'order'   => 'DESC', 
     'post_type'  => 'shop_order', 
     'post_status'  => 'publish', 
     'suppress_filters' => FALSE 
    ); 


    $orders = get_posts($args); 
    echo "LAST ID: $lastid"; 
    if ($orders) : 
     foreach ($orders as $order) : 
      $this_order = new WC_Order($order->ID); 


      echo ' 
      <li> 
       <span id = "order-$order->ID" class="order-status '.sanitize_title($this_order->status).'">'.ucwords(__($this_order->status, 'woocommerce')).'</span> <a href="'.admin_url('post.php?post='.$order->ID).'&action=edit">' . get_the_time(__('l jS \of F Y h:i:s A', 'woocommerce'), $order->ID) . '</a><br /> 
       <small>'.sizeof($this_order->get_items()).' '._n('item', 'items', sizeof($this_order->get_items()), 'woocommerce').' <span class="order-cost">'.__('Total:', 'woocommerce') . ' ' . woocommerce_price($this_order->order_total).'</span></small> 
      </li>'; 

     endforeach; 

    endif; 
    remove_filter('posts_where', 'filter_where'); 
//} 
?> 

回答

0

嘗試在原代碼

... 
url: "../wp-content/plugins/woocommerce/admin/test.php?lastid=2098" 
... 

然後在test.php的下面,訪問使用

$lastid = $_GET['lastid']; 

還有其他幾種方法變量這可以完成,這只是一個快速和骯髒的方法。

+0

它的工作原理,但我怎樣才能改變 「2098」 到一個變量?因爲在JavaScript中的$ lastid實際上是動態的它增加了,所以我真的應該傳遞變量。代碼還有更多,但它們有點不相干。 –

+0

@你必須賦值給變量(如'VAR lastid =這種情況下user3295330'然後你可以改變它,你想要多少次這樣的「」:'lastid = some_new_value'(或簡稱'lastid ++'如果你想增加一個值))。 –

+0

@ user3295330和發送它只是使用此代碼時:'網址: '../的wp-content /插件/ woocommerce /管理/ test.php的lastid =' + lastid,' –

0

發送變量:

變化:

//data: {$lastid}, 

到:

data: { lastid : '<?php echo $lastid; ?>'}, 

2.獲取變量test.php的

$lastid = $_GET['lastid']; 
1

我不知道如果我明白,如果我明白你的問題,但它似乎是你的第一頁已經評價$ lastId,最有可能從一個INSERT查詢......你也想將其設置爲javascript變量,同時也使用post方法。假設所有,這是我怎麼會在第一頁

<script> 
var $lastid = <?php echo $lastid ?>; 

... 

window.onload = function(){ 

var request = $.ajax({ 
url: "../wp-content/plugins/woocommerce/admin/test.php", 
type: "POST",    
dataType: "html" 
data: {"lastid":$lastid}, 
}); 

request.done(function(msg) { 
    $(".recent-orders").append(msg); 
}); 

request.fail(function(jqXHR, textStatus) { 
    alert("Request failed: " + textStatus); 
}); 

.... 

</script> 

然後在第二頁使用此訪問後

<?php 
$lastid = $_POST['lastid']; 
?> 

這就是你如何張貼在PHP希望這有助於。

0
window.onload = function(){ 
var lastId = <?php echo $lastId; ?>; 
var request = $.ajax({ 
url: "../wp-content/plugins/woocommerce/admin/test.php" + "?lastid=" + lastId, 
type: "GET",    
dataType: "html" 
//data: {$lastid}, 
}); 

request.done(function(msg) { 
    $(".recent-orders").append(msg); 
}); 

request.fail(function(jqXHR, textStatus) { 
    alert("Request failed: " + textStatus); 
}); 

上的test.php加入這一行

$lastId = $_GET['lastid'];