Variables between php files.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
poteus
Forum Newbie
Posts: 10
Joined: Sat Aug 04, 2007 12:24 pm

Variables between php files.

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

  1. Try it.
  2. Call in what manner? Please explain.
poteus
Forum Newbie
Posts: 10
Joined: Sat Aug 04, 2007 12:24 pm

Post by poteus »

Just open a new browser... A link to another URL.
User avatar
robshanks
Forum Commoner
Posts: 32
Joined: Sun Aug 05, 2007 9:27 pm
Location: Hull, UK

Post by robshanks »

For no.2 try:

Code: Select all

header("location: http://www.gophp5.org");
poteus
Forum Newbie
Posts: 10
Joined: Sat Aug 04, 2007 12:24 pm

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

  1. What code did you try that "didn't pass"?
  2. Searching on the error should give you plenty of insight into fixing it.
poteus
Forum Newbie
Posts: 10
Joined: Sat Aug 04, 2007 12:24 pm

Post 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>&nbsp;</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 */


?>
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
robshanks
Forum Commoner
Posts: 32
Joined: Sun Aug 05, 2007 9:27 pm
Location: Hull, UK

Post 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:

Code: Select all

$example=$_GET("q");
poteus
Forum Newbie
Posts: 10
Joined: Sat Aug 04, 2007 12:24 pm

Post by poteus »

1/ Answered! :o

I want to filter the input of my form. Is there anyway to do that without posting to another file?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

You can post to the same file and process the input locally first.
poteus
Forum Newbie
Posts: 10
Joined: Sat Aug 04, 2007 12:24 pm

Post by poteus »

Thx! 8)

You were of great help... I havent program in a while, and I need to get back to this.

Good luck and have fun :twisted:
Post Reply