PHP Concatenation with JS Onclick Event - javascript

This is driving me nuts.
I have a button on my php template
echo '<a href="'. esc_url($action_link) .'" class="action_button'. esc_attr($action_class) .'" '. wp_kses_data($action_target) .'>'. wp_kses(mfn_opts_get('header-action-title'), mfn_allowed_html('button')) .'</a>';
And I need to attach the following Analytics event tracker code:
onclick="gtag('event', 'clic', { 'event_category': 'EVENTCAT', 'event_label': 'EVENTLABEL'});"
Sounds not too hard, but im not managing to make this work, after I have tried a lot of different combinations.
Is there any way to just escape everything?

Try this please
echo '<a
onclick="gtag(\'event\', \'clic\', { \'event_category\': \'EVENTCAT\', \'event_label\': \'EVENTLABEL\'});"
href="'. esc_url($action_link) .'"
class="action_button'. esc_attr($action_class) .'" '. wp_kses_data($action_target) .'>'.
wp_kses(mfn_opts_get('header-action-title'), mfn_allowed_html('button'))
.'</a>';

Related

integrating history.back(-1) in php code

I'm not a php developer and I have a basic knowledge in html.
I currently have this code
<div class="page-header">
<h1 class="<?php echo $this->item->backlink ? "backlink" : ""; ?>">
<?php echo $this->item->backlink ? '<a class="backtomap" href="' . $this->item->backlink . '">' . JText::_('COM_FOCALPOINT_BACK_TO_MAP') . '</a>' : "";
echo trim($this->item->title); ?>
</h1>
</div>
now I want to integrate the history.back function in place of the current
onclick="history.back(-1)"
so that the generated html looks something like
<a class="backtomap" href="#" onclick="history.back(-1)>' . JText::_('COM_FOCALPOINT_BACK_TO_MAP') . '</a>'
I tried to change the code here and there, but without the right knowledge it doesn't really work.
Can anybody help?
Thank you.
Just Change line
'<a class="backtomap" href="' . $this->item->backlink . '">' . JText::_('COM_FOCALPOINT_BACK_TO_MAP') . '</a>'
with
'<a class="backtomap" href="#" onclick="history.back(-1)" >' . JText::_('COM_FOCALPOINT_BACK_TO_MAP') . '</a>'

php use <div> for json

In my PHP I have a line of JSON:
echo '"jsoncaption":"'.$xxx.' '.$yyy.' <br /> ('.$zzz.')",';
I want to have the text line-height: 110%;, so I do this:
echo '<div style=\'line-height: 110%;\'>';
echo '"jsoncaption":"'.$xxx.' '.$yyy.' <br /> ('.$zzz.')",';
echo '</div>';
But this causes an an error in my JS console!
Unexpected token < in JSON at position 55
Without the <div> everything works, but I want to use line-height.
Someone have an idea?
echo '"jsoncaption":"' . '<div style=\'line-height: 110%;\'>' . $xxx .' '.$yyy.' <br /> ('.$zzz.')</div>",';
What did I do?
This would be a more detailed script:
$content = $xxx.' '.$yyy.' <br /> ('.$zzz.')';
$wrapped_content = '<div style=\'line-height: 110%;\'>' . $content . "</div>";
echo '"jsoncaption":"' . $wrapped_content . '",';
EDIT: Untested of course, maybe some quotation marks are wrong...

Can the JS Prepend statement edit pre-defined html?

