Author Topic: php inside onclick  (Read 1170 times)

0 Members and 1 Guest are viewing this topic.

Offline V1ktorTopic starter

  • Irregular
  • Posts: 17
    • View Profile
php inside onclick
« on: March 08, 2010, 08:10:05 PM »
I need to display 3 php values inside onClick event. I tried code below, but clearly I'm missing something. It pulls values from db and displays them instead of staying within onClick.

<?php 
$info 
= array("'$title'",",","'$city'",",","'$state'");
?>

onclick="javascript:PayScaleExtension.displaySalaryCalculator(<?php echo implode(', '$info); ?> , 'United States');return false;">


This is what I'm trying to accomplish once it pulls values from db:
onclick="javascript:PayScaleExtension.displaySalaryCalculator('Registered Nurse (RN)', 'Seattle', 'Washington', 'United States');return false;">

Thanks.

Offline khr2003

  • Enthusiast
  • Posts: 112
  • Gender: Male
    • View Profile
Re: php inside onclick
« Reply #1 on: March 08, 2010, 08:23:52 PM »
hi
try this

<?php 
$info 
= array("'$title'",",","'$city'",",","'$state'");
$info implode(', '$info);

echo 
"<a href='#' onclick=\"javascript:PayScaleExtension.displaySalaryCalculator($info , 'United States'); return false;\">Click here</a>

Offline V1ktorTopic starter

  • Irregular
  • Posts: 17
    • View Profile
Re: php inside onclick
« Reply #2 on: March 08, 2010, 09:01:36 PM »
Hey, thanks for reply. But still nothing, it displays it right next to the image link. Here's full code that I'm using:
<?php 
$info 
= array("'$title'",",","'$city'",",","'$state'");
$info implode(', '$info);

echo 
"<a href=\"http://www.payscale.com\" 
   title=\"Research Salary at PayScale\"
   onclick=\"javascript:PayScaleExtension.displaySalaryCalculator(
$info , 'United States');return false;\">
   <img src=\"http://www.payscale.com/images/research_salary_bug.gif\" alt=\"PayScale\" width=\"109\" height=\"23\" border=\"0\" /></a>"

?>


This is how it displays it:
Code: [Select]
Sales Representative', ,, 'Greece', ,, 'New York' , 'United States');return false;"> 
Plus image link next to it.

Thanks for your help.

Offline shane18

  • Enthusiast
  • Posts: 272
  • Gender: Male
    • View Profile
Re: php inside onclick
« Reply #3 on: March 08, 2010, 09:06:56 PM »
Try

Code: [Select]
<?php 
$info 
= array("'$title'","'$city'","'$state'");
?>


onclick="javascript: PayScaleExtension.displaySalaryCalculator(<?php echo implode(', '$info); ?>, 'United States');">

That should display it like TITLE, CITY, STATE
« Last Edit: March 08, 2010, 09:09:20 PM by shane18 »
-Shane (TAKE LIFE EASY)

Offline V1ktorTopic starter

  • Irregular
  • Posts: 17
    • View Profile
Re: php inside onclick
« Reply #4 on: March 08, 2010, 09:39:58 PM »
Still nothing. I realized I just need title and city variables passed, state is the same.

Can I use js variables instead? I'm not sure how to insert them inside onClick though.

<script type="text/javascript">
// <![CDATA[
    var city = "' . $city . '";
   var title = "' . $title . '";
// ]]>
</script>   
   <a href="http://www.payscale.com"
   title="Research Salary at PayScale"
   onclick="javascript:PayScaleExtension.displaySalaryCalculator(title,city,'New York','United States');return false;">
   <img src="http://www.payscale.com/images/research_salary_bug.gif" alt="PayScale" width="109" height="23" border="0" /></a>

Offline khr2003

  • Enthusiast
  • Posts: 112
  • Gender: Male
    • View Profile
Re: php inside onclick
« Reply #5 on: March 09, 2010, 05:31:50 AM »
Hey, thanks for reply. But still nothing, it displays it right next to the image link. Here's full code that I'm using:
<?php 
$info 
= array("'$title'",",","'$city'",",","'$state'");
$info implode(', '$info);

echo 
"<a href=\"http://www.payscale.com\" 
   title=\"Research Salary at PayScale\"
   onclick=\"javascript:PayScaleExtension.displaySalaryCalculator(
