It' s Possible to pass a php variable with $script ='var' ?

For Joomla! 3.x Coding related discussions, you could also use: http://groups.google.com/group/joomla-dev-general

Moderators: ooffick, General Support Moderators

Forum rules
Locked
imagosonus
Joomla! Intern
Joomla! Intern
Posts: 74
Joined: Sat Mar 08, 2014 1:50 pm
Location: rome
Contact:

It' s Possible to pass a php variable with $script ='var' ?

Post by imagosonus » Thu Sep 17, 2015 2:38 pm

I' m trying to develop a component (using Component Creator) and a relative (editor-xtd) Plugin.
After a lot of attemps i get it to work.
But i would like to know if it's possible to expand plugin functions, passing a php variable
with $script ='var' ?


Something like:

Code: Select all

$script .= 'var target = <?php echo $target; ?>' . "\n\t";

almooj-craig
Joomla! Guru
Joomla! Guru
Posts: 500
Joined: Mon Aug 11, 2008 3:05 pm

Re: It' s Possible to pass a php variable with $script ='var

Post by almooj-craig » Sun Oct 04, 2015 12:59 pm

I'm guessing that $script is a php variable and you are writing javascript.

Code: Select all

$script .= "var target='$target';\n\t";

imagosonus
Joomla! Intern
Joomla! Intern
Posts: 74
Joined: Sat Mar 08, 2014 1:50 pm
Location: rome
Contact:

Re: It' s Possible to pass a php variable with $script ='var

Post by imagosonus » Tue Nov 03, 2015 12:25 pm

Wow :D Somebody reply me!!!
Thank' s a lot for your help buddy
Even if after almost one month..
I learn it by myself..
And i think the right way is:

Code: Select all

$script .= "var target='.$target.';\n\t";

almooj-craig
Joomla! Guru
Joomla! Guru
Posts: 500
Joined: Mon Aug 11, 2008 3:05 pm

Re: It' s Possible to pass a php variable with $script ='var

Post by almooj-craig » Tue Nov 03, 2015 2:09 pm

Let's say your php variable is:
$target = 'http://www.domain.com';

If you write it using your code example:

Code: Select all

$script .= "var target='.$target.';\n\t";
If will look like this in javascript:
var target='.http://www.domain.com.';

IF you use the example code:

Code: Select all

$script .= "var target='$target';\n\t";
It will look like this:
var target='http://www.domain.com';
which is what you want.

Craig

imagosonus
Joomla! Intern
Joomla! Intern
Posts: 74
Joined: Sat Mar 08, 2014 1:50 pm
Location: rome
Contact:

Re: It' s Possible to pass a php variable with $script ='var

Post by imagosonus » Tue Nov 03, 2015 3:16 pm

Okay, maybe I should be more precise in asking my question...
What I was trying to do was an editor-xtd plugin to insert bootstrap elements in articles..
And then I do it and it work.. so below my articles editor I have a new button which allow me, by a modal pop up window to insert labels, button etc..
What exactly I was asking for was how to expand functions for ex. the target value of a link which for now it allows me only parent, blank etc.. but I would like to have even open in a pop up..

Code: Select all

<?php
defined('_JEXEC') or die;
JHtml::_('formbehavior.chosen', 'select');
JHtml::script(JUri::base() . 'components/com_bootstrap/js/plug.js');
$params = JComponentHelper::getParams('com_bootstrap');
$script  = 'function insertButtonA() {' . "\n\t";

$script .= 'var href = document.getElementById("href").value;' . "\n\t";
$script .= 'if (href != \'\') {' . "\n\t\t";
$script .= 'href = ""+href+"" ;' . "\n\t";
$script .= '}' . "\n\t";

$script .= 'var target = document.getElementById("target").value;' . "\n\t";
$script .= 'if (target != \'\') {' . "\n\t\t";
$script .= 'target = ""+target+"" ;' . "\n\t";
$script .= '}' . "\n\t";

$script .= 'var title = document.getElementById("title").value;' . "\n\t";
$script .= 'if (title != \'\') {' . "\n\t\t";
$script .= 'title = "title=\""+title+"\" ";' . "\n\t";
$script .= '}' . "\n\t";

$script .= 'var btc = document.getElementById("btc").value;' . "\n\t";
$script .= 'if (btc != \'\') {' . "\n\t\t";
$script .= 'btc = ""+btc+"";' . "\n\t";
$script .= '}' . "\n\t";

$script .= 'var btt = document.getElementById("btt").value;' . "\n\t";
$script .= 'if (btt != \'\') {' . "\n\t\t";
$script .= 'btt = ""+btt+"" ;' . "\n\t";
$script .= '}' . "\n\t";