I define my html by setting it equal to a variable i am echoing, just for the sake of keeping my php page dynamic, however i am trying to append another input div onto my text area once a button is clicked via a javascript statement. In terms of syntax i cant seem to find any errors, is this just not allowed?
this is the code for my comment area,
$comment_ui = '<textarea id="statustext" onkeyup="statusMax(this,250)" onfocus="showBtnDiv()" placeholder="What's new with you '.$u.'?"></textarea>';
$comment_ui .= '<div id="uploadDisplay_SP"></div>';
$comment_ui .= '<div id="btns_SP" class="hiddenStuff">';
$comment_ui .= '<form action=""><input id="gendercheck" type="checkbox" name="male" onclick="gendertoggle()">male</form>';
$comment_ui .= '<img src="images/topic.JPG" id="triggerTopic" class="triggerBtn" onclick="showtitleDiv" width="137" height="22" title="Add a title" />';
$comment_ui .= '<button id="statusBtn" onclick="postToStatus(\'status_post\',\'c\',\''.$u.'\',\'statustext\',\''.$male.'\',\'title\')">Post</button>';
$comment_ui .= '<img src="images/camera.JPG" id="triggerBtn_SP" class="triggerBtn" onclick="triggerUpload(event, \'fu_SP\')" width="137" height="22" title="Upload A Photo" />';
$status_ui .= '</div>';
notice i included an image icon for adding a topic to a comment that on click calls this function
function showtitleDiv(){
_("statustext").prepend('<input id="title" onkeyup="statusMax(this,30)" placeholder="Topic headline..."><br />');
/*_("triggerTopic").style.display = "block";*/
}
and i echo the $comment_ui in the body of my document, everything shows, and the comments work, its just the showtitleDiv function isnt working
the javascipt console doesnt show any errors, is the syntax wrong? I appreciate any and all of your expertise!

Get simple CMS plugin

I am currently trying to modify a plugin that was made for get simple cms. The plugin is called "pages with comments". The reason I am posting here is because the topic for the plugin in the forums looks to be dead. Here is the link to the plugin I am currently using. https://mega.co.nz/#!pVcRnTiI!N0SyhLXrPbE1vFH3dNBaqO0zznETDD--QQpWp0c1EJoI have modified it but so you can see what code I am working with I am uploading it as a zip. My question is how do I make a a field required so when users submit the comment there has to be something in it. Either that or make that field a drop down.
Thanks
if ($capt =='Y'){
echo '<div id="captch"> ';
echo '<img alt="" class="capt" id="captcha'.$imfin.'" src="'.$siteurl.'plugins/pages_comments/img_cpt.php?url='.GSPLUGINPATH.'pages_comments/" />';
echo '<input type="button" value="'.$i18n['reload'].'" onClick="javascript:rec_cpt("captcha'.$imfin.'","'.$siteurl.'plugins/pages_comments/img_cpt.php?url='.GSPLUGINPATH.'pages_comments/")" /> <span class="msgavs">'.$i18n['rl'].'</span>';
echo '</div>';
echo '<div class="cap_input">';
echo '<input type="text" value="" name="guest[pot]" /><span>'.$i18n['Cpt'].'('.$i18n['Rf'].']'.'</span>';
echo '</div>';
}
echo '<div class="submit">';
echo '<input type="submit" value="'.$i18n['Ev'].'" name="guest-submit" />';
echo '</div>';
if (empty($_POST["field_name"])) {
$FieldErr = "FIELD is required"; //or any other error message
} else {
// code for submitting;
}
// you can dispaly the error message using echo $FeildErr beside your field area

How to write html image tag inside php [duplicate]

This question already has answers here:
PHP echo variable and html together
(10 answers)
Closed 8 years ago.
I want to write html image tag inside php but the below code is not working, Please help.
<?php
....
....
echo '<img src="../admin/upload/'<?=$display_img?>" width="120px" height="120px"/>
.....
?>
You don't need the php tags you just need to break out of the string:
$width = 120;
echo '<img src="../admin/upload/' . $display_img . '" width="' . $width . 'px" height="120px"/>';
you are inserting a php code block <? ?> when you are already in another.
<?=$display_img?> the equal sign doesn't belong here and the code is attached to the php tags, so the compiler may not be able to read it. It was better this way <?php echo( $display_img ) ?>, of course if you weren't already in php.
Echoing is a bit messy for me, I would've exited php first like this.
<?php
....
....
?>
<img src="../admin/upload/<?php echo( $display_img ) ?>" width="120px" height="120px"/>
<?php
.....
?>
or here is your original code corrected:
<?php
....
....
echo '<img src="../admin/upload/' . $display_img . '" width="120px" height="120px" />';
// OR
echo '<img src="../admin/upload/{$display_img}" width="120px" height="120px" />';
.....
?>

Categories