Automatic href Link click - php

Hai
i want to generated an automated click event. I am working in php server, i Know Javascript.
Below is my Code
<script language="javascript">
function autoClick() {
var elm=document.getElementById('thisLink');
elm.click();
document.getElementById('thisLink').click();
}
</script>
</head>
i put this on inside the body tag :
onload="setTimeout('autoClick();',3000);"
and inside the a tag :
href="./apage.php" id="thisLink" name="thisLink" target="newWindow"
But it doesn't work in MOzilla Is any solution , 0r any other solution ???
Thanks in advance

You could try JQuery's trigger function.
$('#thisLink').trigger('click');
This should possibly work although I haven't tested it.
JQuery: http://jquery.com
doc: http://docs.jquery.com/Events/trigger#eventdata

Element.click works only on input elements on Mozilla. Try something like
function autoClick() {
var elm=document.getElementById('thisLink');
document.location.href = elm.href;
}
instead, or if you prefer opening the link into a new window,
function autoClick() {
var elm=document.getElementById('thisLink');
window.open(elm.href, 'autoclickwindow');
}

Related

running a php code from a link without redirection

I want to execute a php code when a user click on a given link , i've tried the following code
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="jquery.min.js"></script>
<script type="text/javascript">
function doSomething() {
$.get("up.php");
return false;
}
</script>
</head>
<body>
<center>
Click Me!
</center>
</body>
</html>
where up.php is a php code that implement android GCM mechanism similer to the one in the link : GCM with PHP (Google Cloud Messaging)
but unfortunately when the link is clicked nothing happens , the php code not executed ,and only the url of the page change form *****.com/test.html to *****.com/test.html# , and by the way I've tested the php code and its works fine so it is not a problem in php code , so what i have missed here? why the code is not executed?
i found the solution , the problem that the jquery library is not included probably in my code
you are using jquery. $.get() will be something like below.
$.get('ajax/test.html', function(data) {
$('.result').html(data);
alert('Load was performed.');
});
for further help check http://api.jquery.com/jQuery.get/
Sometimes, you may need to call some JavaScript from within a link. Normally, when user click on a link, the browser loads a new page (or refreshes the same page).
This might not always be desirable. For example, you might only want to dynamically update a form field when the user clicks a link.
To prevent the load from refreshing, you could use the JavaScript void() function and pass a parameter of 0 (zero) as below.
<a href="JavaScript:void(0);" onclick="return doSomething(); " />Click Me!</a>
Now if you want to use href="#", make sure onclick always contains return false; at the end.
OR
Use href="javascript:void(0)"
You can also use jQuery Load
function doSomething() {
$('#result').load('up.php', function() {
alert('Load was performed.');
});
}
When you are using Jquery should be :
function doSomething(){
$.get('up.php', function(data) {
$('.data').html(data);
});
}
Link
To stop page refreshing, you can use the javascript void() function and pass parameter of 0.
Click Me!
<div class="data"></div>
This div is used to display up.php result.
Ok start of by using fire bug and watch the console and network panels
Ensure the browser is trying to get something and not returning a 404 or a php error on the get
Look into prevent default http://api.jquery.com/event.preventDefault/
To stop the original link functionality working
If you still want them to move to the link use a window location or...
Look into bootstrap link buttons
http://twitter.github.com/bootstrap/base-css.html#buttons
( the link button )

Can't find what's wrong in this code

I have the following script which brings up a dialog on my screen telling me there has been an error:
<script type="text/javascript">
var myFunc = function()
{
var html='<div class="cs-body">explanation of the error</div>';
$(html).csDialog();
return false;
};
</script>
I want this dialog to pop-up when my php script returns a "1" value for $error, like this:
if ($error==1) {
echo "<script type='javascript'>$(document).ready(myFunc)</script>";
}
Even if I leave the if-clause and just echo the script it doensn't do anything. Can somebody tell me what I'm missing here?
Thanks in advance!
try changing script type from 'javascript' to 'text/javascript'
that did the trick! thanks lostsource!
You need to make sure, this script fragment is output after the inclusion of jQuery.
Check if you javascript function is defined when running this
echo "<script type='javascript'>$(document).ready(myFunc)</script>
Include jQuery and your js function in the tag, just to see if it works.
PS: you should only add js script in the tag if they are necessary right away, else, added them at the bottom of the tag. Remember, the loading of js files blocks the loading of any other page resources
try this
<?php if ($error==1) { ?>
<script type="text/javascript">
$(document).ready(myFunc)
</script>";
<?php } ?>
Dins

