How to submit an ajax call from joomla plugin - php

I have a challenge that is bugging me.
The thing is - I'm creating a plugin that is doing an AJAX call to another file in this plugin. It seems Joomla is preventing me from accessing that file.
I need to POST to a file that returns data from a SOAP call.
I'm calling the file from this line:
$html .= '$.post("search.php", { address: $(\'#address\').val(), zipcode: $(\'#zipcode\').val() },';
I've tried both a relative, absolute and server path as well - Joomla doesn't like it. But I can't remember or find the correct way to do it.
The file is in the same folder as the plugin that outputs above code.
Here's the complete javascript function my plugin outputs:
$html .= '<script type="text/javascript">';
$html .= 'function searchParcels()';
$html .= '{';
$html .= '$.post("search.php", { address: $(\'#address\').val(), zipcode: $(\'#zipcode\').val() },';
$html .= 'function(data) {';
$html .= 'var shops = $.parseJSON(data);';
$html .= '$(\'#shops_output\').html(\'\');';
$html .= 'for(i in shops)';
$html .= '{';
$html .= 'var html = \'<p>\'';
$html .= '+ shops[i][\'CompanyName\'] + \'<br>\'';
$html .= '+ shops[i][\'Streetname\'] + \'<br>\'';
$html .= '+ shops[i][\'ZipCode\'] + \' \' + shops[i][\'CityName\'] + \'<br>\'';
$html .= '+ shops[i][\'Telephone\'] + \'<br>\'';
$html .= '+ \'</p>\';';
$html .= '$(\'#shops_output\').append(html);';
$html .= '}';
$html .= '});';
$html .= '}';
$html .= '</script>';
Any help is greatly appreciated :-)
Thanks.

Please check this post on how to use Joomla's ajax interface (it seems you are not using it). This is much easier and much cleaner (and much more standard) than the method you described. All you need to do is to create the module/plugin (make sure that you have a function name ending with "Ajax") and then issue the right call from anywhere on your Joomla website (make sure you use the URL pattern described on the website).

Related

Link opens as part of page PHP

I have problem with simple link function. On submission form, there is textbox where user can include url. This url is then taken and changed into working link and placed in post. This part works fine. If user include http/https in front of url it will work fine. But if they don't include http/https or only www, then it will open link as example below:
mysiteexample.com/UrlTheyPasted.com
mysiteexample.com/www.UrlTheyPasted.com
(This should open as link to Video/Image/Site,..)
Here is code:
$output .= '<DIV CLASS="'.$outerclass.'">';
$output .= '<DIV CLASS="'.$innertclass.'">'.$title.'</DIV>';
$output .= '<DIV CLASS="'.$innervclass.'">'.$value.'</DIV>';
$output .= '</DIV>';
Anyone know solution for this or why is this happening?
During the processing of the user input, use parse_url() to find out if they have put the protocol on or not and act accordingly.
$url = 'test.com';
$urlscheme = parse_url($url, PHP_URL_SCHEME);
if (empty($urlscheme)) {
$url = 'http://'.$url;
}
die('<pre>'.print_r($url,true));
so for instance, just before your code, put the example:
$scheme = parse_url($value, PHP_URL_SCHEME);
if (empty($scheme)) $value = 'http://'.$value;
$output .= '<DIV CLASS="'.$outerclass.'">';
$output .= '<DIV CLASS="'.$innertclass.'">'.$title.'</DIV>';
$output .= '<DIV CLASS="'.$innervclass.'">'.$value.'</DIV>';
$output .= '</DIV>';

What's a cleaner way to output HTML from a Wordpress plugin?

My Wordpress plugin creates a few shortcodes that return blocks of HTML.
When I register the shortcodes, I do so like this:
add_shortcode('bb-loans-form', function() {
return Shortcodes::loanApplicationForm();
});
And here is the static method from the Shortcodes class:
public static function loadApplicationForm()
{
$form = new \AdamWathan\Form\FormBuilder;
$html = $form->open()->action('/apply')->class('bb-loan-form');
$html .= '<div class="bb-form-field">';
$html .= '<h2>Loan Application Number</h2>';
$html .= $form->text('loan_app_number')->id('loan-app-number');
$html .= $form->submit('Continue Loan');
$html .= '</div>';
$html .= $form->close();
return $html;
}
This is very cumbersome, and messy. I don't like outputting the HTML like this. I've also used Heredoc, but I had to use string substitution to include important values when the form is rendered.
Is there a better way to store my HTML files? I don't want these files publicly accessible. They would have to live in my plugin directory.
It's not a huge plugin, so I'm not overly concerned, but I'd like to know for future reference if there's a cleaner way to include the needed HTML.
You could just use a single string with concatenations...
$form = new \AdamWathan\Form\FormBuilder;
$html = $form->open()->action('/apply')->class('bb-loan-form') .
'<div class="bb-form-field">
<h2>Loan Application Number</h2>' .
$form->text('loan_app_number')->id('loan-app-number') .
$form->submit('Continue Loan') .
'</div>' .
$form->close();
return $html;
It at least keeps the HTML aligned.
I also don't really see an issue with Heredoc, as long as you assign variables and substitute them in:
$form = new \AdamWathan\Form\FormBuilder;
$form_start = $form->open()->action('/apply')->class('bb-loan-form');
$loan_app = $form->text('loan_app_number')->id('loan-app-number');
$submit = $form->submit('Continue Loan');
$form_end = $form->close();
$html = <<<HTML
{$form_start}
<div class="bb-form-field">
<h2>Loan Application Number</h2>
{$loan_app}
{$submit}
</div>
{$form_end}
HTML;
return $html;