$info , 'United States');return false;\">
   <img src=\"http://www.payscale.com/images/research_salary_bug.gif\" alt=\"PayScale\" width=\"109\" height=\"23\" border=\"0\" /></a>"

?>


This is how it displays it:
Code: [Select]
Sales Representative', ,, 'Greece', ,, 'New York' , 'United States');return false;"> 
Plus image link next to it.

Thanks for your help.

isn't  that what you wanted? displaying texts within the js?

Do you mean that javascript is broken at some point?

Offline V1ktorTopic starter

  • Irregular
  • Posts: 17
    • View Profile
Re: php inside onclick
« Reply #6 on: March 09, 2010, 07:17:37 AM »
The variables inside onClick shouldn't display on the page next to the image button.
This is the original code with static values:
Code: [Select]
onclick="javascript:PayScaleExtension.displaySalaryCalculator('Registered Nurse (RN)', 'Seattle', 'Washington', 'United States');return false;">
I need to insert php variables inside onclick so they can be populated from database. The table I have setup already pulls $title and $city from database. All I need to figure out is how to insert them inside onClick without them breaking the code. See the image for what it looks like with:
<?php 
$info 
= array("'$title'",",","'$city'",",","'$state'");
$info implode(', '$info);

echo 
"<a href=\"http://www.payscale.com\" 
   title=\"Research Salary at PayScale\"
   onclick=\"javascript:PayScaleExtension.displaySalaryCalculator(
$info , 'United States');return false;\">
   <img src=\"http://www.payscale.com/images/research_salary_bug.gif\" alt=\"PayScale\" width=\"109\" height=\"23\" border=\"0\" /></a>"

?>



[attachment deleted by admin]

Offline khr2003

  • Enthusiast
  • Posts: 112
  • Gender: Male
    • View Profile
Re: php inside onclick
« Reply #7 on: March 09, 2010, 08:11:20 PM »
alright, here is another approach. first try to remove $info completely from js (just to know the js is working fine and the proble is with the php variables). if everything is ok then try to include the data of $info straight into the js without using implode:


<?php 
echo "<a href=\"http://www.payscale.com\" 
   title=\"Research Salary at PayScale\"
   onclick=\"javascript:PayScaleExtension.displaySalaryCalculator('
$title','',",'$city',",",'$state' 'United States');return false;\">
   <img src=\"http://www.payscale.com/images/research_salary_bug.gif\" alt=\"PayScale\" width=\"109\" height=\"23\" border=\"0\" /></a>"

?>

Offline V1ktorTopic starter

  • Irregular
  • Posts: 17
    • View Profile
Re: php inside onclick
« Reply #8 on: March 09, 2010, 10:28:35 PM »
Ahh why it has to be pain in the butt.
It still doesn't work. I tried removing php variables from
onClick and run it like that... it works fine. Seems like having
php variables inside onClick breaks it.

Maybe convert php variable to js variable? That, technically, could
work.

Offline teamatomic

  • Devotee
  • Posts: 1,197
  • Gender: Male
  • fat skis rule!
    • View Profile
    • myPHPtemplate
Re: php inside onclick
« Reply #9 on: March 10, 2010, 01:54:07 AM »
Why the array. Why complicate it to the point you become confused.

You must already have the title,city and state var to put it into an array so why not just use them?

echo "blah...blah onclick="javascript:PayScaleExtension.displaySalaryCalculator('$title', '$city', '$state', 'United States');return false;">";//escape quotes as needed


HTH
Teamatomic
when in doubt...ski fast
when scared...ski faster
when terrified...point em down

Offline V1ktorTopic starter

  • Irregular
  • Posts: 17
    • View Profile
Re: php inside onclick
« Reply #10 on: March 10, 2010, 02:42:15 PM »
Why the array. Why complicate it to the point you become confused.

You must already have the title,city and state var to put it into an array so why not just use them?

echo "blah...blah onclick="javascript:PayScaleExtension.displaySalaryCalculator('$title', '$city', '$state', 'United States');return false;">";//escape quotes as needed


HTH
Teamatomic

Inserting php vars on their own with everything inside echo does the same thing, it breaks the code by displaying values next to the image button. Refer to image example to see what I mean.

They need to stay inside onClick so when someone clicks on the image it opens window with information based on those values.

Offline khr2003

  • Enthusiast
  • Posts: 112
  • Gender: Male
    • View Profile
