Page 1 of 1
Variables between php files.
Posted: Mon Aug 06, 2007 4:36 pm
by poteus
Hi... I have a couple of
1/ If I create a variable in file1.php and this one call file2.php, is the variable still declared in the 2nd file?
2/ What function can I use to call another homepage?
Posted: Mon Aug 06, 2007 4:39 pm
by feyd
- Try it.
- Call in what manner? Please explain.
Posted: Mon Aug 06, 2007 4:40 pm
by poteus
Just open a new browser... A link to another URL.
Posted: Mon Aug 06, 2007 4:42 pm
by robshanks
For no.2 try:
Code: Select all
header("location: http://www.gophp5.org");
Posted: Mon Aug 06, 2007 4:52 pm
by poteus
1/ It didnt pass.
2/
Warning: Cannot modify header information - headers already sent by (output started at /home/blahblah/public_html/loaners/addLP.php:6) in /home/blahblah/public_html/loaners/addLP.php on line 46
Posted: Mon Aug 06, 2007 4:55 pm
by feyd
- What code did you try that "didn't pass"?
- Searching on the error should give you plenty of insight into fixing it.
Posted: Mon Aug 06, 2007 5:22 pm
by poteus
1/
Code: Select all
<?php $error_input = 20; ?> /* Any variable */
/* This is just a normal form in HTML that will send that info to addLP.php */
<p align="center" class="style1">Loaner Phones
<p>Adding a new Loaner Phone
<form action="addLP.php" method="post">
<table width="500" border="0">
<tr>
<td width="235">Loaner Phone Internal Number :</td>
<td width="255"><input type="text" name="LPIN" /></td>
</tr>
<tr>
<td>ESN:</td>
<td><input type="text" name="ESN" /></td>
</tr>
<tr>
<td>Model: </td>
<td><input type="text" name="Model" /></td>
</tr>
<tr>
<td>Store: </td>
<td><select name="Store" size="1">
<option value="Select one">Select One</option>
<option value="Dixon">Dixon</option>
<option value="Rochelle">Rochelle</option>
<option value="Oregon">Oregon</option>
<option value="Sterling">Sterling</option>
</select></td>
</tr>
<tr>
<td><input name="submit" type="submit" value="Submit"/></td>
<td> </td>
</tr>
</table>
<p>
</p>
addLP.php
Code: Select all
<?php
// Updating variables
$LPIN = htmlspecialchars($_POST['LPIN']);
$ESN = htmlspecialchars($_POST['ESN']);
$Model = htmlspecialchars($_POST['Model']);
$Store = htmlspecialchars($_POST['Store']);
echo "$error_input"; /* Just testing if my variable was "20" */
$error_input=0;
if (strlen($ESN)!=11) $error_input=1;
if ($Store=="Select one") $error_input=2;
Switch($error_input)
{
case 1: $error_message = 'ESN must have 11 characters.'; break;
case 2: $error_message = 'Select a store.'; break;
}
header("location: http://www.gophp5.org"); /* I would like to come back to the previous file if my conditions are not fulfill */
?>
Posted: Mon Aug 06, 2007 5:30 pm
by feyd
You're posting to that page. PHP is stateless, it has no knowledge of previous requests. If you wish to carry information from one page to another you need to communicate it. This needs to be done via posting, url, cookies, or sessions.
Posted: Mon Aug 06, 2007 5:34 pm
by robshanks
No the variable wouldn't be passed like that. If you had 'include' a second PHP file the variable is available.
The easiest way is to add a hidden field to your form with the value you want to pass:
Code: Select all
<input type="hidden" name="example" id="example" value="20">
Alternatively pass it when you call the second php file:
Code: Select all
<form action="addLP.php?q="<?php echo $example; ?>" method="post">
Then in the second php file add:
Posted: Mon Aug 06, 2007 5:40 pm
by poteus
1/ Answered!
I want to filter the input of my form. Is there anyway to do that without posting to another file?
Posted: Mon Aug 06, 2007 5:45 pm
by feyd
You can post to the same file and process the input locally first.
Posted: Mon Aug 06, 2007 5:47 pm
by poteus
Thx!
You were of great help... I havent program in a while, and I need to get back to this.
Good luck and have fun
