how to implement a help function with javascript and php? - 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>

Related

Jquery call after passing into Url

Below is my code
<a class="foot" href="<?php echo someurl.com?id;?>" >Info</a>
Iam setting a click function for the class 'foot'
$('.foot').click(function(){
alert('run some functions');
});
As you can see on the code above First it runs Jquery later on it will be passed to specified Url... But is it possible to pass to specific url then run Jquery.. ???
You have to use Ajax request if you want to call the URL without moving to another page. otherwise your javascript code won't execute.
http://api.jquery.com/jQuery.ajax/
if you are doing this for a fallback in case your client doesn't support Javascipt then you have to do it like this.
$('a').click(function(e){
e.preventDefault();
//your code
});
if you want the code to run after page load then I suggest you introduce:
$(document).ready(handler)
That way jquery runs AFTER page load.
You need to use prventDefaults and then trigger document.location.href to your clicked link.
http://api.jquery.com/event.preventDefault/
Hopefully this will solve your problem.
Why you are not using $(document).ready(handler)
<script type="text/javascript">
$(document).ready(function() {
$('.foot').click(function(){
alert('run some functions');
});
});
</script>
<a class="foot" href="hello.php" >Info</a>

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 )

fade in div inside php using javascript after delay on page load?

Hi does anyone know if you can control a div thats encased in php like the one below?
I want to try and find a way of using javascript to fade the div in after a 3 second delay when the page loads?
Can anyone show me a way of doing this?
i would imagine its something like this:
<script>
jQuery(document).ready(function($){
$('.dashboard_intro').hide().delay(2000).fadeIn(2000);
$('.dashboard_intro_arrow').hide().delay(2000).fadeIn(2000);
$('.dashboard_intro_text').hide().delay(2000).fadeIn(2000);
$('.exit_intro').hide().delay(2000).fadeIn(2000);
});
</SCRIPT>
<?php
$dashboard_intro = dashboard_intro();
while ($intro = mysql_fetch_array($dashboard_intro))
if ($intro['dashboard_intro'] == '0') {
echo "<div class=\"dashboard_intro_arrow\"></div><div class=\"dashboard_intro\"></div><div class=\"dashboard_intro_text\"><strong>Welcome to Your Dashboard</strong><br/><br/>These are your tools: Check Messages, Reviews & more.</div><div class=\"exit_intro\"></div>";
} ?>
PHP is run on the server and doesn't affect what jQuery does later in the browser. What does the rendered HTML look like? Something like your example code should do just fine. What's the problem, exactly? Here's a simple demo:
http://jsfiddle.net/tXhwH/1
<script>
$(function() {
$('.dashboard_intro_text').delay(3000).fadeIn(2000);
});
</script>

Automatic href Link click

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');
}

How do I run PHP code when a user clicks on a link?

I want to have a page run some PHP code when a user clicks on a link, without redirecting them. Is this possible with
or with the javascript onclick event?
Yeah, you'd need to have a javascript function triggered by an onclick that does an AJAX load of a page and then returns false, that way they won't be redirected in the browser. You could use the following in jQuery, if that's acceptable for your project:
<script type="text/javascript" src="jquery.min.js"></script>
<script type="text/javascript">
function doSomething() {
$.get("somepage.php");
return false;
}
</script>
Click Me!
You could also do a post-back if you need to use form values (use the $.post() method).
As others have suggested, use JavaScript to make an AJAX call.
whatever
<script>
function myJsFunction() {
// use ajax to make a call to your PHP script
// for more examples, using Jquery. see the link below
return false; // this is so the browser doesn't follow the link
}
http://docs.jquery.com/Ajax/jQuery.ajax
If you haven't yet installed jquery (because you're just a beginner or something), use this bit of code:
link
<script type="text/javascript">
function thisfunction(){
var x = new XMLHttpRequest();
x.open("GET","function.php",true);
x.send();
return false;
}
</script>
I know this post is old but I just wanted to add my answer!
You said to log a user out WITHOUT directing... this method DOES redirect but it returns the user to the page they were on! here's my implementation:
// every page with logout button
<?php
// get the full url of current page
$page = $_SERVER['PHP_SELF'];
// find position of the last '/'
$file_name_begin_pos = strripos($page, "/");
// get substring from position to end
$file_name = substr($page, ++$fileNamePos);
}
?>
// the logout link in your html
Log Out
// logout.php page
<?php
session_start();
$_SESSION = array();
session_destroy();
$page = "index.php";
if(isset($_GET["redirect_to"])){
$file = $_GET["redirect_to"];
if ($file == "user.php"){
// if redirect to is a restricted page, redirect to index
$file = "index.php";
}
}
header("Location: $file");
?>
and there we go!
the code that gets the file name from the full url isn't bug proof. for example if query strings are involved with un-escaped '/' in them, it will fail.
However there are many scripts out there to get the filename from url!
Happy Coding!
Alex
You cant run PHP when a user clicks on a link without leaving the page unless you use AJAX. PHP is a serverside scripting language, meaning the second that the browser sees the page, there is no PHP in it.
Unlike Javascript, PHP is ran completely on the server, and browser wouldn't know how to interpret it if it bit them on the rear. The only way to invoke PHP code is to make a Page request, by either refreshing the page, or using javascript to go fetch a page.
In an AJAX Solution, basically the page uses javascript to send a page request to another page on your domain. Javascript then gets whatever you decide to echo in the response, and it can parse it and do what it wants from there. When you are creating the response, you can also do any backend stuff like updating databases.
There is the only better way is AJAX as everyone is suggest in their posts.
The alternative is using IFrames like below:
<iframe name="f1" id="f1"> </iframe>
<a href='yourpage.php' target='f1'>Click </a>
Now you will get the output in IFrame (you can place IFrame wherever you need in the page or event hide it and the result from the script).
Hope for non Ajax solution this is better.
Well you said without redirecting. Well its a javascript code:
Whatever!
<script type="text/javascript">
function confirm_delete() {
var delete_confirmed=confirm("Are you sure you want to delete this file?");
if (delete_confirmed==true) {
// the php code :) can't expose mine ^_^
} else {
// this one returns the user if he/she clicks no :)
document.location.href = 'whatever.php';
}
}
</script>
give it a try :) hope you like it
either send the user to another page which does it
Execute PHP
or do it with ajax
<script type="text/javascript">
// <![CDATA[
document.getElementById('link').onclick = function() {
// call script via ajax...
return false;
}
// ]]>
</script>
...
Execute PHP
<a href='javascript:void(0)' id='yourId'>Click Me</a>
$(document).ready(function() {
$("#yourId").click(function() {
$.get("yourpage.php");
return false;;
});
});
This should work as well
Submit
<script type="text/javascript">
function callFunction()
{
<?php require("functions.php"); ?>
}
</script>
Thanks,
cowtipper

Categories