Re: php inside onclick
« Reply #11 on: March 11, 2010, 04:39:30 AM »
ok. here is the code. I modified according to payscale website. You had many small mistakes in your script, but anyway it is working now on my computer. So if it does not work with you check the rest of your script.

<?php 
$job 
'Registered Nurse (RN)';
$state 'Seattle';
$city 'Washington';
$contry 'United States';


echo 
"<a href=\"http://www.payscale.com\" 
   title=\"Research Salary at PayScale\"
   onclick=\"javascript:PayScaleExtension.displaySalaryCalculator('
$job', '$state', '$city', '$contry');return false;\">
   <img src=\"http://www.payscale.com/images/research_salary_bug.gif\" alt=\"PayScale\" width=\"109\" height=\"23\" border=\"0\" /></a>"
;

?>

Offline V1ktorTopic starter

  • Irregular
  • Posts: 17
    • View Profile
Re: php inside onclick
« Reply #12 on: March 11, 2010, 12:48:59 PM »
ok. here is the code. I modified according to payscale website. You had many small mistakes in your script, but anyway it is working now on my computer. So if it does not work with you check the rest of your script.

<?php 
$job 
'Registered Nurse (RN)';
$state 'Seattle';
$city 'Washington';
$contry 'United States';


echo 
"<a href=\"http://www.payscale.com\" 
   title=\"Research Salary at PayScale\"
   onclick=\"javascript:PayScaleExtension.displaySalaryCalculator('
$job', '$state', '$city', '$contry');return false;\">
   <img src=\"http://www.payscale.com/images/research_salary_bug.gif\" alt=\"PayScale\" width=\"109\" height=\"23\" border=\"0\" /></a>"
;

?>


Ok, I see. It works now. But, you redefined vars to the static options. In that file I already have php setup to call $title, $city, $state from a database. How can I connect that inside PayScale onClick event.

Thanks for your help, really appreciate it.

Offline wildteen88

  • Guru
  • 'Insane!'
  • *
  • Posts: 12,021
  • Gender: Male
    • View Profile
Re: php inside onclick
« Reply #13 on: March 11, 2010, 02:08:08 PM »
if the variables, $job, $state, $city and $country are already defined in your script elsewhere then just delete these lines
$job 'Registered Nurse (RN)';
$state 'Seattle';
$city 'Washington';
$contry 'United States';


If it still doesn't work you'll need to post more code, specifically where you're defining the above variables.
« Last Edit: March 11, 2010, 02:08:59 PM by wildteen88 »

Offline V1ktorTopic starter

  • Irregular
  • Posts: 17
    • View Profile
Re: php inside onclick
« Reply #14 on: March 11, 2010, 06:07:48 PM »
Nope, for some reason it doesn't work when I comment out those vars. Here's the code in that file:
<?php

if ( ! ( defined'_JEXEC' ) || defined'_VALID_MOS' ) ) ) { die( 'Direct Access to this location is not allowed.' ); }

	
	
global 
$_config;
	
	
$make_national=$_config['make_jobs_national'];
	
	

	
	
/*
	
	
 * 1:hepsini göster
	
	
 * 2:&#351;irketin seçmesine izin ver;
	
	
 * 3:hepsini gizle
	
	
 * */
	
	
$hide=$_config['hide_company'];

	
	

	
	
?>
	
	
<?php echo $simple_search_box_wide;?><br />
	
	
<?php echo $subcats;?>
	
	
	
<?php 
	
	
	
