doupishan3309 2010-12-29 05:57
浏览 61
已采纳

尝试.load()一个php文件,并在里面显示html

I'm trying to display the html inside of a php file i have. I'm using .load('file.php') to grab it and show it in a hidden div appended via jquery. But the problem is, it wont display anything past a php line in the file. Like this:

<div id="content">
<h1> Welcome </h1>
<form action="<?php activity_post_form_action()?> "method="post" id="whats-new-form" name="whats-new-form">

Everything below the form wont show, but the <h1> Welcome </h1> will show.

All of the html and such is valid, meaning I closed all of my tags and such. It just halts after it sees an php line. I removed the php from the form, and more showed up until the next php line.

Its definitely something i did wrong...lol, first rule to coding: EVERYTHING is your fault :)

Any ideas what i did wrong?

also, if you want a look at my jquery line :

jQuery('#window').load('file.php');

This is the content of the file.php:

<div id="content">
<div class="raberu"><h6>post update</h6></div>
<h1> Welcome </h1>
<form action="<?php bp_activity_post_form_action()?>" method="post" id="whats-new-form" name="whats-new-form">

<?php do_action( 'bp_before_activity_post_form' )?>

<div class="content-body">

<div class="top-content">
<h5>
<?php if ( bp_is_group() ) : ?>
<?php printf( __( "What's new in %s, %s?", 'buddypress' ), bp_get_group_name(), bp_get_user_firstname() ) ?>
<?php else : ?>
<?php printf( __( "So, what's up %s?", 'buddypress' ), bp_get_user_firstname() ) ?>
<?php endif; ?>
</h5>
</div>

<div class="avatar"><img src="<?php bp_loggedin_user_avatar('html=false');?>"/></div>

<div class="foremu">

<div id="whats-new-textarea">
 <span class="arato">Click here to start typing</span>
<textarea name="whats-new" id="whats-new">
</textarea>
</div>
<div id="whats-new-options">
<div id="whats-new-submit">
 <span class="ajax-loader"></span>
 <input type="submit" name="aw-whats-new-submit" id="aw-whats-new-submit" value="<?php     _e( 'Post Update', 'buddypress' )?>"/>
 <input type="button" class="cancel" value="cancel"/>
</div>
</div>

</div>

And when I check my error counsel, I get This : PHP Fatal error: Call to undefined function and it says this for each line of php code that works just fine if I load that page as a template file via wordpress/php...

EDIT**

Ok, I found this, I just have to figure out how to work with it:

  • When you post your ajax call from javascript using jQuery, you can define the action
  • which will determin which function to run in your PHP component code. *
  • Here's an example: *
  • In Javascript we can post an action with some parameters via jQuery:
    • jQuery.post( ajaxurl, {
  • action: 'my_example_action',
  • 'cookie': encodeURIComponent(document.cookie),
  • 'parameter_1': 'some_value'
  • }, function(response) { ... } ); *
  • Notice the action 'my_example_action', this is the part that will hook into the wp_ajax action.
  • You will need to add an add_action( 'wp_ajax_my_example_action', 'the_function_to_run' ); so that
  • your function will run when this action is fired.
    • You'll be able to access any of the parameters passed using the $_POST variable.

So if anyone is having trouble implementing php functions via ajax in wordpress, buddypress etc. Maybe this will help :)

  • 写回答

1条回答 默认 最新

  • douzhang3356 2010-12-29 06:16
    关注

    <form action="<?php activity_post_form_action()?> "method="post" id="whats-new-form" name="whats-new-form">

    You have a space after your closing PHP tag. so your action might be showing up like this:

    <form action="whatever.php ">

    However, I don't think that is your issue.

    Your jQuery syntax looks correct. (http://api.jquery.com/load/)

    An easy way to double check for Javascript errors you can use Firebug for Firefox (https://addons.mozilla.org/en-US/firefox/addon/1843/)

    It could have to do with running on a local server. Do you know the details of your server setup?

    EDIT:

    If call to undefined function is an error you are getting, it is likely that the standalone PHP file is failing to call functions that are included in the template.

    When you include that PHP file on a template page it works fine. HOWEVER, if you call a PHP file via AJAX, it cannot access functions on the page you loaded from.

    ajaxphpfile.php

    whatever();

    Example1.php

    
    
    function whatever() {
        echo "hello!";
    }
    
    include('ajaxphp.file.php');
    
    

    Example2.php

    
    
    function whatever() {
        echo "hello!";
    }
    
    jQuery('#window').load('ajaxphpfile.php');
    
    

    Example2.php will throw an error when loaded using .load() because it can't access the whatever() function. You are trying to call wordpress functions in a standalone php file when you have not included them. Does that make sense?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突