I want to incorporate a session variable $_SESSION['LI'] into the jQuery with an if statement.
So if($_SESSION['LI'] == 'falsus') hide the #feedback else show #feedback.
Thanks
<?php
session_start();
$_SESSION['LI'] = 'falsus';
?><html>
<head>
<title></title>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
$('#feedback').hide();
});
</script>
</head>
<body>
<div id="feedback">Hello</div>
<? print_r($_SESSION);?>
</body>
</html>
Could you just do something like this:
<?php
session_start();
$_SESSION['LI'] = 'falsus';
?>
<html>
<head>
<title></title>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"></script>
<?php if($_SESSION['LI'] == 'falsus'): ?>
<script type="text/javascript">
$(document).ready(function() {
$('#feedback').hide();
});
</script>
<?php endif; ?>
</head>
<body>
<div id="feedback">Hello</div>
<? print_r($_SESSION);?>
</body>
</html>
This will only add the script to hide the #feedback div when the session variable is "falsus". If it is NOT "falsus", then the whole script block is omitted.
<?php
session_start();
$_SESSION['LI'] = 'falsus';
?><html>
<head>
<title></title>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
<?php if ($_SESSION['LI'] == 'falsus') { ?>
$('#feedback').hide();
<?php } else { ?>
$('#feedback').show();
<?php } ?>
});
</script>
</head>
<body>
<div id="feedback">Hello</div>
<? print_r($_SESSION);?>
</body>
</html>
You can weave your code with php wherever you want.
Related
im confused how to add delete alert in this condition
else
if($g=='hapus')
{
// echo 'Hapus'
mysqli_query($config,"DELETE FROM tb_perangkat where id_perangkat='$_GET[id_perangkat]'");
echo '<script LANGUAGE="JavaScript">
alert("Anggota dengan Id '.$_GET[id_perangkat].' Telah Terhapus")
window.location.href="index.php?page=admin";
</script>';
}
Use jquery to hide your div
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
$("p").click(function(){
$(this).hide();
});
});
</script>
</head>
<body>
<p>If you click on me, I will disappear.</p>
<p>Click me away!</p>
<p>Click me too!</p>
</body>
</html>
tried to ask this question earlier but made a total mess of it. so thought i'd try it again but clearer this time.
how can you get php variables to display in loaded content using JQuery?
index.php:
<!doctype html>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<script>
$(document).ready(function(){
$('#clickMe').click(function(){
$('#parent').load('loaded.php #child', {},function(){
});
});
});
</script>
</head>
<?php
session_start();
$test = "this should display php string";
$_SESSION['another'] = "Session variable String";
echo ' tests to see if they work below <br>';
echo $test."<br>";
echo $_SESSION['another']."<br><br>";
?>
<button name="clickMe" id="clickMe" class="clickMe">Click me</button>
<div class="parent" name="parent" id="parent" style="background-color:yellow; height:200px; width:200px;">
</div>
<body>
</body>
</html>
loaded.php:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>
<body>
<div name="child" id="child" class="child">
<p1> html loads fine..</p1><br>
<?php echo $test ?><br>
<?php echo $_SESSION['another'] ?>
</div>
</body>
</html>
As stated by #Fred -ii- in the comments, you only have to fetch the session in your index.php file to do this.
If you want to get a part of another web page inside index.php :
Your index.php should contain this call :
$(document).ready(function(){
$('#clickMe').click(function(){
$('#parent').load('loaded.php'); // No need for additional parameters
});
});
You don't need to select a part of the HTML, return just what you need :
loaded.php :
<?php session_start() ?>
<p>Example text</p>
<?php echo $_SESSION['another'] ?>
I'm trying to build a website for school project. I want the schoolResponse() function to happen after clicking the Submit button. If I remove the jQuery function it works but then it shows a default value when I load the page.
Here's my code:
<!DOCTYPE html>
<html>
<head>
<title>NHG</title>
<meta charset="UTF-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<link type="text/css" rel="stylesheet" href="css/normalize.css"/>
<link type="text/css" rel="stylesheet" href="css/style.css"/>
<link type="text/css" rel="stylesheet" href="css/resposive.css"/>
</head>
<body>
<header>
<div id="main-head">
<h2 id="main-heading">sKoolBook</h2>
</div>
</header>
<section>
<div id="container">
<div id="wrapper">
<form method="POST">
<select name="school">
<option value="none" name="none">Please Select a School...</option>
<option value="NHG">New Horizon Gurukul</option>
</select>
<input type="submit" class="button"/>
<?php
error_reporting(0);
$school = $_POST['school'];
function schoolResponse() {
if ($_POST['school'] == 'none'){
echo 'nothing';
} else {
echo 'something';
}
}
?>
<script>
$('.button').click(
function(
<?php schoolResponse(); ?>
);
);
</script>
</form>
</div>
</div>
</section>
<footer>
</footer>
</body>
</html>
You should use jQuery/AJAX to do this.
<script>
$('.button').click(
$.ajax({
url: 'yoururl.php',
data: $("form").serialize(), //Better to put an ID to the form or something
success: function(data){
//DO something with your data
}
})
);
</script>
There are other functions you should check to properly use ajax requests.
In yoururl.php you should do something like
$school = $_POST['school'];
function schoolResponse() {
if ($_POST['school'] == 'none'){
echo 'nothing';
} else {
echo 'something';
}
}
You can't achieve it like this. Here is one solution:
<?php
error_reporting(0);
$school = $_POST['school'];
function schoolResponse() {
if ($_POST['school'] == 'none'){
echo 'nothing';
} else {
echo 'something';
}
}
if($_POST['school']) schoolResponse();
?>
It would be better if you replace error_reporting(0); with error_reporting(E_ALL); for example, because otherwise php errors will be disabled.
And remove this part:
<script>
$('.button').click(
function(
<?php schoolResponse(); ?>
);
);
</script>
But it is very very basic example.
In this code which I am posting i have one problem. I want my PHP variable to be stored in JavaScript variable but it shows error. The code is below.
<?php
$name="anurag singh";
echo '
<html>
<head>
<script type="text/javascript" src="jquery-2.0.2.js"></script>
<script type="text/javascript">
$(document).ready(function(){
var name1=.$name.";"
$.post("main_page.php",{input1: name1}, function(msg){
alert("hi "+msg);
});
});
</script>
</head>
<body>
<h1>This is the demo!</h1>
<h2>In echo we can add more than one statements</h2>
</body>
</html>
';
?>
Now when I am assigning $name to the variable name1 than I get an syntax error. Please let me know what changes I have to make. So that I get the value of the PHP variable $name stored in JavaScript variable name1.
In javascript with echo version: var name1= "'.$name.'";
<?php
$name = "anurag singh";
echo '
<html>
<head>
<script type="text/javascript" src="jquery-2.0.2.js"></script>
<script type="text/javascript">
$(document).ready(function(){
var name1= "'.$name.'";
$.post("main_page.php",{input1: name1}, function(msg){
alert("hi "+msg);
});
});
</script>
</head>
<body>
<h1>This is the demo!</h1>
<h2>In echo we can add more than one statements</h2>
</body>
</html>
';
?>
And you can use like var name1= "<?php echo $name; ?>"; seperated html and php
<?php
$name="anurag singh";
?>
<html>
<head>
<script type="text/javascript" src="jquery-2.0.2.js"></script>
<script type="text/javascript">
$(document).ready(function(){
var name1= "<?php echo $name; ?>";
$.post("main_page.php",{input1: name1}, function(msg){
alert("hi "+msg);
});
});
</script>
</head>
<body>
<h1>This is the demo!</h1>
<h2>In echo we can add more than one statements</h2>
</body>
</html>
<?php
$name="anurag singh";
echo '
<html>
<head>
<script type="text/javascript" src="jquery-2.0.2.js"></script>
<script type="text/javascript">
$(document).ready(function(){
var name1='.$name.'";"
$.post("main_page.php",{input1: name1}, function(msg){
alert("hi "+msg);
});
});
</script>
</head>
<body>
<h1>This is the demo!</h1>
<h2>In echo we can add more than one statements</h2>
</body>
</html>
';
?>
echo it into a script tag
echo '<script> var name = '.$name.';</script>';
You can do it this way -
<?php $name="anurag singh"; ?>
<html>
<head>
<script type="text/javascript" src="jquery-2.0.2.js"></script>
<script type="text/javascript">
$(document).ready(function(){
var name1="<?php echo $name ?>";
$.post("main_page.php",{input1: name1}, function(msg){
alert("hi "+msg);
});
});
</script>
</head>
<body>
<h1>This is the demo!</h1>
<h2>In echo we can add more than one statements</h2>
</body>
</html>
You are pasing string $ame not variable as because you have used '...' this let the php know that its string no more variables inside this string.
<?php
$name="anurag singh";
?>
<html>
<head>
<script type="text/javascript" src="jquery-2.0.2.js"></script>
<script type="text/javascript">
$(document).ready(function(){
var name1=<?pgp echo $name ?>;
$.post("main_page.php",{input1: name1}, function(msg){
alert("hi "+msg);
});
});
</script>
</head>
<body>
<h1>This is the demo!</h1>
<h2>In echo we can add more than one statements</h2>
</body>
</html>
try this
$(document).ready(function(){
var name1="'.$name.'";
$.post("main_page.php",{input1: name1}, function(msg){
alert("hi "+msg);
});
you can assign this value like var name= "'. $name.'";
I'd like to implement bigpipe in PHP using pcntl.
However, there is something wrong when I visit the web page from browser.
test.php
<html>
<head>
<script type="text/javascript">
function fill(id,content){ document.getElementById(id).innerHTML=content; }
</script>
</head>
<body>
<div id="1">loading...</div>
<div id="2">loading...</div>
<?php
$pids = array();
$pids[0] = pcntl_fork();
if($pids[0]==-1){
die("failed to fork\n");
}else if($pids[0] == 0){//child
sleep(1);
echo "<script type=\"text/javascript\">fill(\"1\",\"im txx\");</script>\n";
exit(0);
}else if($pids[0] > 0){//father
$pids[1] = pcntl_fork();
if($pids[1]==-1){
die("failed to fork\n");
}else if($pids[1] == 0){//child
echo "<script type=\"text/javascript\">fill(\"2\",\"im txx!\");</script>\n";
exit(0);
}else if($pids[1] >0){//father
while(pcntl_wait($status)!=-1);
}
}
?>
</body>
</html>
when I type command "php test.php" in shell, the output is correct:
<html>
<head>
<script type="text/javascript">
function fill(id,content){ document.getElementById(id).innerHTML=content; }
</script>
</head>
<body>
<div id="1">loading...</div>
<div id="2">loading...</div>
<script type="text/javascript">fill("2","im txx!");</script>
<script type="text/javascript">fill("1","im txx");</script>
</body>
</html>
But when I visit it by browser, the page source is unexpected:
<html>
<head>
<script type="text/javascript">
function fill(id,content){ document.getElementById(id).innerHTML=content; }
</script>
</head>
<body>
<div id="1">loading...</div>
<div id="2">loading...</div>
<script type="text/javascript">fill("2","im txx!");</script>
Why the rest of code disappeared?
You are exiting after printing:
exit(0);
this stops all execution of any code beyond this point
Remove the exit() command