Onclick function that mails page url to admin - php

I need to create some kind of JS onclick that will run a php mail script that emails a page with broken content.
Would it be best to run a normal javascript onclick that calls a php file which is hidden, grabs the referring url and emails it?
I would also like a message confirming the click, then disable it.
Is there anything like this available?

This will create onClick handler for your link, and action will be done in case user will accept confirm message.
var onClickHandler = function() {
if (confirm("Are you sure?")) {
$.post("your_php_mail_script.php", {broken_url: document.location});
}
};
$("#your_link_id").bind("click", onClickHandler);
And that is all. In your script you'll get broken url as POST parameter ($_POST['broken_url']). This will be done asynchronous.
I can't understand what you want to disable and after what action.

Related

Remove content of textarea when iframe is done?

So currently my website works like this; you post an update and through iframe your update gets added to the database and then shown in a list below. But the problem is that when you clicked "Submit" the text you wrote is still shown in the textarea because the website doesn't update completely. I have tried to have "onsubmit" and "onclick" but both remove the content of the textarea before it gets added to the database so it displays an empty message.
What should I do in order to delay it just a second or how do I make it wait for the iframe to "send" data to my PHP-script?
Give your textarea an id and supposing that iframe is an element of the page that contains the textarea you have done the following in the iframe page:
<?php
//code should be done after db add
?>
<script>
o = parent.document.getElementById('textareaID');
o.value = '';
</script>
<?php
//the end of code or something else
?>
You are able to see those demos on jsbin:
http://jsbin.com/ulOyiVo/1 The page with iframe. Supply the textarea with any text and then click on simulate submit link
http://jsbin.com/EyuBeLo/1/ The iframe page
If your only problem is to have a delayed response, you could trigger a setTimeout function to your onClick, with the given setTimeout:
setTimeout(
function() {
alert('hello');
},1250 //in milliseconds
);
You can define a click or a submit event using jQuery and send a request to your server. You can handle the event when the server responded using a callback. In that callback you need to do whatever it is needed to do. Using setTimeout in this case is an unnecessary hack. You will either set up a big time to wait harming the user experience or in case the page responds later than the specified time your page will work unexpectedly. So, instead of that try defining an event.

javascript button click using curl

I have parsed a page using curl and it contains some check box and a 'select all' and a 'submit' button . Clicking the button selects every check box.'select all' button triggers a javascript function which actually select all check box.
Now i need to click 'select all' and 'submit' button.How can I do this ??
Here is the button code:
input type="button" onclick="SelectAll(137)" value="Select All"
Here is the js function:
function SelectAll(n)
{
for(i=0;i<n;i++)
document.getElementById("ch"+i).checked=true;
}
You can't. If you must use cURL, you'd have to manually craft the POST request yourself. This would involve finding the names of all the checkboxes and sending a bunch of checkbox1=on&checkbox2=on&... in the POST body. See http://davidwalsh.name/execute-http-post-php-curl for a general example of a POST request through cURL with PHP.
Another option would be to use something like Selenium Web Driver, which pretty much allows you to script a web browser like Firefox that can run JavaScript, click things, etc.
Pass a parameter in with your Curl request that gets parsed by jQuery on its $(document).ready(). There's some plugins that help with parsing parameters.
Something like
$(document).ready(function(){
var buttonPress = $.param("buttonPress");
if (buttonPress == true)
$("#myButton").click();
});

AJAX and JS for LIKE Button

I'm trying to learn how make an AJAX script
for a LIKE button, on my website. I have the following questions:
if i'm sending 1 variable.... id.. I do this
data: "action=vote_up&id="+(this).attr("id")",
is this syntactically correct if i'm sending two variables id and id1 ?
data: "action=vote_up&id="+(this).attr("id")&id1="+(this).attr("id1")",
2) What goes into the href attribute? The php page or the AJAX?
<img scr="like.png">
3) which is run first.. The php page or the AJAX.
4) Is it mandatory for me to use jQuery or Pure Javascript for running AJAX
thanks for your time and patience. I most appreciate it.
1) Yes, you could simple undestand it as a PHP-Get request to a script, so multiple vars are possible, like Adam mentioned.
2) For backwards compatibility you should just link to a PHP/whatever-Script that provides the same functionality but doesn't rely on javascript (Not everyone has js enabled). In your javascript you just disable the defult click actione ( see: http://api.jquery.com/event.preventDefault/ ) otherwise it you only want to allow the like funktionality if js is enabled than you could just link to the page anchor '#'.
3) The page runs first. It is progressed by the server and than sent to your browser. In the browser the recieved javascript will start its action.
4) Everything you are using in jquery is based on simple javascript functions, but jquery is much more comfortable ;) The equivalent to the ajax method of jquery is XMLHttpRequest ( http://www.w3schools.com/xml/xml_http.asp )
Here is a idea, hope it helps.
If handle_vote.php is the URL responsible for the handle the up vote, you must do two things:
the a href is the URL with the query string for the up vote, your data, is this case. It must be generated for you server application. It will be used in case of no javascript.
you should put you event to handle the up vote in the a onclick event, to send the ajax request, and use the preventDefault jQuery function to avoid the default event. In this case, a href will never be used, the js will suppress the link click.
A code sample will be almost like this, in you php page:
<a class="like" href="handle_vote.php?action=vote_up&id=<?php echo $post_id; ?>"><img src="like.png"></a>
And it as your jQuery script:
$(function() {
$('a.like').on('click', function(e) {
e.preventDefault();
$.get($(this).attr("href"));
});
});
You can personalize as you like, it is only the idea of how to do it.
<img scr="like.png"> put onclick event on that link, and make AJAX request` to increment count, on success response update count clicks on button. And you forgot about one thing, you should save the state of that button. Because one user can go to your site and click 1000 times on it.

javascript/php - record click destination before redirect

Say I have the link:
google
Is there any way I can record the click (with php/sql)? For example an onclick event to load ajax? Would the ajax run before the page redirects? I want to avoid:
google
You could change your link to:
TEST
And then use the function:
function recordClick(t) {
//Insert AJAX here. t contains the URL
document.location = t;
}
You could then wait for the AJAX request to complete before using the document.location part. However I would suggest simply using a separate file to log clicks like you have already suggested because it is an immediate action that doesn't require JS to be on, and that provides immediate feedback.

Browser Back Button Ajax

I know there a bunch of topics on this, but I couldn't determine what to do based on what I read in the other topics.
I have a page "abc.php". The user can do a search which then populates a form with 2 ajax requests. Then if the user navigates to another page and then clicks BACK to "abc.php", the contents of the form is not complete because the ajax doesn't run. Is there a way to make this happen?
Modify your URL when you do the ajax by adding the search terms there after a hash (e.g. http://example.com/search.php#search-terms-here)
Then when the page is loaded, read the search terms back from the URL.
This is a very nice article / tutorial on enabling the back-button using jQuery.
Using history.js, the following function 'listens' to changes in the url bar, and calls a function to load the appropriate page:
History.Adapter.bind(window,'statechange',function(){
var State = History.getState();
page(State.url);
});
function page(url) {
//AJAX
}
Now whenever you want to change the page, you call:
History.pushState({state:X}, "Page Title", "Page Url");
This will update the browser's url bar, and automatically call page(State.url) for the new url; and all the browser features like forward/back button, bookmarks, etc... should work.

Categories