PHP - generate & store xml as string in a variable

I'm trying to store a xml as string in a variable so that I can store it in my database.
$xml = "<root>";
foreach(...){
$xml .= "<user id='$id'/>";
}
$xml .= "</root>";
When I echo it, it's not displayed at all as if my web brower reads it as html tag. It doesn't even look like $xml is storing those as texts. Now, I'm trying to do it with DOMDocument... not not quite successful yet. Any tips? :(
Edited my stupid += mistakes..
PHP uses a . as a concatenate operator, or a .= as a shortcut, not a + or +=.
$xml = "<root>";
foreach(...){
$xml .= "<user id='$id'/>";
}
$xml .= "</root>";

How to use CHtml::link(); inside "<li></li>" in Yii?

I need to use CHtml::link() inside " ".
Here is my existing code:
$html .="<li>\n <a href='books".$menu['items'][$itemId]['link']."'>
".$menu['items'][$itemId]['label']."
</a> \n";
$html .= buildMenutree($itemId, $menu);
$html .= "</li> \n";
I hope I can find a nice solution.
You can use the CHtml function in your string just as any other PHP function:
$html .="<li>\n " . CHtml::link($menu['items'][$itemId]['label'], $menu['items'][$itemId]['link']);
See normalizeUrl for the url's you can use.

PHP/Joomla "Premature end of script headers: cgi_wrapper"

I'm experiencing a 500, Internal Server error on my website. The error logs show the following error: "Premature end of script headers: cgi_wrapper", in my Joomla application.
It only occurs when one particular file is loaded. The rest of the application/website works as it should. There are no other related issues that show up in the error logs - no indication of a timeout or a permissions error.
The file that is causing the error is a template/layout file. Furthermore, I have identified a snippet of code that will cause the error, when this is commented out, the site loads just fine. Here it is:
if( $question->type == '1' || $question->type == '2' || $question->type == '3' )
{
//shuffle items
$shuffled_items = $this->shuffle_assoc($items);
$output = '';
$output .= '<table class="answer-table">';
$output .= '<tr>';
$output .= '<td>';
$output .= '<ul class="answers">';
foreach($shuffled_items as $item)
{
$output .= '<li num=' . $item->num . '>';
$output .= $item->item;
$output .= '</li>';
}
$output .= '</ul>';
$output .= '</td>';
$output .= '</tr>';
$output .= '</table>';
$output .= '<table class="answer-table correct-answer" style="display: none">';
$output .= '<tr>';
$output .= '<td>';
$output .= '<p class="question-instructions">Correct Answer:</p>';
$output .= '<ul class="answers correct-answer">';
foreach($items as $item)
{
$output .= '<li num=' . $item->num . '>';
$output .= $item->item;
$output .= '</li>';
}
$output .= '</ul>';
$output .= '</td>';
$output .= '</table>';
echo $output;
}
It uses a variable, $questions, that is set in the view.html.php file (for those who know Joomla). It is this variable that seems to be triggering the error. If I don't assign the $questions variable, then the template will load just fine.
Also, in the example above, it first tests the question type ($question->type). There are other sections of code that would be triggered, and still produce the error, if $question->type is not 1, 2, or 3. But I left out the other code for brevity, and because it only gets called when the relevant question type exists. When the code above alone gets run, it causes the error.
So, I'm lost here. What baffles me is that only this template file, and the $questions variable causes the error. Everything else works fine. It also runs fine on my local server.
Almost forgot, using Joomla 1.5.22, PHP 5.3.3, Apache 2.2.3, Cent OS 5.8. The server also runs Parallels Plesk Panel 10.
I hope someone can point me to a solution.
Thanks!
Check /var/log/httpd/suexec_log
and permissions on /var/www/cgi-bin/cgi_wrapper/cgi_wrapper
it should be
ls -la /var/www/cgi-bin/cgi_wrapper/cgi_wrapper
-rwxr-xr-x 1 root root 5288 Jul 14 2011 /var/www/cgi-bin/cgi_wrapper/cgi_wrapper

Categories