include php file many times - php

I want a include a .php file to another one. But I want to do it many times. For each one I am going to pass concrete parameters. I want to print the result page on load.
for example imagin I have a html like this:
<html>
<body>
<div id="#friend1">
Name: <input/>
Age: <input />
</div>
<div id="#friend2">
Name: <input/>
Age: <input />
</div>
<button>print</button>
</body>
</html>
I have another file wich prints friends info in a cool wai like:
<html>
<body>
<div>
Yo are my friend<?php echo $_SESSION['friend']?> you are <?php echo $_SESSION['age']?>
</div>
</body>
</html>
I want to include this file with concrete info in order to print it. I have been trying with include o require with no succeed. Just one of them is shown. Parameters are passed correctly and I don-t know how to make it.
Any Ideas? Thank you, very much!
I'll try to be more specific:
<?php
session_start();
?>
<!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="es" lang="es">
<meta charset="utf-8">
<head>
</head>
<script type="text/javascript">
<?php
require_once ("jqdirectPrint.js");
?>
</script>
<body onload="printLoad();">
<?php
for ($i = 0; $i < sizeof($_SESSION['friendList']); $i++) {
$_SESSION['friend']=$_SESSION['friendList'][$i];
require 'friendInfo.php';
}
?>
</body>
</html>
friendInfor.php is just a html form which shows info for concrete friend. But just the first one is shown. I want to show same form for each elemnt on the list. It just contains tags.

Hobo Sapiens was right I had some extra tags on my friendInfo.php so I removed them and it worked fine. So thank you for your help. requireallows you to introduce or load same file many times.
Still waitting for some nice PHP manuals to check. ;)

Related

On a loop: fgets keeps reading only one line that keeps getting used in the following loop

My goal from my code (using only HTML and PHP) is to provide a page for choosing the type of question (checkbox...) with the number of inputs and display the result in a different page by reading the 3 files that has been already saved in 3 main files (file for questions, file for number of inputs, file for options content), my idea is almost.
Here's my code from the displaying page :
<!doctype html>
<html>
<head>
<title>
form du reponse
</title>
</head>
<body>
<?php
//3 files
$k=fopen("choix multiples.txt","a+");//questions
$nn=fopen("nombre.txt","a+");//number of inputs
$no=fopen("les_options_multiples.txt","a+");//options content
//from here it only reads the first line of $nn and it keeps using it on the last loop
for($lol=fgets($nn);!feof($nn);$lol=fgets($nn))
{
for($questioncm=fgets($k);!feof($k);$questioncm=fgets($k))
{
echo "$questioncm :<br/>";
for($x=1;$x<=$lol;$x++)
{
echo fgets($no);
?>
<form action="" method="POST">
<div>
<input name="haha" type="radio" value="hahaha">
</div>
<?php
}
?>
</form>
<?php
}
}
</form>
</body>
</html>

Passing session variables to more than 3 page and back in php

