PHP pcntl_fork() : lose code in browser - php

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

Related

how to jquery select $("#div_id").html(' php echo?

How to change div content by jquery with php echo content?
I have tried :
<script>
$("#div_id").html("
<?php echo "asdddd"; ?>
");
</script>
It show error : Uncaught SyntaxError: Invalid or unexpected token
You Have to try this and its working 100% .
<script>
$("#div_id").html('<?php echo "asdddd"; ?>');
</script>
Try this
<script>
$("#div_id").html('<?php echo "asdddd"; ?>');
</script>
below is my working code
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.1/jquery.min.js"></script>
</head>
<body>
<div id="div_id"></div>
</body>
<script>
"$("#div_id").html('<?php echo "asdddd"; ?>');"
</script>
</html>

progressbar para ciclos for em php

My question is very simple. I have some cycles within other cycles, however, let's imagine it's just one and I want to use a progress bar to only show the progress of this cycle.
I have here code that I am developing, in which I use UIKit, but that is making my head hurt because everything works except the progress bar.
The memory consumption is also a complete exaggeration, because through the task manager I see RAM being completely consumed!
The code I'm using is this:
<!DOCTYPE html>
<html>
<head>
<title></title>
<link rel="stylesheet" href="uikit3/css/uikit.min.css" />
<script src="https://code.jquery.com/jquery-3.1.1.min.js"></script>
<script src="uikit3/js/uikit.min.js"></script>
</head>
<body>
<progress id="progressbar" class="uk-progress" value="0" max="100"></progress>
<div id="information" style="width"></div>
<div id="information2" style="width"></div>
<?php
ini_set('memory_limit','16M');
$total = 1741768;
$x = 0;
for($n1=1; $n1 < 15; ++$n1)
{
for($n2=($n1+1); $n2<48; ++$n2)
{
for($n3=($n2+1); $n3<49; ++$n3)
{
for($n4=($n3+1); $n4<50; ++$n4)
{
for($n5=($n4+1); $n5<51; ++$n5)
{
++$x;
$percent = intval($x/$total * 100);
echo '<script language="javascript">document.getElementById("information").innerHTML="'.$percent.'"</script>';
echo '<script language="javascript">document.getElementById("information2").innerHTML="'.$x.'"</script>';
echo '<script>
jQuery(function () {
var animate = setInterval(function () {
window.progressbar && (progressbar.value = "'.$percent.'");
if (!window.progressbar || progressbar.value >= progressbar.max) {
clearInterval(animate);
}
},0);
});
</script>';
}
}
}
}
}
echo '<script language="javascript">document.getElementById("information").innerHTML="Chaves Geradas!"</script>';
?>
</body>
</html>

how to make php variables show in loaded content

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'] ?>

JS/PHP/HTML problem with onload

index.php
<html>
<head>
<script language="JavaScript" src="lol.js.php"></script>
</head>
<?php
//grab product id's who need to be showed
$pids = '1,2,3';
?>
<body onload="update_timers();">
lol.js.php
<script type="text/javascript">
function update_timers()
{
alert('hi');
}
</script>
I'm not sure what I'm missing, but this isn't poping up the alert window. Why is that?
Remove
<script type="text/javascript">
</script>
from the JS file.
The JavaScript error console in your browser should show these tags as syntax errors.
Also, as #Jared points out, you should send a content type header:
<?php header("Content-type: text/javascript"); ?>

How can incorporate session variable in jQuery

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.

Categories