pass variable from input box to php

Hi all i know this question has been posted but being a total noob i couldnt get what answers meant. Please help. I want to pass inputbox value dynamically to a php variable . i am using javascript here please suggest if there's another way without using form submission , _GET or _POST. i want it done dynamically without any submission.
function showHint(str)
{
document.getElementById('TK').innerHTML = str;
var str = str
}
</script>
<html>
<head>Inputbox</head>
<title>TEST PAGE </TITLE>
<body>
<input type='text' id='TK' name='TK' onkeyup='showHint(this.value)'/>
<?php
$str = var str ;
echo "<a href = 'newpage.php?S=$str'/>" ; ?>
</body>
</html>
No. You can't. PHP is NOT a dynamic language, and it does NOT run client side. PHP runs once, and only once, and that's when the page is loaded. It runs its script and it stops. What you can do is get javascript to do an AJAX call. AJAX is basically a way of passing information to another page and getting the data, all in JavaScript. Do some research on it, but in short, you can't make PHP run once it's already been run
<script type="text/javascript" >
function process(){
var field1 = 'whatever';
var field2 = 'more whatever';
$.post("go.php",{field:field1,bext_field:field2},function(result){
alert(result);
});
};
</script>
This will alert out whatever you ECHO from GO.PHP.
You will also need a handler like:
onClick="process();"
on a div, button, image, just about anything you want to "initiate" your post
I would imagine the other answers you found probably would have said the following:
PHP executes before the user has a chance to see the page.
JS let you control what happens after.
Therefore, your problem is that you are trying to use PHP to do something it simply cannot.
Use those points to help guide your decisions when developing your applications. In this case, if you're trying to build a link based on what a user types in a box, your solution to the problem isn't PHP at all (the page is already loaded, you're too late!) -- your solution is JS.
Think about it like this:
/*
assumes you already have an <a> on the page. if not, you'll
have to create a new <a> element dynamically. (google "mdn createElement"
for help)
*/
function showHint (str) {
document.getElementById('TK').innerHTML = str;
var link = document.getElementById('your-a-link');
link.setAttribute('href', 'newpage.php?S=' + str);
}

how to implement a help function with javascript and php?

On a page (i.e mypage.html) I display a menu. One button of this menu shall call a contextual help.
Help
The javascript helpme function calls a php module:
function helpme() {document.location="help.php";}
The help.php, according to the context, shall open into a NEW browser window the content of another php module :
switch ( $_SESSION['function'] )
{
case 'central':
echo '<script type="text/javascript" language="javascript">window.open("/help/help_central.php","help", "height=200, width=200, top=100, left=500, menubar=0, toolbar=0, location=0, status=0");</script>';
break;
etc...
Unfortunately this complicate solution doesn't work: the new window is displayed with the correct text but mypage.html is replaced by help.php !
Could you suggest a correction or an easier way to implement a help ?
Just change the a tag like this:
Help
As the javascript function does nothing but send the user to there it's not needed to do with js.
EDIT
If you want a popup window you can use something like this:
<script type="text/javascript">
function newPopup(url) {
popupWindow = window.open(url,'popUpWindow','height=700,width=800,left=10,top=10,resizable=yes,scrollbars=yes,toolbar=yes,menubar=no,location=no,directories=no,status=yes');
}
</script>
<a onClick="newPopup('http://www.mydomain.com/example/help.php');">Help me</a>
Why not just something like this?
<a target="_blank" href="help.php?#monster">Help on mosters</a>

Saving AJAX page state

Is there any way to listen for hashchanges in the url and log the event for use of a backbutton in AJAX etc.
Using links like such:
Go to page!
and script like this:
<script type="text/javascript">
$(function () {
if (window.location.hash){contentload(window.location.hash);}
$('a').click(function() {
fragment = this.hash;
contentload(fragment);
});
});
function contentload(fragment) {
fragment = fragment.slice(1).replace('!', '')
$('#content').load('http://mysite.com/'+fragment+'?ajax=1');
}
</script>
I need to try and save the state of the page, Ive seen the jQuery address plugin but have no idea how to implement this..
take a look at this jquery plugin
http://benalman.com/projects/jquery-bbq-plugin/
i solved this using the jQuery Address plugin

Categories