Jump to content

Variables


.Darkman

Recommended Posts

Hello All,

 

I need some help with variables.

Let me explain what i want with an example.

 

I have 3 files :-

File-A

File-B

File-C

 

I am using a variable called $tdir in File-C.

Its default value is stored in File-B.

 

Now, there is a Form in File-A where i can input a certain value and when i click "Submit", the value will be stored as $tdir in File-B.

From then, this new value will be used in File-A.

 

How to do this ?

Please help.

 

Do let me know if i was not clear.

 

 

Thanks,

 

Link to comment
Share on other sites

I hope you meant that after a value is inserted via File-A into a variable in File-B that it would then be used in "File-C" not "File-A" as you have typed. Is that correct? If so, you are probably in need of using some sort of session handling in order to carry the variables from one page to the next:

 

File-A:

<form name="myform" action="fileB.php" method="post">
Your Value: <input type="text" name="myVar" value="" />
<input type="submit" name="submit" value="Save" />
</form>

 

File-B:

<?php
session_start();

if (isset($_POST['myVar'])) {
  $_SESSION['myVar'] = $_POST['myVar'];
}

echo "<a href=\"fileC.php\">File-C</a>\n";
?>

 

File-C:

<?php
session_start();

if (!isset($_SESSION['myVar'])) {
  echo "You haven't entered a value yet. Go to <a href=\"fileA.php\">The Form</a>\n";
} else {
  echo "You entered <b>$_SESSION[myVar]</b> as your value!";
}
?>

 

Hope this helps.

Link to comment
Share on other sites

Should i use Session only ?

 

Will this not work ?

 

File-A :-

<form name="myform" action="fileB.php" method="post">
Your Value: <input type="text" name="myVar" value="" />
<input type="submit" name="submit" value="Save" />
</form>

 

File-B :-

<?php
if (isset($_POST['submitted'])) 
{
if (empty($_POST['myVar'])) {
$errors[] = '<font color="red">Please entera value.</font>';
} else {
$myVar = $_POST['myVar'];
} 
?>

 

 

File-C :-

<?php
include ('File-B.php');
echo "The variable is $myVar";
?>

 

And, i want a default value to be in the variable. This value will be unchanged till the Form is processed.

How can that be done ?

 

Thanks,

Link to comment
Share on other sites

that wouldnt work, you would get the ouput for file b. So i suggest hidden fields.

 

Create a hidden field containing the POST and make the action FILEC.

 

Also make sure file b has the right isset function.

 

submitted would never work.

 

Page 2:

<?php
if (isset($_POST['myVar'])) 
{
if (empty($_POST['myVar'])) {$errors[] = '<font color="red">Please entera value.</font>';
} else {
$myVar = $_POST['myVar'];
}
?>
<!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=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
<form id="form1" name="form1" method="post" action="">
  <input name="variable" type="hidden" value="<?php echo "" .$_POST['myVar']; ?>" />
</form>
</body>
</html>

 

Page 3:

<?php
echo "The variable is : .$_POST['myVar'];
?>

 

Snooble

Link to comment
Share on other sites

That didn't work

I get this :

Parse error: parse error, expecting `T_STRING' or `T_VARIABLE' or `T_NUM_STRING' in c:\program files\easyphp1-8\www\tblog2\out.php on line 2

 

Anyway, let me tell what i need this for.

I am making a Blog System.

I'd like to have a Theme system for it.

 

So, i'd want to set the Theme's Directory from Admin panel and i will use it in Includes.

for eg :

<?php
include ('themes/$theme_dir/header.php');

// Other Codes here

include ('themes/$theme_dir/footer.php');
?>

 

thanks,

Link to comment
Share on other sites

  • 3 weeks later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.