$script .= 'var btsz = document.getElementById("btsz").value;' . "\n\t";
$script .= 'if (btsz != \'\') {' . "\n\t\t";
$script .= 'btsz = ""+btsz+"";' . "\n\t";
$script .= '}' . "\n\t";

$script .= 'var btal = document.getElementById("btal").value;' . "\n\t";
$script .= 'if (btal != \'\') {' . "\n\t\t";
$script .= 'btal = ""+btal+"";' . "\n\t";
$script .= '}' . "\n\t";

$script .= 'var bag = "<a href=\""+href+"\" class=\""+btc+" "+btsz+" "+btal+"\" "+title+" "+target+">"+btt+"</a>";' . "\n\t";
$script .= 'window.parent.jInsertEditorText(bag, ' . json_encode($this->eName) . ');' . "\n\t";
$script .= 'window.parent.jModalClose();' . "\n\t";
$script .= 'return false;' . "\n";
$script .= '}' . "\n";

JFactory::getDocument()->addScriptDeclaration($script);


$script  = 'function insertButtonB() {' . "\n\t";

$script .= 'var lbc = document.getElementById("lbc").value;' . "\n\t";
$script .= 'if (lbc != \'\') {' . "\n\t\t";
$script .= 'lbc = ""+lbc+"";' . "\n\t";
$script .= '}' . "\n\t";

$script .= 'var lbt = document.getElementById("lbt").value;' . "\n\t";
$script .= 'if (lbt != \'\') {' . "\n\t\t";
$script .= 'lbt = ""+lbt+"" ;' . "\n\t";
$script .= '}' . "\n\t";


$script .= 'var bag = "<span class=\""+lbc+"\">"+lbt+"</span>";' . "\n\t";
$script .= 'window.parent.jInsertEditorText(bag, ' . json_encode($this->eName) . ');' . "\n\t";
$script .= 'window.parent.jModalClose();' . "\n\t";
$script .= 'return false;' . "\n";
$script .= '}' . "\n";
JFactory::getDocument()->addScriptDeclaration($script);
?>

imagosonus
Joomla! Intern
Joomla! Intern
Posts: 74
Joined: Sat Mar 08, 2014 1:50 pm
Location: rome
Contact:

Re: It' s Possible to pass a php variable with $script ='var

Post by imagosonus » Tue Nov 03, 2015 3:26 pm

And those document.getElementById are related to a form below it which i tried to post here but i get error..
But for ex. this ↓

Code: Select all

$script .= 'var target = document.getElementById("target").value;' . "\n\t";
is related to this in the form below ↓

Code: Select all

<label for="target" class="control-label">Target Window</label>
		<div class="controls">
			<select size="1" id="target" name="target">
			     <option value="" selected="selected">Open in parent window</option>
			     <option value="target='_blank'">Open in new window</option>
	        </select>
		</div>

imagosonus
Joomla! Intern
Joomla! Intern
Posts: 74
Joined: Sat Mar 08, 2014 1:50 pm
Location: rome
Contact:

Re: It' s Possible to pass a php variable with $script ='var

Post by imagosonus » Tue Nov 03, 2015 3:42 pm

One more thing..
If you want i can create i dowload link so you can get copy of the component and plugin..
We can make it better together!! And I really want to learn and share knowledge..
And I think it's JOOMLA!!

almooj-craig
Joomla! Guru
Joomla! Guru
Posts: 500
Joined: Mon Aug 11, 2008 3:05 pm

Re: It' s Possible to pass a php variable with $script ='var

Post by almooj-craig » Wed Nov 04, 2015 3:05 pm

There is no value properity for the select object which is why you get a js error. You need to get the selected option's value.

Do a search for accessing a select object in javascript, then once you know how to do it then write it into your php code. Here is an example that's using both ID and TagName, just ignore the TagName for now.
http://www.w3schools.com/jsref/tryit.as ... tion_value

It could be written something like this.

Code: Select all

$script .= "el = document.getElementById('target');\n\t";
$script .= "var target = el[el.selectedIndex].value;\n\t";
I didn't test this so sorry if there is a typo.

imagosonus
Joomla! Intern
Joomla! Intern
Posts: 74
Joined: Sat Mar 08, 2014 1:50 pm
Location: rome
Contact:

Re: It' s Possible to pass a php variable with $script ='var

Post by imagosonus » Thu Nov 05, 2015 10:38 am

Thank you anyway for your support, but i don't get any js error

And this is corrrect ▼

var target = document.getElementById("target").value;


Locked

Return to “Joomla! 3.x Coding”