Can I pop up alert in javascript using php - php

I want to trigger javascript alert using PHP.
Is it possible
I want to use it in head section, for displaying it at load time.
<head>
<?php
$valid="valid";
if(!isset($valid))
echo "<script type=\"text/javascript\"> alert('Hi');</script>";
?>
</head>
EDIT
i want to display javascript alert() at load time after checking existance of session

Of course this is possible.
All you are doing is outputting JavaScript, it's all the same thing.
The only issue is that you are nesting <script> tags, which is an HTML error, so get rid of the tags in the echo string.
<script type="text/javascript">
<?php
$valid="valid";
if(!isset($valid))
echo "alert('Hi');";
?>
</script>
By the way, as i'm sure you already know, this specific code will ALWAYS echo the "alert('Hi');"

Related

Check if certain text was outputted to the screen PHP

How would I check if certain text was outputted to the screen using PHP? For example, I have this code:
<html>
<body>
<noscript>
error: no js
<noscript>
<?php
//detect if the HTML printed out "error: no js"
?>
</body>
</html>
I want to detect if HTML printed out the specified <noscript> message. Is this possible to do with PHP, or on a PHP page? Is there a better way to stop any scripts if JavaScript is disabled, or if certain output was outputted?
I would especially like to do this because then I can pass certain output if certain conditions were met, and have the PHP act according to the output.Thanks!
The only thing I can think of is setting a cookie:
<script type="text/javascript">
document.cookie = "jsEnabled=true";
</script>
Then, you can do a simple check in PHP:
<?php
if (isset($_COOKIE['jsEnabled'])) {
// Javascript is enabled!
} else {
// Javascript is not enabled!
}

Sharing data between php and javascript on same page

I am trying to share data on same .php page. I receive some data on a page via POST method and I want to use it on javascript on that page during page load. But somehow javascript is showing the value undefined. What's the reason and how do I fix it?
<?php
echo "<label id='origin' style='visibility:hidden;'>".$_POST["startStation"]."</label>";
?>
<script type="text/javascript">
alert(document.getElementById('origin').value);
</script>
Because label does not have a .value. To access that data you need to use .innerHTML.
<?php echo "<label id='origin' style='visibility:hidden;'>".$_POST["startStation"]."</label>"; ?>
<script type="text/javascript"> alert(document.getElementById('origin').innerHTML); </script>

PHP and JS within HTML

This is what I am trying to do:
I have a data.php page.
In this page I have mostly HTML. Within the HTML I have some PHP code, designated by
<?php
?>
Now, I am trying to insert JavaSript with in the PHP; something like below:
<?php
<script type="text/javascript">
</script>
?>
But I get 'syntax error' notification from Adobe Dreamweaver.
What am I doing wrong here?
Thanks for any help.
You need to end your PHP and then start the Javascript
<?php
...php here...
?>
<script type="text/javascript">
</script>
<?php
... more php ...
?>
You shouldn't enclose Javascript into <?php ... ?> tags.
PHP is executed on the server, before the request reaches the client. Javascript is executed on the client. Therefore you need PHP to echo the javascript.
<?php
echo '<script type="text/javascript" src="xxx">';
echo 'SOME JAVASCRIPT FUNCTIONALITY';
echo '</script>';
?>
Not that I recommend it
If you must enclose JavaScript within PHP tags then you can do something such as this:
<?php
echo '<script type="text/javascript">';
echo '</script>';
?>
Keep in mind that you may have to escape quotes if you need to use the same types within the echo statement. This can be done by adding a back slash - \".
I was recently involved on a project myself where we needed to do some encryption using PHP and use the values returned within the JavaScript but our code was in the following form:
<script type="text/javascript">
// Do something in JavaScript
var x = <?php echo $calculatedValue; ?>;
// etc..
</script>
It can be a nightmare to maintain but it is better form than the first example in this answer.

How can I insert a jQuery plugin into PHP code?

I want to insert the jQuery plugin dropdown login form to make my website more pretty, but I am really confused where and how to insert jQuery into my code. For instance the following is my index.php:
<?php
include ('book_fns.php');
session_start();
do_html_header("Welcome to Store");
echo "<p>Please choose a category:</p>";
// Get categories out of database
$cat_array = get_categories();
// Display as links to cat pages
display_categories($cat_array);
//If logged in as admin, show add, delete, edit cat links.
if(isset($_SESSION['admin_user'])) {
display_button("admin.php", "admin-menu", "Admin Menu");
}
do_html_footer();
?>
And I want to add the jQuery plugin to the top of my index page.
How do I insert this jQuery plugin?
Get it straight from the CDN. Paste this code on your page's HEAD :
<HTML>
<HEAD>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
</HEAD>
</HTML>
Then utilize the library just as you would with your usual javascript code just like this one:
<script>
$(document).ready(function() {
alert('Hey! I was called when the document finished loading.');
});
</script>
I think you should echo the tag to your index page like this:
echo "<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>"
Or you include an HTML/PHP file that contains jQuery script in the <head></head> tag
include('headScript.php');
Or you write a function that echo the <script></script> tag above:
echo $this->headScript("http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js");
protected function headScript($src){
return '<script type="text/javascript" src="$src"></script>';
}
Yes, just treat the JavaScript and jQuery code as you would HTML. The server reads the PHP code and the PHP code tells the server what to send to the browser, and the browser is what reads the jQuery and HTML.

php in javascript?

i need to have some php code inside javascript
<script ...>
<?php
echo " ... ";
?>
</script>
but this doesnt work. how can u implement php inside javascript that is in a own file javascript.php?
That doesn't do what you probably think it does. It'll work, but the PHP gets run once, when the page is loaded, not every time the JavaScript function is called.
Just for clarification, this is what will happen
index.php
<script type="text/javascript">
<?php echo "alert('hello!');"; ?>
</script>
output html in browser
<script type="text/javascript">
alert('hello!');
</script>
If that is what you want to do, then you can output all the javascript you like. What you cannot do is execute PHP code in the user's browser.
your can use php to dynamically generate javascript code, but you cannot execute php client side. If you need to execute php you will need to postback or use AJAX
There seems to be a good bit of misunderstanding of the question... Here is what you want to do to generate JS from PHP on the server:
file javascript.js.php
<?php
header('Content-Type: text/javascript');
?>
// javascript code here
function PrintTime()
{
alert("The time is " + <?php echo json_encode(time()); ?>);
}
Now, include it on the HTML page using normal script tags:
<script type="text/javascript" src="/url/to/javascript.js.php"></script>
The server will process the PHP file, and return javascript from it.
You can't run PHP inside a javascript file. Primarily because PHP runs server side and is processed before the client is sent any actual http info. JavaScript is processed by the browser on the client side and is sent as text.
It looks like you want to pass some kind of dynamic info to the JavaScript. You can do this by passing a variable like this:
<?php $variable="its me"; ?>
<script>
alert('<?php print($variable)?>')
</script>
The output passed to the client is:
<script>
alert('its me')
</script>
What are you trying to accomplish and maybe we can help you come up with a better solution?

Categories