PHP - passing variable from one PHP file to another PHP file - php

I am new to PHP and trying to pass variable from one page to another page. Initially, I have a HTML page which contains the frames as below.
<!DOCTYPE html>
<html>
<frameset cols="70%,*">
<frame src="index.php">
<frame src="slider.php">
</frameset>
</html>
As seen above, I have 2 PHP pages, in which am trying to send some values from index.php file to my slider.php file. My index.php file is as below.
<?php
$names = file('demo.csv');
$page = $_GET['page'];
$pagedResults = new Paginated($names, 20, $page);
$handle = fopen('demo.csv', 'r');
if (($data = fgetcsv($handle, 1000, ',')) !== FALSE)
{
}
echo "<table border='3' bgcolor='#dceba9' style='float:center; margin:50'>";
echo '<tr><th>'.implode('</th><th>', $data).'</th></tr>';
while ( $row = $pagedResults->fetchPagedRow())
{
echo "<tr><td>";
$row1 = str_replace( ',', "</td><td>", $row );
echo $row1;
echo "</td></tr>";
}
fclose($handle);
echo "</table>";
//important to set the strategy to be used before a call to fetchPagedNavigation
$pagedResults->setLayout(new DoubleBarLayout());
echo $pagedResults->fetchPagedNavigation();
?>
<form method="get" action="slider.php">
<input type="hidden" name="totalcolumns" value="3">
<input type="submit">
</form>
This is my slider.php file.
<?php
$totalcolumns = $_GET['totalcolumns'];
echo "My next value should get printed";
echo $totalcolumns;
?>
<input type="text" data-slider="true" data-slider-range="100,500" data-slider-step="100">
</html>
As seen above, I am trying to retrieve the value with the name "totalcolumns". However, I am not able to retrieve the value in my slider.php file. I also tried using SESSION as suggested in this link, but no luck. Can someone please let me know what am doing wrong?

You should be able to use the $_SESSION. This is:
$_SESSION['totalcolumns'] = $columns --> your value here in the first script
your value will be stored in the $columns variable in the second
$columns = $_SESSION['totalcolumns']
You can also review the require or include functions. These functions make one file depend on another one, it is as if you directly paste one file on the other.
It is not a good practice to pass variables with these functions. You should use Session
http://php.net/manual/en/function.require.php
Btw, don't use framesets

You should use sessions and you should not use html framesets or iframes, it is a bad practice. If you don't want to reload the whole page by any change, you should use javascript.

