I got some php code like this to redirect my page inside one php function:
<script>
function a(){
<?php
header('Location: http://www.localhost.com/');
?>
}
</script>
I will like to know if there is any way to stop this to redirect inmediatly when i load my page, and if it is possible to do it with some Javascript or JQuery, cause I tried it with JS and I couldn't find a way to do it .
The php end of it will process before the javascript does. I'm not sure why you have it set up this way but you will need to come up with a different way of doing things. Perhaps not using PHP at all would be your best option here and just using a javascript command to change the page instead.
Such as:
<script>
function a(){
window.location = "http://www.localhost.com/";
}
</script>
<button onclick="a()">Go</button>
I don't think you can stop it when it comes from php, but you can have the redirect with javascript. See this for example.
Related
How can I auto reload html page based on the value assigned to the session?
if ($_SESSION["LoadPage"])
{
//Reload my html page
}
I know how to use <meta http-equiv="refresh" content="5" > but it cannot start auto refresh itself without someone to refresh
Please anyone can help me
You can use location.reload(); function of the javascript for this.
use string concatenation with script and php
<?php
if ($_SESSION["LoadPage"])
{
?>
<script>
location.reload();//Reload my html page
</script>
<?php
}
?>
You could try php's header location. Or go a javascript route with location.href.
If you use header() make sure the following code is before any output.
In both cases (php or javascript) you want to make sure you unset (or otherwise clear) your session var so you don't run into a circular loop.
header() way:
if (isset($_SESSION["LoadPage"]))
{
unset( $_SESSION["LoadPage"] );
//Reload my html page
header("location: mypage.php");
}
For the javascript way, you want to make sure your script will run after page load. The js way might be preferable to avoid a "redirection warning" from the browser, if applicable.
JS way:
Here I just attach the event to the html body tag, but you could create a more formal event listener if you want.
<body <?php
if ( isset($_SESSION["LoadPage"]) ) {
unset( $_SESSION["LoadPage"] );
?>onload='location.href="mypage.php"';<?php
}
?>>
I am having a dynamic table with info popups.
here mycode
<a id="1" onclick="check(this.id)">
<script type="text/javascript">
function check(value)
{
var result = <?php echo function(value)?>
}
</script>
So I need pass an value Which I received from js function to php function.
Please help how to solve it.I dont want to use Ajax or go to other page.
If you want to use PHP with your function, you'll have to either use AJAX or POST the data and have it display on refresh. However, I don't recommend doing a form submit every time someone clicks a form item.
Ideally, you'd do one of the following:
Use JavaScript by itself to run the function and display the result (AngularJS, Backbone, etc. are good for this)
Submit the form and it will spit out a result on completion
Use AJAX to fill in the item in its place
#meagar I solved this in this way. May be this look some nasty but Ifeel it isok.
<a onclick="check('<?php echo nameofphpfunction($myvariable)','<?php echo nameofphpfunction2($myvariable)')" ></a>
<script>
function check(value1,value2)
{
//here I am sending data to my modal popup which is dynamically
$(#heading).text(value1);
$(#content).text(value2);
$(#modal).modal('show');
}
</script>
If this helps anyone I would most happy.
So I ran into a problem and I couldn't really find a good solution anywhere. And I'm sure more people run into this problem.
I tried to have an Ajax script call to a php-script which echoes a JavaScript function again but this function wont run or activate. It however does show up in the code if you do inspect element.
So the html and Ajax is as follows. Its dummy code since my own is a bit more complicated. so any syntax errors I made here are not the solution since this works for other parts of my code.
<html><headbodyetc>
<div id='change'>
//is supposed to alert or call another js function after
//verifying something with a database for instance.
<button type='button' onclick='ajaxbelow();'>alert</button>
</div>
<script type='text/javascript'>
function ajaxbelow(){
//AJAX code as found on w3schools.com/php/php_ajax_php.asp
//calls the change.php
</script>
</etc></html>
The php code that gets called is very simple.
//This shows up in the html-code after clicking the button but doesnt run.
echo"<script type='text/javascript'>alert('doenst work?')</script>";
So I am looking for a solution which makes me able to run a JavaScript or jquery function after an Ajax call, or the main reason why this doesn't work. Since I couldn't find it.
Inb4 why call the alert via php? Because I need to verify something first with the db on the server-side in my actual code.
So after combining and testing some of the comments I figured out my own answer.
You cant create new javascript within the php echo. You can however echo an onload that calls an existing function. Onload only works for the following tags:
"body", "frame", "frameset", "iframe", "img", "input type="image", "link", "script", "style".
However in this case after testing some of them like "script" and "img" it still didn't work with all tags, but it did with the "style" tag. I didnt test all other tags though.
So that changed my code to:
<html><headbodyetc>
<div id='change'>
//is supposed to alert or call another js function after
//verifying something with a database for instance.
<button type='button' onclick='ajaxbelow();'>alert</button>
</div>
<script type='text/javascript'>
//function to be called
function test(){
alert("now it works");
}
function ajaxbelow(){
//AJAX code as found on w3schools.com/php/php_ajax_php.asp
//calls the change.php
</script>
</etc></html>
and the php-code will then become
echo"<style onload='test();'></style>";
and now it does run the function.
edit this doesn't seem to work for IE, looking for a solution right now.
^
EDIT: By default, IE Browsers "DENY" the ability of scripts to throw prompts. So to enable this functionality, you must go to [Tools/ Internet Options/ Security / Custom Level / "Allow websites to prompt for information using scripted windows"] and enable that... Once you refresh, you will see your alert in IE :)
Just add slashes before single quote as given below
<?php echo '<script type="text/javascript">alert(\'doenst work?\')</script>'; ?>
You could use eval() to evaluate the returned javascript. The script tags wont be required if this method is used.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval
Try this:
http://www.webdeveloper.com/forum/showthread.php?184830-Ajax-echo-script-problem
Basically the reason why nothing happens is because you are just sticking content in the DOM. Javascript is an event driven language and since nothing is telling the javascript to run at this point, its just sitting there doing nothing. If that code were there when the browser loaded the page, then the browser parsing the code is what would tell it to run. So, what you need to do is evaluate any scripts that come back
I have a javascript function "myFunction()" and i need to call this function on First page load only not for all page.
how can i do it.
I will appreciate a lot your help.
thanks
The best is to store in localStorage the fact that you already visited the site (to be precise it's the "origin").
You can put this code in all your pages :
<script>
if (!localStorage['done']) {
localStorage['done'] = 'yes';
myFunction();
}
</script>
If you want to show the page again for next session, replace localStorage with sessionStorage.
Currently I am using YOURLS PHP URL shorterning script.
In their functions.php, seems like they support both redirection which is with PHP header location and javascript.
Functions.php
I want to redirect with javascript, which will show me the message before redirect. I found this coding inside functions.php
// Redirect to another page using Javascript. Set optional (bool)$dontwait to false to force manual redirection (make sure a message has been read by user)
function yourls_redirect_javascript( $location, $dontwait = true ) {
if( $dontwait ) {
echo <<<REDIR
<script type="text/javascript">
window.location="$location";
</script>
<small>(if you are not redirected after 10 seconds, please click here)</small>
REDIR;
} else {
echo <<<MANUAL
<p>Please click here</p>
MANUAL;
}
}
But I don't know how to do it. Is that coding currently commented or ? What should i change to redirect with javascript ?
Please help me out. Thanks.
The function is all ready to go. A javascript redirect will occur on the client side, so this script outputs the appropriate javascript. Call it like so:
<?php # http://www.myurl.com/was/here.php
yourls_redirect_javascript('http://www.myurl.com/go/here.php');
?>
When this page loads javascript will be used to redirect the user. If the javascript fails, there will be a link for them to click to follow the redirect.
I suspect that the "here document" syntax is throwing you off a bit. Read the PHP Docs to learn more about echo <<<
http://php.net/manual/en/function.echo.php