Use both ahref and onclick on php to hIde parameters? - php

I have a php function which gets data from database and calls a javasript function like this:
echo "<p>$header</p>";
The problem is when someone hovers over the link, the browser shows the complete parameters like this:(on bottom left of the browser, or a new tab)
javascript: customm('some secret code here');
Is there a way to hide this? I tried adding onclick to the php function and pointing ahref to #.
echo "<p><a href='#' onclick='javascript: customm('$variable')'>$header</a></p>";
But it didnot work.
Thanks in advance!

It's not working because you need to escape the quotes:
echo "<p><a href='#' onclick='customm(\"$variable\"); return false;'>$header</a></p>";

This code worked:
echo "<p>$header</p>";
Thanks KaeruCT and Blender!

Related

%0d%0a getting added behind the URL of a href attribute in an <a> tag

In a helper plugin for WooCommerce for WordPress, I'm trying to create a custom message to be sent to a buyer.
I don't know why in all the emails that are sent out, %0d%0a is getting added behind the URL in the href attribute. I have tried several different combinations and nothing is working.
This is what I'm trying to do:
echo "<a href='$cl'> Please click here to redeem the coupon</a>";
But because %0d%0a is being added at the back of it the URL doesn't work properly for coupon codes.
This is what the URL looks like afterwards:
https://www.example.com/product-1/?couponCode=TDXGUA9G&utm_source=test&utm_medium=testt&utm_campaign=test&aff_code=TSJU89XYZ%0d%0a
The only thing that doesn't add %0d%0a is this:
echo $cl;
But I want to be able to use the <a> tag so that the buyer doesn't have to click on a long URL link.
Try this
echo "<a href='" . $cl . "'> Please click here to redeem the coupon</a>";
If you still have the issue, check how $cl was created.
I really appreciate all the responses. But I just want to post the code that finally worked for me after 7 hours of work. I understand that it looks silly but this is the best I could do for now. Here's what worked for me:
?>
Click here to take <?php echo ' '.$_product_name.'</span>';
I just encountered the same issue, but I'm using ASP.NET. All I did was delete the single quote and type it again. Now it works.
<asp:HyperLink ... NavigateUrl='<%# Eval("Url") %>' Text='<%# Eval("Name") %>' />
There were possibly some extra invisible characters which needed to be deleted.

How to embed location.href() attribute in PHP statement

I have a PHP echo statement in which I create an HTML button element, and within that button element I'd like to set the onclick attribute to the location.href function to redirect to another page when the user clicks the button. But I can't seem to get this working, nothing happens when the button is clicked. I think it has to do with the multitude of single and double quotes but I'm not sure. Here's my latest attempt, but I've tried escaping the inner quotes a number of ways.
echo "Commissioner Admin</th><td><button type='button' id='adddirectoradmin' value='adddirectoradmin' width='75' onclick=\"location.href('http://some-url')\">Add</button></td>";
location.href is not a function. Your quotes are all OK.
Your problem is that you're calling location.href instead of just assigning it a new value:
onclick="location.href = 'http://some-url'"
try this:
echo "Commissioner Admin</th><td><button type='button' id='adddirectoradmin' value='adddirectoradmin' width='75' onclick='". location.href('http://some-url')."'>Add</button></td>";

PHP, javascript around a link

I have a small piece of javascript to get a user to confirm things on my website. I have a link that I also ask them to confirm before proceeding. I've used the 'onclick' function with buttons no problem, but how do I use it on a link?
Heres my link:
echo"Delete";
And heres my 'onclick':
onclick="return confirm('Please confirm you wish to edit these details')"/>
Any help would be great!
Ummm...
<?php ?>
Delete
You can use onclick on an anchor tag the same way you'd use it on a button:
Delete
However you'd be better off using an event handler:
<script type="text/javascript">
function myClickHandler() {
return confirm("Really?");
}
document.getElementById("my_link").onclick = myClickHandler;
</script>
<a id="my_link" href="<?php echo $urlHTML; ?>">Delete</a>
I recommend you also read up on jQuery. It'll save you a lot of time.
You might want to consider using addEventListener and attachEvent (IE) instead of using inline JavaScript.

jQuery - click() not working when attached to anchor created in PHP

I have this PHP code which outputs HTML anchor elements on a page:
if(!$isOnOwnPage)
echo '<a id="message-button" class="button">Message</a>';
if($isOnOwnPage)
echo '<a id="add-img-button" class="button">Add Image';
else if(!$isFollowing)
echo '<a id="follow-button" class="button">Follow';
else
echo '<a id="follow-button" class="button">Unfollow';
echo "</a>";
When I load the web page, I get this, as expected:
...
<a id="message-button" class="button">Message</a><a id="follow-button" class="button">Unfollow</a>
...
But when I try to attach a click() function to it, the clicking doesn't work. This is the jQuery code. (It's weird because all of my other JS on the page works flawlessly."
$('.button').click(function() { alert("HEY"); }); // It doesn't work grabbing by #follow-button or #message-button either.
What did I do wrong here? I've spent an hour looking at these snippets of code to no avail.
Try this:
$('.button').live('click',function() { alert("HEY"); });
Put it in your $(document).ready function.
Gonna go out on a limb and guess that you've forgotten to wait for document.ready and that the a.button element doesn't exist when the click event is bound.
Is ajax included somehow? Because then you need to either use the live, or re-apply the function.
It should work regardless if it's PHP generated. In the end it's still HTML that gets output to the client.
I think you should make sure that the JS code gets to the part where you assign clicks to the buttons. Maybe there's not document ready or a correct one. Maybe there's a boolean IF that doesn't get passed.
All in all, if you can see the code in the View Source page, then it's a HTML/JS problem, not a PHP one.
This is an old question, but to all there who want to add listeners to dynamic content, instead of using $('a').(click(function(){}) or $('a').on('click',function(){}), you need to use instead the document as the object you want to attach the listeners, because the tag you are inserting wasn't in the DOM when the listener was assigned.
So instead you should use:
$(document).on('click','a',function(){ ... });

Trying to use window.location and replaceWith() in the same time

I have a link that when clicked should generate a PDF document and change the text of himself.
This is my JS function:
function clander(){
window.location="orders/issueBill/order_id/"+order_id;
$('#jander').replaceWith('pdf just generated');
}
The link:
<a href="#" id="jander" onClick='clander()'>click here</a>
The action:
public function executeIssueBill(sfWebRequest $request) {
//here is the code that generates the pdf.
}
The problem: when I click the link, it generates the pdf but the text of the link is not changed. It only changes the text if I remove the window.location... line.
The reason of my code is here.
Any idea?
Regards
Javi
The problem is that it never gets to this part:
$('#jander').replaceWith('pdf just generated');
It redirects the user to "orders/issueBill/order_id/"+order_id, which is a different page, so it doesn't remember state.
I guess your best bet is to pass a parameter to the new page (ie. "orders/issueBill/order_id/"+order_id+"?pdf_generated=1") - this way it'll know that the pdf was generated, and you can change the link accordingly.
I think it's the order of your statements in the js function. If you change the order the other way around, should work fine like so:
<script>
function clander(){
document.getElementById('jander').innerHTML = 'pdf just generated';
window.location="orders/issueBill/order_id/"+order_id;
}
</script>
<a href="#" id="jander" onClick='clander()'>click here</a>
Best,
Kamran

Categories