I want to know if my php variable does not exists, then I want to execute my javascript.
For example-
<?php
if(!isset($_REQUEST['myVar'])){
?>
<script>
alert('variable not exist');
</script>
<?php
}
?>
Is this right way to use javascript code in php extension file
I know all other answers solve your issue but i prefer it to do this way
<script>
<?php
$isset = !isset($_POST['myVar']) ? 'true' : 'false';
echo "var isset = $isset;";
?>
if(isset) {
alert('variable not exist');
}
</script>
when php render your page it will give this output
<script>
var isset = true;
if(isset) {
alert('variable not exist');
}
</script>
Do it like this and it will work:
if (!isset($_REQUEST['myVar'])) {
echo "<script>alert('variable not exist');</script>";
}
you can try writing this piece of code where you want the script to be placed:
<?php
if (!isset($_REQUEST['myVar'])) {
echo '<script>
alert("variable not exist");
</script>';
}
?>
Related
I m trying to redirect pages according to PHP session status, so that, if session start () redirect to page1 else page 2
but seems like i kinda didn't done well with coding.
code goes as:
<script>
if (session_start();) {
window.location = 'page1.php';
}
else {
window.location = 'page2.php';
}
</script>
Any Help is Appreciated..
I think u want something like this:
<script>
<?php
session_start();
if($_SESSION['login'] == true) {
$_SESSION['login'] = false;
echo "window.location = 'page1.php';";
} else {
$_SESSION['login'] = true;
echo "window.location = 'page2.php';";
}
?>
</script>
First of all, session_start is a php-related function, and you are using it inside javascript.
You should prepend and append to every PHP code you want to use both these symbols:
<?php # Start
?> # End
Second Point: If you need to redirect someone to some page, use header("location") in PHP.
So your result code should be:
<?php
if(session_start()){
header("location: page1.php");
}
else{
header("location: page2.php");
}
?>
Btw.. I don't understand why you are using a "session_start" as a condition.
// create a php file first
index.php
// include js on all pages
// create a javascript file
func.js
<script type="text/javascript">
// lets create a function
function newtab(page)
{
if(page != "")
{
window.open(page,"_self","location=yes");
}
}
</script>
<?php
// no harm, we can still add javascript here
// we assume this is index.php
// start a session here
session_start();
// you need to create a switch
if(!isset($_SESSION['switch']))
{
echo '<script>newtab("page1.php")</script>';
}
else
{
echo '<script>newtab("page2.php")</script>';
}
// you can play with the switches
?>
hope this helps
Just do it with PHP:
if(session_start()){
header("Location:page1.php"); die();
} else {
header("Location:page1.php"); die();
}
On page load, I want to check if a PHP Session variable exists:
If it does, alert() the contents
If it doesn't, create it and save the current time
Here is my code:
$(document).ready(function(){
<?php if(session_id() == '') { session_start(); } ?>
if (!<?php echo isset($_SESSION['lbBegin'])?'true':'false'; ?>) {
<?php $_SESSION['lbBegin'] = date("Y-m-d H:i:s"); ?>
} else {
alert("<?php echo $_SESSION['lbBegin']; ?>")
}
});
This code works in the sense that the first page load doesn't produce an alert() and a refresh shows the time, however every refresh / link click afterwards changes the time. I was expecting the time to stay the same during the entire session.
What have I done wrong?
You need to add session_start() at the very beginning and check if a session variable exists. Do this way:
<?php session_start(); // At the very top! No matter what! ?>
<script>
$(document).ready(function(){
if (!<?php echo isset($_SESSION['lbBegin']) ? 'true' : 'false' ; ?>) {
// And you cannot do the below thing, because, first PHP executes before JS even runs. So, you need to use an AJAX Call for this:
<?php $_SESSION['lbBegin'] = date("Y-m-d H:i:s"); ?>
} else {
alert("<?php echo $_SESSION['lbBegin']; ?>")
}
});
</script>
Correcting the AJAX Bit:
<?php session_start(); // At the very top! No matter what! ?>
<script>
$(document).ready(function(){
if (!<?php echo isset($_SESSION['lbBegin']) ? 'true' : 'false' ; ?>) {
// And you cannot do the below thing, because, first PHP executes before JS even runs. So, you need to use an AJAX Call for this:
$.getScript("setTime.php");
} else {
alert("<?php echo $_SESSION['lbBegin']; ?>")
}
});
</script>
Inside the setTime.php add the code:
<?php $_SESSION['lbBegin'] = date("Y-m-d H:i:s"); ?>
Is there a best way to separate javascript and php code. for example:
//php code
if(condition1)
{
?>
// javascript code
$card_typej = $(this).val().trim();
if(condition2)
{
$("#id1").html('<?php echo($var1); ?>');
}
else
{
$("#id1").html('<?php echo($var2); ?>');
}
<?php
}
?>
If yes then, how to separate the above code?
First store required PHP variables in Javascript vars and then manipulate it.
<script>
var var2 = <?php echo json_encode($var2); ?>;
var var1 = <?php echo json_encode($var1); ?>;
if(condition1)
{
?>
// javascript code
$card_typej = $(this).val().trim();
if(condition2)
{
$("#id1").html(var1);
}
else
{
$("#id1").html(var2);
}
<?php
}
?>
</script>
You can see, complicated php+js code mixture is not there now.
I'm having some trouble in JS function for combobox. It functions well if there is a PHP variable being passed, but if there's nothing, the whole block of code doesn't work. I've tried using if(typeof(<?php echo json_encode($information['day']) ?>)!='undefined') but it still doesn't work. Is there another way on how to determine if the PHP variable is set or passed?
There are more ways to do this than I can think of. Here is one.
<script>
<?php if( isset($information) && isset($information['day']) ) { ?>
var myJson = <?php echo json_encode($information); ?>;
<?php } else { ?>
var myJson = null;
<?php } ?>
if(myJson != null) {
// do something
}
</script>
Change this line of code
if(typeof(<?php echo json_encode($information['day']) ?>)!='undefined')
to this
if(typeof("<?php echo json_encode($information['day']); ?>") != 'undefined')
I know it's bad putting JavaScript inside PHP, but I'm rendering HTML with PHP and i want to check if row is empty and hide each of those HTML elements in a class. I know about Ajax, I just want this in PHP.
PHP:
if ($row['image1'] != ''){
echo '<script>
$(".image1Class").each(function() {
$(this).show();
});
</script>';
}
I don't clearly understand what you want to do, but instead of
if ($row['image1'] != '')
you can use
if(!empty($row['image1']))
You can do it like this:
<script>
$(document).ready(
function (){
<?php if ($row['image1'] != ''): ?>
$(".image1Class").show();
<?php else: ?>
$(".image1Class").hide();
<?php endif; ?>
}
);
</script>
Hope this will help your
$(".image1Class").each(function () {
if ($(this).attr("src") == "" )
{
$(this).css("display","none");
}
});