trying to .load() a php file, and display html inside - php

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 :)

<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?

Related

How to alter css class once found in chrome console

Not sure how best to title this. I have a button that creates a modal popup for a job application form in WP Job Manager. I wish to duplicate this button on the same page which I have done, however my new button not only creates the modal but also loads the same application form on the page itself at the same time, this is not intended. The popup/modal is using Magnific Popup. Using Chrome console I found the issue that my new button is using a class without 'mfp-hide' at the end of it whereas the first button does have this.
By adding mfp-hide to the end of the class for that button in the Chrome console I solve the issue - the modal then loads correctly and the modal content does not load on the page. My issue is how/where to allocate a new class with mfp-hide at the end of it in my files. I am used to modifying class properties in child themes but not sure how to add this property/replace the class type. If you look here https://boolerang.co.uk/job/birch-creative-limited-permanent-full-stack-developer/ for example and right click inspect the top apply button then do the same with the lower apply button you can see the missing 'mfp-hide' on the lower. Clicking the lower button will show the problem.
In content-single-job_listing.php I added this to generate the second button:
<?php if ( candidates_can_apply() ) : ?>
<?php get_job_manager_template( 'job-application.php' ); ?>
<?php endif; ?>
job-application.php contains:
<?php if ( $apply = get_the_job_application_method() ) :
wp_enqueue_script( 'wp-job-manager-job-application' );
?>
<div class="job_application application">
<?php do_action( 'job_application_start', $apply ); ?>
<input type="button" class="application_button button" value="<?php
esc_attr_e( 'Apply for job', 'wp-job-manager' ); ?>" />
<div class="application_details">
<?php
/**
* job_manager_application_details_email or
job_manager_application_details_url hook
*/
do_action( 'job_manager_application_details_' . $apply->type,
$apply );
?>
</div>
<?php do_action( 'job_application_end', $apply ); ?>
</div>
<?php endif; ?>
The Chrome console for the original button shows:
<div class="application_details modal mfp-hide" id="apply-overlay"
style="display: block;">
<form class="job-manager-application-form job-manager-form" method="post"
enctype="multipart/form-data" action="https://boolerang.co.uk/job/birch-
creative-limited-permanent-full-stack-developer/">
I wish to change the class associated with the lower button to include the 'mfp-hide' which the Magnific Popup says is required in their documentation to have the desired effect. I don't know which file I need to alter to do this as it's not the button class which would be much easier. Any suggestions of where to edit to get this included would be much appreciated.

Where do i add custom PHP code in Wordpress custom template?

Hy all! From what I read, the best way to add PHP code to a wordpress page would be via a custom template. So i took the page.php from the theme and customised it with html code without any problems. The problem is with the PHP code. No matter where I add it, it doesn't work.
My question is where do I add the custom PHP code for the form validation?
The page looks like this:
/*
Template Name: example
*/
<?php
get_header();
if ($tempera_frontpage=="Enable" && is_front_page()): get_template_part( 'frontpage' );
else :
?>
<section id="container" class="<?php echo tempera_get_layout_class(); ?>">
<div id="content" role="main">
<?php cryout_before_content_hook(); ?>
<?php get_template_part( 'content/content', 'page'); ?>
I added the HTML code here:
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?> " >
.....
</form>
<?php
endif;
get_footer();
?>
According to this, you will add it before the HTML, near the top of your code.
Well basically it's PHP and should work, if you want to validate the form in the same template file, use an empty action attribute:
<form method="post" action="">
.....
</form>
And make sure not to use an WordPress reserved word on your form element name attribute, for example name.
At the top of the file you can verify and validate all the request being sent by the form as any other PHP file.
<?php
if (!empty($_POST)) {
//validate or do something here
}
get_header();
if ($tempera_frontpage=="Enable" && is_front_page()): get_template_part( 'frontpage' );
else :
?>
Hope this solution help you out.
I'm thinking you should be modifying the content-page.php template. Note the page template calls it with:
<?php get_template_part( 'content/content', 'page'); ?>
Within the content template you should see something like
<?php the_content(); ?>
If you add the form html on the next line, it will be within the same div, something like
<div class="entry-content>
</div>
This way the new content will fall within the same container as other content and be styled as such.

Wordpress not including my php file

This is my code that I have calling my php file:
<section style="width:100%; height:120px; clear:both;" >
<section class="campaign_statistics" style="background-color:#EFEFEF;">
<?php include('progress_chart.php'); ?>
</section>
</section>
However, nothing displays on the page when I call it, and when I view the page source this is what I see:
<section style="width:100%; height:120px; clear:both;" >
<section class="campaign_statistics" style="background-color:#EFEFEF;">
</section>
</section>
Everything but my php include. All of this was working fine on friday. I go and check my site this morning and it's not displaying what's in that php file. Is there a problem with wordpress? I've gone over all my code and can't find any errors and no changes were made over the weekend.
Here is a portion of the contents of my php file:
<?php
if ($blog_id == 1)
echo
'
<script>
var percent = String(totalProgress.getPercent());
document.write(totalProgress.toString());
</script>
'
;
if ($blog_id == 68)
echo
'
<script>
var percent = String(alumniProgress.getPercent());
document.write(alumniProgress.toString());
</script>
'
;
?>
etc... there's about 20 of these. They're javascript calls.
Either of these built-in wordpress functions should work instead of a plain include:
get_template_part ('progress_chart');
or
include locate_template('progress_chart.php');
http://codex.wordpress.org/Function_Reference/get_template_part
http://codex.wordpress.org/Function_Reference/locate_template
EDIT: Use "include locate_template()" if you want progress_chart to be able to access variables from the file n which it's being included. For some reason get_template_part doesn't allow that.