if (
$job_count==0) { 
	
	
	
	
echo 
_lkn_error_no_job;
	
	
	

	
	
	
}else {
?>
	
	
	
<div style="padding-top: 20px;">
	
	
	
	
<div id="middleleftwidgetcontainer">
	
	
	
	
	
<div class="jobsfoundnosuggs" >
	
	
	
	
	
	
<h1 style="margin: 15px 0px 8px;" class="searchInfo"><?php echo $job_count;?> - <?php echo temizleSlash($cat_name);?> <?php echo _lkn_jobs?></h1>
	
	
	
	
	
</div>
	
	
	
	
</div>
	
	
	
	
<div style="clear: both;"></div>
	
	
	
</div>
	
	
	
<?php //kategoriye ait i&#351;lerin yaz&#305;r&#305;lmas&#305; ba&#351;lad&#305;?>
	
	
	
<div class="jl_wrap_div grad">
	

	
	
	
    <table cellspacing="0" border="0" style="border-collapse: collapse;" class="jl_tbl">
	
	
	
	
	
	
<tbody>
	
	
	
	
	
	
	
<tr id="gradtitle">
	
	
	
	
	
	
	
	
<th align="left" style="padding: 3px;padding-left: 15px;"><?php echo _lkn_job;?></th>
	
	
	
	
	
	
	
	
<?php if ($hide!='3') {?>
	
	
	
	
	
	
	
	
<th align="left" style="padding: 3px;"><?php echo _lkn_company;?></th>
	
	
	
	
	
	
	
	
<?php }?>
	
	
	
	
	
	
	
	
<?php if ($make_national=='0') {?>
	
	
	
	
	
	
	
	
	
<th align="left" style="padding: 3px;"><?php echo _lkn_job_country;?></th>
	
	
	
	
	
	
	
	
<?php }?>
	
	
	
	
	
	
	
	
<th align="left" style="padding: 3px;"><?php echo _lkn_job_location;?></th>
	
	
	
	
	
	
	
	
<th align="left" style="padding: 3px;"><?php echo _lkn_created;?></th>
                                <th align="left" style="padding: 3px;">Salary</th>
	
	
	
	
	
	
	
</tr>
	
	
	
	
	
	
	
<?php
	
	
	
	
	
	
	
	
$k=1;
	
	
	
	
	
	

	
	
	
	
	
	
	
	
foreach (
$rows as $row) {
	
	
	
	
	
	
	
	
	
//print_r($row);
	
	
	
	
	
	
	
	
	
$title=temizleSlash($row->title);
	
	
	
	
	
	
	
	
	
$id=$row->id;
	
	
	
	
	
	
	
	
	
$published=$row->published;
	
	
	
	
	
	
	
	
	
$alias=$row->alias;
	
	
	
	
	
	
	
	
	

	
	
	
	
	
	
	
	
	
$company_name=temizleSlash($row->company_name);
	
	
	
	
	
	
	
	
	
$company_alias=$row->company_alias;
	
	
	
	
	
	
	
	
	
$company_id=$row->company_id;
	
	
	
	
	
	
	
	
	

	
	
	
	
	
	
	
	
	
$created=$row->created;
	
	
	
	
	
	
	
	
	
$created=lknDate::formatDate($created);
	
	
	
	
	
	
	
	
	

	
	
	
	
	
	
	
	
	
$job_location=$row->job_location;
	
	
	
	
	
	
	
	
	
$city=$row->city;
	
	
	
	
	
	
	
	
	
$state=$row->state;
	
	
	
	
	
	
	
	
	

	
	
	
	
	
	
	
	
	
$location=array();
	
	
	
	
	
	
	
	
	
if (
$city!='') {
	
	
	
	
	
	
	
	
	
	
$location[]=$city;
	
	
	
	
	
	
	
	
	
}
	
	
	
	
	
	
	
	
	

	
	
	
	
	
	
	
	
	
if (
$state!='') {
	
	
	
	
	
	
	
	
	
	
$location[]=$state;
	
	
	
	
	
	
	
	
	
}
	
	
	
	
	
	
	
	
	

	
	
	
	
	
	
	
	
	
if (
count$location )>0) {
	
	
	
	
	
	
	
	
	
	
$locationimplode' , '$location );
	
	
	
	
	
	
	
	
	
}else {
	
	
	
	
	
	
	
	
	
	
 
$location' - ';
	
	
	
	
	
	
	
	
	
}
	
	
	
	
	
	
	
	
	

	
	
	
	
	
	
	
	
	
if (
$hide=='2') {
	
	
	
	
	
	
	
	
	
	
//&#351;irketin seçmesine izin verilmi&#351;
	
	
	
	
	
	
	
	
	
	
$hide_company_name=$row->hide_company_name;
	
	
	
	
	
	
	
	
	
}elseif (
$hide=='1'){
	
	
	
	
	
	
	
	
	
	
$hide_company_name='0';
	
	
	
	
	
	
	
	
	
}elseif (
$hide=='3'){
	
	
	
	
	
	
	
	
	
	
$hide_company_name='1';
	
	
	
	
	
	
	
	
	
}
	
	
	
	
	
	
	
	
	

	
	
	
	
	
	
	
	
	
if (
$hide_company_name=='1') {
	
	
	
	
	
	
	
	
	
	
$company_name=_lkn_job_company_name_is_hidden;
	
	
	
	
	
	
	
	
	
}else {
	
	
	
	
	
	
	
	
	
	
$link_company="index.php?option=com_jobs&task=detail_company&id=$company_id:$company_alias$Itemid;
	
	
	
	
	
	
	
	
	
	
$link_company=lknSef::url($link_company);
	
	
	
	
	
	
	
	
	
	
$company_name="<a href=\"$link_company\">$company_name</a>";
	
	
	

	
	
	
	
	
	
	
	
	
}
	
	
	
	
	
	


	
	
	
	
	
	
	
	
	
	

	
	
	
	
	
	
	
	
	
	
