How to disable back button in php - javascript

<html>
<head>
<script type="text/javasscript">
window.history.forward();
function noback()
{
window.history.forward();
}
</script>
</head>
<body onload="noback();" onpageshow="if(event.persisted) ;" onunload="">
aa
</body>
</html>
This is the code in HTML which forwards you to same page when you click back, but how to include this script in PHP code.

You can for example do something like that:
<?php
Your PHP Code
?>
Your Html code
<?php
Your PHP Code
?>
Your Html code
<?php
Your PHP Code
?>
Or
<?php
Your PHP Code
...
echo "Your HTML code";
...
?>

just change the extension of your file
.html/.htm
to
.php
And put your php code in this .php file in head section

Related

Insert JS in PHP - on load scroll

I'm trying to get my page to scroll to a certain number of pixels down the page when it loads. I'm editing a wordpress template that I created so it's in PHP.
I've got:
<?php
echo "<body onload='window.scroll(0,400)'>";
?>
at the top of the php file but it's not working. what do I need to do to make this work?
Use this code in your header.php on your child theme or any similar.
<script type="text/javascript">
$( document ).ready(function() {
window.scroll(0,400);
});
</script>
Don't forget insert jquery in your head if this code not working
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
</head>
The onload event attribute is expecting a function, and you can do that inline with javascript: in the attribute...
<?php
echo "<body onload='javascript:window.scroll(0,400);'>";
?>

Hide output from echo json_encode

I was passing an array in php to Javascript using json_encode and ajax. It seems that the only way is to use
echo json_encode($var)
which also prints out the information of $var on the page because of echo. Is there an easy way to hide the output? My code was like the follows,
main.php
<?php
include_once('testing.php');
?>
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script type="text/javascript" src="testing.js"></script>
</body>
<html>
testing.php
<?php
$var=array('1','2','3','4','5');
echo json_encode($var);
?>
testing.js
$.ajax({
url : './testing.php',
type : "GET",
dataType : 'json',
success : function (result) {
showstaff(result);
}
});
function showstaff(x){
console.log(x);
}
Running main.php print out the array('1','2','3','4','5') on the page and show the array in the console, but what I need is to just show the array in the console (i.e. hide the results from the page).
You don't need include_once('testing.php') in main.php. When main.php is sent to the browser, it will load testing.js, which will then execute testing.php via the AJAX call.
You can try this:
<script type="text/javascript">
var object = <?php echo json_encode($var); ?>;
</script>
Hope it helps, thanks!

External javascript call php for widget?

I want that users can include my php file and load content from file in some class.
For example, let say that user have this file:
user.html
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script type="text/javascript" src="myfile.php?width=100&height=100">
</script>
</head>
<body>
<div class="my-class">
</div>
</body>
</html>
and i want from myfile.php in .my-class show some content:
<?php
$width = $_GET['width'];
$height = $_GET['height'];
echo "$('.my-class').load('load_content.php?width=$width&height=$height')";
?>
and in load_content.php something simple for now:
<?php
echo $_GET['width'] + $_GET['height'];
?>
I can't find similar question anywhere, i tried something but only what i success is to document.write something on user.html from myfile.php, but when i tried load something or use innerHTML i get blank page.
I think you are trying to do an ajax call using a <script> tag. Here's what you should rather do, since you have jquery included on your page already:
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
</head>
<body>
<div class="my-class">
</div>
<script>
$(document).ready(function(){
$.get('load_content.php?width=100&height=100')
.done(function(resp){
$('.my-class').html(resp);
})
.fail(function(){
$('.my-class').html('<h2>There was an error loading content</h2>');
});
});
</script>
</body>
</html>

Error while using valiable in Jquery.val() funciton

I got no output when I use variable in val() function instead of fixed value. How can I fix this error.
Example: In the below example, when I use variable ($data="Glenn Quagmire"), I get no output.
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
<?php $data="Glenn Quagmire"; ?>
$("input:text").val($data);
});
});
</script>
</head>
<body>
<p>Name: <input type="text" name="user"></p>
<button>Set the value of the input field</button>
</body>
</html>
Regards
Just use like this:
$("input:text").val("<?php echo $data; ?>");
Full code
<script>
$(document).ready(function(){
$("button").click(function(){
<?php $data="Glenn Quagmire"; ?>
$("input:text").val("<?php echo $data; ?>");
});
});
</script>
Don't use "$" for variables (that's PHP) in jQuery.
<?php echo 'data="Glenn Quagmire";'; ?>
$("input:text").val(data);
JavaScript running in the browser cannot access variables from a PHP program (which has finished running on another computer before delivering the page to the browser so the variable does't even exist any more).
You have to make PHP output the data to the JavaScript.
You can encode it using json_encode (because JSON is (more or less) a subset of JavaScript literal syntax). This will add quotes as needed and escape any special characters in your data (you don't have any in your example, but this is a safe, general approach that protects you from XSS).
$("input:text").val(<?php echo json_encode($data); ?>);
Use <?= $variable ?> to embed variable values in HTML, e.g.:
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("button").click(function(){
<?php $data="Glenn Quagmire"; ?>
$("input:text").val("<?= $data ?>");
});
});
</script>
</head>
<body>
<p>Name: <input type="text" name="user"></p>
<button>Set the value of the input field</button>
</body>
</html>

PHP code will not execute in script tags

I can put php code anywhere in my php file, but if i put the php code within the tags it simply will not work.... What am I doing wrong?
<html>
<head>
<?php
$hello3 = 'hello3';
echo 'hello1';
?>
<script>
<?php echo 'hello2'; ?>
$(document).ready(function(){
$("#target").click(function(){
alert('hello');
});
});
</script>
<?php echo $hello3; ?>
</head>
<body>
<button type="button" id="target">Press Me</button>
</body>
</html>
when I execute this page I get this displayed on my screen:
hello1hello3 (then the button)
the echo within the script tags will not execute... at all.
This is the HTML equivalent of your code:
<html>
<head>
hello1
<script>
hello2
$(document).ready(function(){
$("#target").click(function(){
alert('hello');
});
});
</script>
hello3
</head>
<body>
<button type="button" id="target">Press Me</button>
</body>
</html>
What's wrong with your output?
The output of your PHP, within the script element, looks like this:
hello2 $(document).ready(function(){
$("#target").click(function(){
alert('hello');
});
});
It immediately falls over (with a reference error) because you try to use the variable hello2 without declaring it.
Were that not the cause, it would then fall over because you try to use the variable $ without declaring it (i.e. by including jQuery in your script).
The PHP is executing fine. You are just writing PHP that generates completely broken JavaScript.
Yes this does execute. But items in the <script> tag are not displayed on the screen unless done so by a function or method. You just have items like hello2; in JavaScript. For example what would you expect so see if you had this:
<script>
hello1;
</script>
Well, nothing would be on the screen. You are just creating global variables, which are not defined, so what would happen? Try adding some code for example a simple alert:
<script>
<?php echo 'alert("hello")'; ?>
</script>
When writing PHP the items you echo would be exactly the same as you were writing JavaScript normally. So the same rules for writing JavaScript apply. The above item would look like so after PHP executes:
<script>
alert("hello")
</script>

Categories

Resources