Get PHP script result from a page with HTML content by AJAX

I have a PHP page with HTML content in it. Now I run some PHP codes between HTMLs and I get some results. The fact is, when I try to get a respond from this page by AJAX it'll show me the whole page content and plus the result I was looking for. What can I do to prevent the page from writing extra content. I know whenever something get printed on page it'll go as respond to the AJAX call but I want a way to somehow fix this.
My page file (name: page.php):
<?php echo $Content->GetData('HEADER'); ?>
<div id="Content">
<div id="Page">
<?php if($Content->GetData('PAGE','IS_TRUE')) : ?>
<?php if(NULL !== $Content->GetData('PAGE','TITLE')) : ?>
<?php echo $Content->GetPlugins("Page:" . $Content->GetData('PAGE','ID') . ",BeforeTitle"); ?>
<div id="Title" dir="rtl">
<?php echo $Content->GetData('PAGE','TITLE'); ?>
</div>
<?php echo $Content->GetPlugins("Page:" . $Content->GetData('PAGE','ID') . ",AfterTitle"); ?>
<?php endif; ?>
<div id="Content" dir="rtl">
<div style="float: right; width: 966px; padding: 6px">
<?php echo $Content->GetPlugins("Page:" . $Content->GetData('PAGE','ID') . ",BeforeContent"); ?>
<?php echo $Content->GetData('PAGE','CONTENT'); ?>
<?php echo $Content->GetPlugins("Page:" . $Content->GetData('PAGE','ID') . ",AfterContent"); ?>
</div>
</div>
<?php else : ?>
<div id="Content" dir="rtl">
<div style="float: right; width: 966px; padding: 6px">
There is no page like this in our archives.
</div>
</div>
<?php endif; ?>
</div>
</div>
<?php echo $Content->GetData('FOOTER'); ?>
If I write an address in my browser like this localhost/cload/blog?action=rate it'll go through my redirection list and show the page.php with blog plugin loaded. The problem is I want to call my blog plugin by AJAX through this address but it will first render the page data.
Sorry if this is messy.
I'd suggest modifying page.php to be some functions, primarily a data processing function, and then an output function. Then, when you load the page, put a check in for whether it's an AJAX request, and if so, echo the data you want as JSON, otherwise render the page using the output function.
Alternatively, you could create a second, separate page, but that could be more difficult to maintain than a single file.
Yes, you should check whether your request is a ajax request, if it is so you should change your response to return only the result whatever want.
This php code will give an rough idea on this.
if($request->isXmlHttpRequest()){
return new Response(json_encode($data_array));
}
Hope this will help.

Yii client side validation doesn't work

There is a page with a link inside the "body" tag.
When the link is inserted the following view is rendered in fancybox.
<div class="form">
<?php
$model = new User;
$model->scenario='registration';
$form=$this->beginWidget('CActiveForm', array(
'id'=>'signup-form',
'action'=>'/signup/',
'enableClientValidation'=>true,
'clientOptions'=>array(
'validateOnSubmit'=>true,
),
)); ?>
<p class="note">Fields with <span class="required">*</span> are required.</p>
<div class="row">
<?php echo $form->labelEx($model,'username'); ?>
<?php echo $form->textField($model,'username'); ?>
<?php echo $form->error($model,'username'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'password'); ?>
<?php echo $form->passwordField($model,'password'); ?>
<?php echo $form->error($model,'password'); ?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton('sign up',array('name'=>'submit')); ?>
</div>
<?php $this->endWidget(); ?>
</div><!-- form -->
When the above code is inserted without fancybox the client validation script is inserted correctly, but when in fancybox there are no jQuery validation functions inserted to the page.
What can be the problem?
(siteController code is inserted here on purpose)
Your problem is most likely how you send the HTML to Fancybox. If you do a renderPartial (as alluded to by user1248203), you need to make sure you have renderPartial postprocess the view that is sent back (post-processing includes javascript/css files that you want rendered along with the view itself).
More info on the CController page
One other thing to note: when you do the post-processing, you will need to make sure that the Javascript for your Fancybox (and possibly jQuery, etc) isn't sent back again. It can cause some really confusing problems. To keep the files from being re-sent over Ajax, use this:
Yii::app()->clientScript->scriptMap['jquery.js'] = false;
Yii::app()->clientScript->scriptMap['jquery.min.js'] = false;
Yii::app()->clientscript->scriptMap['jquery-ui.min.js'] = false;
etc.
Also note that jquery.js is sent when you're debugging on a dev server (with YII_DEBUG set to true), but jquery.min.js is sent on production servers (where YII_DEBUG is set to false). That's bitten me :-) You can see it defined in framework/web/js/packages.php
Is the code called from an Ajax call (renderPartial) or is it called direct? Ajax calls (renderPartial) do not include the javascript validation function, only the whole page does.

Categories