You can use $_REQUEST instead of $_GET or just it as:
<?php
if(array_key_exists('totalcolumns', $_GET)) {
$totalcolumns = $_GET['totalcolumns'];
echo "My next value should get printed";
echo $totalcolumns;
?>
may this help you

i'd id the frames first, removing the src for the second one
<!DOCTYPE html>
<html>
<frameset cols="70%,*">
<frame src="index.php" id="f1">
<frame src="" id="f2">
</frameset>
</html>
then change the index.php adding this piece of code at the end
<script>
parent.frames['f2'].location.href="slider.php?totalcolumns=3";
</script>
or perhaps if you have the totalcolumns in your php
<script>
parent.frames['f2'].location.href="slider.php?totalcolumns=<?php echo $totalcolumns;?>";
</script>

Related

How to include a seperate header file?

Hey so I am currently working on an html/php page.
I have a header file that I am supposed to include in my page but for some reason it's not working and I have no idea, I've done the same thing for all my other pages and it has worked so I don't know why all of a sudden its not working.
<?php
include 'header.php';
?>
<center>
<p>
This page utilizes several postgreSQL method calls. Such as pg_connect(),
pg_query(), and pg_fetch_result().
</p>
<!-- setup the table -->
<table border="1" width="75%">
<tr><th width="50%">Make</th><th width="15%">Model</th><th width="20%">Year</th><th width="15%">MSRP</th></tr>
<?php
$output = ""; //Set up a variable to store the output of the loop
//connect
$conn = pg_connect("host=127.0.0.1 dbname=slotegraafd_db user=slotegraafd password=100658347" );
//issue the query
$sql = "SELECT automobiles.make, automobiles.model, automobiles.year, automobiles.msrp
FROM automobiles
ORDER BY automobiles.year ASC";
$result = pg_query($conn, $sql);
$records = pg_num_rows($result);
//generate the table
for($i = 0; $i < $records; $i++){ //loop through all of the retrieved records and add to the output variable
$output .= "\n\t<tr>\n\t\t<td>".pg_fetch_result($result, $i, "Make")."</td>";
$output .= "\n\t\t<td>".pg_fetch_result($result, $i, "Model")."</td>";
$output .= "\n\t\t<td>".pg_fetch_result($result, $i, "Year")."</td>";
$output .= "\n\t\t<td>".pg_fetch_result($result, $i, "msrp")."</td>\n\t</tr>";
}
echo $output; //display the output
?>
</table>
<!-- end the table -->
</center>
</body>
</html>
This is my full set of code with the include statement. There is no opening html or body tag because it is in the header file therefore I am not supposed to add it in this page.
Anyway any help?
Thanks.
Make sure that the file path is correct. If this new file is in a folder, then you need to change your include statement.
For example, if this new file is in a folder called foo, you could say: include '/foo/header.php
Or you can use a relative path: include ../header.php
Read up on file paths if needed.
It should work just the way your code looks like. Maybe your path of header.php isnt correct, or the content can't be displayed / executed so it shows nothing.

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>';
;

What method should I use for later set a variable and then echo the page title

I'm trying to avoid to query my database twice: for set <title> attribute and also for echo the page title. I want to query it just one time:
Example:
<html>
<head>
<?php
// how should I use here ob_start() ? Is there any other possible way to achieve this?
$title = "";
echo '<title>', $title, '</title>'; // this should be in the end <title>value of $row['page_title']</title>
?>
</head>
<body>
<?php
$sql = $mysqli->query("MY QUERY");
$row = $sql->fetch_assoc();
$title = $row['page_title']; // I want that this assignment to set the variable in the top
// I know that for this job I can use ob_start() but I didn't used it until now
// and I will really appreciate any help from you.
?>
<h1><?php echo $title; ?></h1>
</body>
</html>
I know that I can do the query before echo the title attribute but I don't want to do it like that. Do you have any suggestion? or can you show me how to use that ob_start() / ob_clean() functions?
Thank you!
Shift the query to the top of the code and reuse the variables!
<?php
$sql = $mysqli->query("MY QUERY");
$row = $sql->fetch_assoc();
$title = $row['page_title'];
?>
<html>
<head>
<?php echo '<title>', $title, '</title>'; ?>
</head>
<body>
<h1><?php echo $title; ?></h1>
</body>
</html>
ob_start();, in usual sense, won't help you have. However, you can use ugly solution like this:
ob_start();
echo '
<html>
<head>
<title>{title}</title>
</head>
<body>
';
$header = ob_get_clean();
// and then, when you know your title
echo str_replace('{title}', $known_title, $header);
But I strongly recommend taking another approach. You need to choose and fill the template after all data has been gathered and you know all details about which template you want. If you start echoing different parts preemptively, you will get more and more troubles. Now you need to change title for some pages. Then you will need to add css file for specific page and you will have to do that ugly business again. Why not do it the right way?

$_GET variable in php

<?php
$conn = oci_connect('usr', 'pass', 'host');
$instance_status="command1";
$spacecheck="command2";
$log_apply="command3";
$command=$_GET['name'];
echo $command;
$stid = oci_parse($conn, $command);
--some code--
?>
My HTML Page:
<html>
<title>Status Check</title>
<body>
<b>Spacecheck</b>
<b>Log Application Status</b>
<b>Database Status</b>
</body>
</html>
The above is my code, I intend to assign to $command, the value from the href variable through $_GET. But, when I test this code, $command is not being assigned the value of the variable from $_GET, rather the name of the variable is simply assigned to $command.
Eg, If I click on this:
Spacecheck
This should assign the VALUE of $spacecheck to $command, which is not happening. $command returns '$spacecheck'
How do I do this variable assignment?
You are simply writing $spacecheck. What you need to do is jump inside PHP tags and echo the variable values. Like so:
Spacecheck
or use the php echo shortcut:
Spacecheck
See the difference?
Good luck.
try going the other way around, I mean print from php:
<?php
echo '<b>Spacecheck</b>';
echo '<b>Log Application Status</b>';
echo '<b>Database Status</b>';
?>
You must use PHP open and close tags in order to place PHP code in your HTML page among other things. Try this link:
http://www.php.net/manual/en/tutorial.firstpage.php
Your HTML actually contains the literal string '$spacecheck' in the URL. $variables are only parsed in sections between tags, not in plain HTML.
Try this in your HTML file (which should be called .php) instead:
<?php
$spacecheck = 'foobar'; // (some dummy values)
$log_apply = 'nope';
$instance_status = 'idle';
print("<html>
<title>Status Check</title>
<body>
<b><a href='oraData.php?name=$spacecheck'>Spacecheck</a></b>
<b><a href='oraData.php?name=$log_apply'>Log Application Status</a></b>
<b><a href='oraData.php?name=$instance_status'>Database Status</a></b>
</body>
</html>");
?>

Passing the name of the link when clicked

I have the following code
cholera
I want to pass cholera to process.html onClick. Is there any way to do that in HTML?
If no, PHP scripts are also most welcome.
In pure HTML, only by pre-populating the link with the correct value:
cholera
for anything that fetches the link's contents automatically, you would have to use JavaScript. This is comparably easy to do in jQuery. (Update: #James M presents a simple and nice non-jQuery solution in his answer.)
On the receiving end, though, you are going to need some kind of server language (or JavaScript) to do anything with the passed argument.
cholera
index.php
<html>
<body>
<?php
$values = array('cholera', 'chlamydia'); // dynamic
foreach($values as $value) echo '' . $value . '' . '<br />';
?>
</body>
</html>
process.php
<html>
<body>
<?php
$value = !isset($_GET['value']) ? 'none' : $_GET['value'];
?>
Value received: <?php echo $value; ?>
</body>
</html>

Categories