PHP GET parameter not showing without page refresh - php

any idea what is wrong here. I generate link list from database. When I click link, it navigates to another page, but echo is missing. However, if I check "View page source", I can found my echo. If I want to see it on page, I need to refresh page manually, so I can see my echo. I don't to refresh page, so any idea what is problem here?
Source codes:
page.php:
<?php
while ($line = pg_fetch_array($result, null, PGSQL_ASSOC)) {
foreach ($line as $col_value) {
echo '<li>'.$col_value.'</li>';
}
}
// Free resultset
pg_free_result($result);
// Closing connection
pg_close($dbconn);
?>
test.php:
<?php
echo 'TEST ';
echo 'Hello ' . htmlspecialchars($_GET["name"]) . '!';
?>

In your test.php page, replace your code with the following code:
<!DOCTYPE html>
<html>
<head></head>
<body>
<?php
echo 'TEST ';
echo 'Hello ' . htmlspecialchars($_GET["name"]) . '!';
?>
</body>
</html>
Let me know if it works now!
Edited: Explanation of why this solution worked
The image shown in the question showed 2 windows. The confusion starts there. The window on the left is the browser at work displaying the html code that was rendered, while the window on the right is the "Source Code"! What that means is that the browser probably didn't understand what TESTHELLO dbName meant and made a blank page, but when adding all the default tags, then the browser was happy to interpret it as text within the pages body.

Related

Update html page values with PHP

I would like to update html page several times during my PHP script execution.
I understand, that when I requested something like http://index.php the index.php script will return html page in response.
I have my index.php code like:
<?php
set_time_limit(120);
$_SESSION['my_number'] = 0;
//header('Refresh:2 url=http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']);
?>
<!DOCTYPE html>
<html lang="en">
<head/>
<body>
<p style="color:blue;">my_number is <?php echo $_SESSION['my_number']; ?> </p>
</body>
</html>
<?php
foreach (array(1,2,3,4) as $v) {
$_SESSION['my_number'] = $v;
sleep(10);
}
?>
It displays only my_number is 0
If I move foreach before html body - it display my_number is 4.
But I would like to get the html page with the updating my_number every 10 seconds.
So the number should be overwriteen every 10 seconds.
I tried to add
header('Refresh:2 url=http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF']); but still no luck.
Somehow I need to send several responses to browser before exiting the php script. Fist response to load page, others - to update my_number values.
I prefer to get solution only within php. Not using javascript if possible.

echo php variabel in HTML(wordpress)

I have created a custom wordpress post type everything works but my client asked me to insert a function that doenst show the button if the link field is empty that is also working but when I want to display the tekst or link the part where the php is inserted just doesnt shows up what am I doing wrong
I am able to get the data on other parts of this php file but not in this part of the page
<?php
$linktitle = $day_aray=get_field("under_shoe_button_title");
$linkexist = get_field("under_shoe_button_link");
echo($linktitle);
if (empty($linkexist)) {
echo '<html> <p></p></html>' ;
}
else {
echo '<html>
<a href="google.nl" class="button primary is-bevel box-shadow-3 box-shadow-4-hover expand" style="border-radius:5px;"
</html> <?php echo($linktitle); ?> <html><span></span>
<i class="icon-shopping-cart"></i></a>
</html>';
}
?>
If you would look carefully, you would notice, that you are echoing a string where, inside the string, you are trying to echo again. Even with little programming knowledge, you should understand, that it is not logical to do that.
The same goes for php opening <?php tag. You opened the tag at start of the page and later on, inside a string, you are trying to open it again. This does not work.
Instead, close the string (or escape it) and then add the echo option.
echo '<html>
<a href="google.nl" class="button primary is-bevel box-shadow-3 box-shadow-4-hover expand" style="border-radius:5px;"
</html>';
echo($linktitle);
echo '<html><span></span>
<i class="icon-shopping-cart"></i></a>
</html>';
And please, read the comments to you question and learn basic HTML
There are so many things wrong in your code
Firstly you are using echo inside echo you should use concatenation instead.
so you want to echo it like this
echo '<your html code>'.$linktitle.'<your other html code>';
Also your html code is wrong coz u are using many html tags.

Show a complete PHP Variable

