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.
Related
I am trying to add an if else statement to a file in Magento.
Basically I want it to display different content depending on the body class.
For example, if the body element had a specific class, such as in:
<body class="specific-class">
Then within that body block, then the contents of it might display:
<section class="banner banner-inner <?php Mage::helper('function')->convert_category_name($category_name); ?>">
<div class="wrapper">
<?php echo $this->getChildHtml('breadcrumbs') ?>
<?php echo $this->getLayout()->getBlock('breadcrumbs')->toHtml(); ?>
</div>
<div class="bottom-rip"></div>
</section>
But if the body had a different class, or no class at all, it would default to:
<div class="no_content">
<?php echo="<h1>No content</h1>"; ?>
</div>
Does anyone know the syntax for performing such an operation?
I have a loop in my view that outputs all the content gathered from the database:
<?php foreach($content as $contentRow): ?>
<?php
echo $contentRow->value;
?>
<?php endforeach; ?>
This works fine for HTML strings like:
<h2><strong>Example Text</strong></h2>
however I have some image content that I would like to display and I have tried the following database entries to no avail:
<img src="<?php echo site_url('pathToImage/Image.png'); ?>" alt="Cover">"
<img src="site_url('pathToImage/Image.png')" alt="Cover\">"
I feel like I am missing a step on how to use PHP values in this way.
How do I access the URL of the image and use that to show the image?
Full Code Edit
<?php
$CI =& get_instance();
?>
<div class="container">
<div class="row">
<div class="col-md-9">
<div class="col-md-2"></div>
<div class="col-md-20">
<!--<form class="form-center" method="post" action="<?php echo site_url(''); ?>" role="form">-->
<!-- <h2 class="">Title</h2>
<h2 class=""SubTitle/h2>-->
<?php echo $this->session->userdata('someValue'); ?>
<!--//<table class="" id="">-->
<?php foreach($content as $contentRow): ?>
<tr>
<td><?php
echo $contentRow->value;
?></td>
</tr>
<?php endforeach; ?>
<!--</table>-->
<!--</form>-->
</div>
<div class="col-md-2"></div>
</div>
</div>
</div><!-- /.container -->
and the values are being read out in $contentRow->value;
I have to verify this, but to me it looks like you are echo'ing a string with a PHP function. The function site_url() is not executed, but simply displayed. You can execute it by running the eval() function. But I have to add this function can be very dangerous and its use is not recommended.
Update:
To sum up some comments: The use of eval() is discouraged! You should reconsider / rethink your design. Maybe the use of tags which are replaced by HTML are a solution (Thanks to Manfred Radlwimmer). Always keep in mind to never trust the data you display, always filter and check!
I'm not going to accept this answer as #Philipp Palmtag's answer helped me out alot more and this is more supplementary information.
Because I'm reading data from the database it seems a sensible place to leave some information about what content is stored. In the same table that the content is stored I have added a "content type" field.
In my view I can then read this content type and render appropriately for the content that is stored. If it is just text I can leave it as HTML markup, images all I need to do is specify the file path and then I can scale this as I see fit.
I have updated my view to something akin to this and the if/else statement can be added to in the future if required:
<?php foreach($content as $contentRow): ?>
<?php if ($contentRow->type != "image"): ?>
<?php echo $contentRow->value; ?>
<?php else: ?>
<?php echo "<img src=\"".site_url($contentRow->value)."\">"; ?>
<?php endif; ?>
<?php endforeach; ?>
I tried several options but they are not working, I have 2 pages, page.php and rating.php, on page.php, I have button with this link
< a href=”rating.php/go.php?id=$rows[$id]”>button</a>
, but on page rating.php I have a div with code.
<div id="review" ><?php echo $rows['id'];?></div>
I want that the $rows[$id]from the link rating.php/go.php?id=$rows[$id] could be displayed on div like this . How can I do that to see this $rows[$id] on div?
On page.php you are using < a href="rating.php?id=$rows[$id]">button</a>
If you want $rows[$id] in your rating.php use $_GET
<div id="review" ><?php echo $_GET['id'];?></div>
Read this tutorials too http://www.tutorialspoint.com/php/php_get_post.htm
you get a param from an url with that code snippet:
<div id="review"><?php echo $_GET['id']; ?></div>
the $row[$id] must be a value, it can't be an object
works as :
<div id="review"><?php echo $_GET['id']; ?></div>
Read this:
http://www.formget.com/php-post-get/
For my new wordpress site I need some help. I got 4 pages and 2 different sidebars. On 2 pages there shouldn't be a border-left and on the other 2 pages there should be a border-left. So I got following code:
<div id="sidebar" class="sidebar">
<?php if(is_page('Willkommen') || is_page('Angebot'))
{
dynamic_sidebar('Angebot');
}
else
{
dynamic_sidebar('Anfahrt');
if(is_page('Anfahrt'))
{
echo "<script type='text/javascript'>
function removeBorder()
{
$(document).ready(function() {
if($('#sidebar').hasClass('sidebar'))
{
$(this).removeClass('sidebar')
$(this).addClass('secondsidebar')
}
});
}
</script>";
}
}
?>
</div><!-- sidebar -->
I want to remove the class "sidebar" and add the class "secondsidebar" if there is a div#sidebar on the page. But the code won't work. I wrote this code for myself and I'm a beginner in jQuery :) So please be patient.
I hope someone can give me a hint.
Cheers
A pure PHP solution should be possible as well.
<div id="sidebar" class="<?php if (is_page('Anfahrt')) : ?>secondsidebar<?php else : ?>sidebar<?php endif; ?>">
<?php
if (is_page('Willkommen') || is_page('Angebot')) {
dynamic_sidebar('Angebot');
}
else {
dynamic_sidebar('Anfahrt');
}
?>
</div>
NOTE: try not to use IDs and classes with the same name. #sidebar and .sidebar might get confusing.
Explanation for the classes:
<div id="sidebar" class="<?php if (is_page('Anfahrt')) : ?>secondsidebar<?php else : ?>sidebar<?php endif; ?>">
When the server reads this (PHP is code that is executed by the server before rendering the result back to the user) it comes to "class" and it notices an if-statement. "what text should I put in the class="" of this element?" The server then sees the instructions:
If the page is "Anfahrt": the text to return should be secondsidebar
Else (in all other cases): the text should be sidebar
"Text" isn't really a good name here. But you get what I mean. This "text" is then placed where the if-statement is. The result then is (as it is returned to a user):
If the page is "Anfahrt":
<div id="sidebar" class="secondsidebar">
If the page is anything else:
<div id="sidebar" class="sidebar">
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.