$link_job="index.php?option=com_jobs&task=detail_job&id=$id:$alias$Itemid;
	
	
	
	
	
	
	
	
	
	
$link_job=lknSef::url($link_job);
	
	
	
	
	
	
	
	
	
	
$title="<a href=\"$link_job\" class=\"jt\">$title</a>";
	
	
	
	
	
	
	
	
	
	

	
	
	
	
	
	
	
	
	
	

	
	
	
	
	
	
	
	
	
	
if (
$k=='1') {
	
	
	
	
	
	
	
	
	
	
	
$class='jl_odd_row';
	
	
	
	
	
	
	
	
	
	
}else {
	
	
	
	
	
	
	
	
	
	
	
$class='jl_even_row';
	
	
	
	
	
	
	
	
	
	
}
	
	
	
	
	
	
	
	
	
?>
	
	
	
	
	
	
	
	
	
	
<tr class="<?php echo $class;?>">
	
	
	
	
	
	
	
	
	
	
	
<td align="left"><?php echo $title;?></td>
	
	
	
	
	
	
	
	
	
	
	
<?php if ($hide!='3') {?>
	
	
	
	
	
	
	
	
	
	
	
<td align="left"><?php echo $company_name?></td>
	
	
	
	
	
	
	
	
	
	
	
<?php }?>
	
	
	
	
	
	
	
	
	
	
	

	
	
	
	
	
	
	
	
	
	
	
<?php if ($make_national=='0') {?>
	
	
	
	
	
	
	
	
	
	
	
	
<td align="left"><?php echo $job_location;?></td>
	
	
	
	
	
	
	
	
	
	
	
<?php }?>
	
	
	
	
	
	
	
	
	
	
	
<td align="left"><?php echo $location;?></td>
	
	
	
	
	
	
	
	
	
	
	
<td align="left"><?php echo $created?></td>
                                            <td align="left">
	
	
	
	
	
	
	
	
	
	
	


<script type="text/javascript" src="http://www.payscale.com/js/psextension.js"></script>
<script type="text/javascript">
PayScaleExtension.affiliateId='';
PayScaleExtension.ServiceURL='http://www.payscale.com';
PayScaleExtension.jobListing = false;
PayScaleExtension.backgroundColor = '#9a0000';
PayScaleExtension.textColor = '#ffffff';
PayScaleExtension.init();
</script>

<?php 
//$job = 'Registered Nurse (RN)';
//$state = 'Seattle';
//$city = 'Washington';
//$contry = 'United States';


echo "<a href=\"http://www.payscale.com\" 
   title=\"Research Salary at PayScale\"
   onclick=\"javascript:PayScaleExtension.displaySalaryCalculator('
$job', '$state', '$city', '$contry');return false;\">
   <img src=\"http://www.payscale.com/images/research_salary_bug.gif\" alt=\"PayScale\" width=\"109\" height=\"23\" border=\"0\" /></a>"
;

?>
</td>
	
	
	
	
	
	
	
	
	
	
</tr>
	
	
	
	
	
	
	

	
	
	
	
	
	
	
	
	
<?php
	
	
	
	
	
	
	
	
	
$k=3-$k;
	
	
	
	
	
	
	
	
}
	
	
	
	
	
	
	
	
?>
	
	
	
	
	
	
</tbody>
	
	
	
	
	
</table>
	
	
	
</div>
	
	
	
<?php //kategoriye ait i&#351;lerin yaz&#305;r&#305;lmas&#305; bitti?>
	
	
	

	
	
	
<?php //sayfalama linkleri ba&#351;lad&#305;?>
	
	
	
<div style="margin: 5px; padding: 5px;">
	
	
	
	
<?php echo $paging_links;?>
	
	
	
</div>
	
	
	
<?php //sayfalama linkleri bitti?>
	
	
<?php }?>