im trying to show a complete Php Variable, my code looks like:
<!DOCTYPE HTML>
<html>
<body>
<?php
if (isset($_POST["appendedInputButtonRoom"])) {
$CodeRoom = "<room xs=\"zwinky3\" ac=\"f\" sf=\"N\">" .$_POST["appendedInputButtonRoom"] . "</room>";
echo $CodeRoom;
}
else
{
echo "Error";
}
?>
</body>
</html>
So, for example: I enter "dsf" into the textbox, click "add" and PHP starts to work. It will just show the "dsf" part on the Site, but it should be supposed to show the code like:
<room xs="zwinky3" ac="f" sf="N">dsa</room>
like it does in the source code.
Any one got a idea?
You'll have to html encode your output so the browser wont read the tags
echo htmlspecialchars($CodeRoom);

Can't use document.write more than one time in PHP

Here is my HTML and I call external PHP
<!DOCTYPE html>
<html>
<body>
<h1>My First Heading</h1>
<script src="index.php"></script>
<p>My first paragraph.</p>
</body>
</html>
and my PHP Script
<?
$strFileName = "poom.bis";
$objFopen = fopen($strFileName, 'r');
if ($objFopen) {
while (!feof($objFopen)) {
$file = fgets($objFopen, 4096);
// echo $file;
echo "document.writeln('$file'+);";
}
fclose($objFopen);
}
$test = "hello world";
echo "document.writeln(
'<ul>'+
'<li>.$test.</li>'+
'<li>test2</li>'+
'<li>test3</li>'+
'</ul>'
);";
?>
It error when using document.write more than one time
What should I do to solve this problem
Please Advice
PS. use echo "document.writeln('$file'+);"; for one time there is no error and show a result
First error: your line
echo "document.writeln('$file'+);";
should be
echo "document.writeln('$file');";
(without the plus sign). Also make sure that the file poom.bis doesn't contain a newline, not even at the end. If it does, you have to strip them away (trim()).
Second error was (until you edited it) the use of document.writeIn (which doesn't exist) instead of document.writeln (which does).
Tested and it works.
Also, while I'm at it, since you asked for advice how to solve this problem: look at your browser's error console and try to debug it.
echo '<script>document.writeln(';
echo '"<ul><li>test1</li><li>test2</li><li>test3</li></ul>"';
echo ');</script>';
;

PHP echo out jQuery in IF statement

EDIT: What a TW*T. Sorry everyone for wasting your time. Just missed a Google jQuery link on one F'in page. Whoops.
Hi, i have a div containing 3 forms. These should be the only thing on the screen on page load. When any of them are submitted, a graph gets shown below. What i'm trying to do is within the PHP IF statement is make the div disappear that contains the forms. Sound simple?
This is my code:
if($_GET['submit1']){
echo "<script type='text/javascript'>$('#options').css('display','none');</script>";
However, when i do submit one of the forms (therefore a $_GET has occurred) the div is still there??
EDIT:
If i try people answer on one line i get this:
Parse error: syntax error, unexpected '(', expecting T_VARIABLE or '$'
But if i put in people's multiline answers, no error, but div still shows!
Why do you want to hide the forms with JavaScript?
Simply do it with PHP:
<?php
if(!isset($_GET['submit1'])) {
?>
//<form> your form HTML here
<?php
} else {
?>
<p>Your submitted data: <?php print_r($_GET); ?></p>
<?php
}
?>
So your forms are only shown if you have NOT submitted one of them. You might have to adjust the parameters if you have multiple forms, for example
if(!isset($_GET['submit1']) && !isset($_GET['submit2'])) {
Edit: If you want to keep your forms after submitting but only hide it, you could do it that way:
<?php
$formsVisible = !isset($_GET['submit1']));
$formsDisplay = $formsVisible ? 'block' : 'none';
?>
<form style="display:<?php echo $formsDisplay; ?>">
<!-- ... --->
</form>
You need to add a DOM ready event:
if($_GET['submit1']) {
echo '<script type="text/javascript">' . "\n";
echo ' $(function() {' . "\n";
echo ' $("#options").hide();' . "\n";
echo ' });' . "\n";
echo '</script>' . "\n";
}
edit
You are getting a parse error because you're using double quotes, so the php parser is reading the dollar sign as a php variable. I would switch to a single quote php syntax to make your life easier:
if($_GET['submit1']){
echo '<script type="text/javascript">
$(function(){
$("#options").css("display","none");
});
</script>';
}
Be sure to wrap your jquery code in the onLoad function $(function(){};

Categories