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 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多
  • ¥15 python中合并修改日期相同的CSV文件并按照修改日期的名字命名文件
  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败