The first page (inputform1test.php)
<?php require_once('Connections/Project.php'); ?>
<?php
if (!isset($_SESSION)) {
session_start();
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Input (Test)</title>
</head>
<body>
<form id="form1" name="form1" method="post" action="inputdisplaytest.php">
<p>Name
<input type="text" name="name" id="textfield" />
</p>
<p>Text
<input type="text" name="text" id="textfield2" />
</p>
<p>
<input type="submit" name="button" id="button" value="Submit" />
</p>
</form>
</body>
</html>
Second page(inputdisplaytest.php)
To test if it's working in 2nd page(inputdisplaytest.php)
<?php include('Connections/Project.php'); ?>
<?php
if (!isset($_SESSION)) {
session_start();
}
$_SESSION['name']= $_POST['name'];
$_SESSION['text']= $_POST['text'];
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Display Input (Test)</title>
</head>
<body>
<table width="200" border="0">
<tr>
<td><?php echo $_SESSION['name']?></td>
<td><?php echo $_SESSION['text']?></td>
</tr>
</table>
inputdisplaytest_2.php
</body>
</html>
Third page(inputdisplaytest2.php)
This is the part where I got the error
<?php include('Connections/Project.php'); ?>
<?php
if (!isset($_SESSION)) {
session_start();
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Display Input (Test)</title>
</head>
<body>
<table width="200" border="0">
<tr>
<td><?php echo $_SESSION['name']?></td>
<td><?php echo $_SESSION['text']?></td>
</tr>
</table>
<p>inputdisplaytest.php</p>
</body>
</html>
I clicked to see if it's still working on 2nd page. (Which is not working)
I got the undefinex index problem when I go to the hyperlink and "Document Expired" when I clicked back button via browser.
How do I get the session variables back/not expire?
Any help would be appreciated.
You don't need the
if (!isset($_SESSION)) {
session_start();
}
just use session_start() at the beggining of your script. Even before those includes.
You can't print anything before the session_start. Check your logs for errors.
I believe the reason you're finding this error is because you are POSTing the form data. When you POST the form data, it is sent when you hit Submit. Going back to the form will give you the message to reload the form data. If you change it to GET, your form data should stick.
Remove if statements that you're using to check if session is set, because in your case the session_start() is never being executed, since the $_SESSION array is always set by PHP unless you perform unset($_SESSION) which is deprecated, this will unset the whole $_SESSION with unset($_SESSION) as this will disable the registering of session variables through the $_SESSION superglobal array. So:
if(!isset($_SESSION)){
session_start();
}
becomes:
session_start();
this resolves the problem of undefined index error.
To resolve the browser back button error when navigating back to the page processing your form, you have to put:
session_cache_limiter('private_no_expire');
before session_start() on second.php file, this line of code avoid "Page Has Expired" warnings, you can find more explanation here avoid "Page Has Expired" warnings.
Finally, after session_start() on second.php file, add the following code:
if(isset($_POST['button'])){
$_SESSION['name']= $_POST['name'];
$_SESSION['text']= $_POST['text'];
}
to check if the form was submitted or not, if yes, you can process the data and display the page, else you just display the page using the perviously stored data on your session.
I have had issues with this myself, where the session variables are not passed on, and recently came across the answer. I am sorry that I cannot credit the person who answered this in a previous post because I cannot find the post again. Simply put, sometimes, especially on shared hosting, the sessions are not stored in the right place. You can fix this by forcing the sessions to be saved in a particular spot. My solution was:
ini_set('session.save_path',realpath(dirname($_SERVER['DOCUMENT_ROOT']) . '/session'));
My session file is in the domain's root folder.
I then start the session:
session_start();
And then I make sure that the person is signed in:
if(isset($_SESSION["your_variable"])) {
$your_variable = $_SESSION["your_variable"];
//add any other session variables in the same manner
//be sure to rename "your_variable" with the name of your variable
}else{
header('Location: signin.php');
exit;
}
The header redirect at the bottom is where the person is going if they are not signed in or the session variable is not found, so that the session variables are passed to this page. Note the exit, it keeps your code safer. So altogether my first lines of code are:
ini_set('session.save_path',realpath(dirname($_SERVER['DOCUMENT_ROOT']) . `'/session'));`
session_start();
if(isset($_SESSION["your_variable"])) {
$your_variable = $_SESSION["your_variable"];
}else{
header('Location: signin.php');// or wherever your sign in happens to be
exit;
}
I hope this helps.

How to pass values from a post and extract with GET

I have three pages One with HTML with the form (input variable) and its going to POST the variable to linkslvl2.php . With my third page i want to use the GET function and retrieve the variables from linkslvl2.php. When i click on the process i want it to automatically process linkslvl2 and print linkslvl3 statement. I'm stumped on how to pass it the variables along.
<!doctype html>
<html>
<head>
</head>
<body>
<h1 style="text-align:center;"> Super Hero</h1>
<form action="linklvltwo.php" method="post">
Please Enter a name:
Enter A super Hero Name:
Enter your weakness:
<!doctype html>
<html>
<head>
</head>
<body>
<?php
$name = $_POST['namep'];
$superHero = $_POST['superhero'];
$weakness = $_POST['weakness'];
?>
</body>
</html>
<!doctype html>
<html>
<head>
</head>
<body>
<?php
$person = $_GET['name'];
$chosenhero = $_GET['superHero'];
$chosenweakness = $_GET['weakness'];
print"
<p>$person you have chosen $chosenhero as you super hero, and your weakness $chosenweakness</p>
"
?>
</body>
</html>
perhaps you can try using $_REQUEST which gives you the parameters wether they are posted or "getted".
I think you can do this type of works in two way: 1) you can use db, file, session to save the value after submitting each form and use an id to indentify/get the previous values. 2) you can Submit the values of each steps via

How to change dropdown selection into typeahead which using mysql database?

I want to change this code (dropdown select course) into typeahead so i just need to type a few word and then, it will suggest.
<TD width="112"><INPUT type="checkbox" name="chk[]"/></TD>
<TD width="472">
<SELECT name="course[]">
<?php
$sql = "select * from tt_course";
$result = mysql_query($sql);
while($rs = mysql_fetch_assoc($result))
{
$select="";
echo "<option value=\"".$rs['course_id']."\" ".$select.">".$rs['course_name']."</option>";
}
?>
</SELECT>
</TD>
Have a look at http://jqueryui.com/demos/autocomplete/
Its simple and easy to setup and will serve the purpose. You can create an array of values and provide it to Autocomplete or you can make it call your php page and parse the returned JSON data.
As promised, here is an example code to get you going. Please note that this code uses a local javascript variable to prepare suggestions for the autocomplete box. You can easily update that to look for JSON data from your php page and i could easily have made you even that but purpose is to help you learn so i think this way it would be better for you so that you can see how it works and then update it to your needs accordingly. Here you go, you can simply copy paste this code and see it running
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/black-tie/jquery-ui.css" type="text/css" />
<script src='http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js'></script>
<script src='http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.2/jquery-ui.min.js'></script>
<script>
$(document).ready(function() {
$( "#AC" ).autocomplete({
source: programmingLang
});
});
var programmingLang = ["ActionScript","AppleScript","Asp","BASIC","C","C++",
"Clojure","COBOL","ColdFusion","Erlang","Fortran","Groovy","Haskell",
"Java","JavaScript","Lisp","Perl","PHP","Python","Ruby","Scala","Scheme"];
</script>
</head>
<body>
<input type=text name="AC" id="AC">
</body>
</html>

Checking if URL has been tampered with

I am trying to ensure the parameter from a GET method of page 2 (which displays the result) will not be changed by others in the URL (the variables behind the question mark) by making sure users clicked the submit button on page 1 (which contains the form).
The professor ask us to use isset() to implement this requirement
Here's the login page: (page1.php)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional ...> <html
xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Login
Page</title> </head>
<body>
<label>User ID:</label>
<input type="text" name="uid">
<br />
<input type="submit" value="SubmitCredentials" name="SubmitCredentials"/>
<br />
</form> </body> </html>
And this is the second page: (page2.php)
<?php
// get the data from the form
$uid = $_GET['uid'];
if(!isset($_GET['SubmitCredentials'])){
include('error.html');
exit();
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional
...>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>HELLO</title>
</head>
<body>
<label>Hello,</label>
<span><?php echo $uid; ?></span><br />
</body>
</html>
However, the if statement doesn't seem to be working. It's supposed to forward the page to error.html when I change the variable uid in the URL (e.g. when I change the URL to http://localhost/1/page2.php?uid=admin) but it's not. How can I fix it?
PS: a relevant tutorial I found on google>>>
http://www.homeandlearn.co.uk/php/php4p7.html
Try:
if(!isset($_GET['SubmitCredentials'])){
header('Location: error.html');
}
Isset is not safe on Arrays. Try comparing with the ===operator instead:
if(!($_GET['SubmitCredentials'] === 'SubmitCredentials')) {
include('error.html');
exit();
}
Page #1:
<form method="GET" action="...">
<input>
<input>
<input>
</form>
Submit URL:
http://localhost/1/page2.php?SubmitCredentials=SubmitCredentials&uid=admin&i_like_php=yes&...

Categories