Jump to content

Variables


arunpatal

Recommended Posts

Hello everybody :happy-04:

 

i have a variable call

 

$header = 1000;

 

in index page.......

 

How can i change the value of this variable from another page....

 

For example

 

i make page setting.php

and there i make a form with editbox and submit button.

now when i change value 1000 to 800 in editbox and click submit then it change the value

of $header variable.....

Link to comment
Share on other sites

Well, You since you're using a form, you can pass the variable via $_POST[]

 

Ok here is setting.php

 

<form method="post" action="catching-var.php">
0. <input type="text" name="name0"/><br/>
1. <input type="text" name="name1"/><br/>
<input type="submit" name="submit"/>
</form>

 

Here is Index.php

 

<?php
$name0 = $_POST['name0'];
$name1 = $_POST['name1'];
echo $name0.'<br/><br/>';
echo $name1.'<br/><br/>';
?>

 

Now i want to save the name0 and name1 value

 

$link = $name0 (value)

$link1 = $name1 (value)

 

because when i come back to this page then it says

 

Notice: Undefined index: name0 in C:\xampp\htdocs\index.php on line 1

 

Notice: Undefined index: name1 in C:\xampp\htdocs\index.php on line 3

 

how can i save these values

Link to comment
Share on other sites

If you are trying to save your setting values so that they are persistent and they apply to all visitors, you need to save the values either to a file or to a database table. Since you haven't described what overall goal you are trying to achieve, you haven't gotten many replies.

Link to comment
Share on other sites

If you are trying to save your setting values so that they are persistent and they apply to all visitors, you need to save the values either to a file or to a database table. Since you haven't described what overall goal you are trying to achieve, you haven't gotten many replies.

 

Ok i need to save these values in config.php file

example

 

<?php

$handy = "nokia";

$computer = "P4";

?>

 

so that i can make

require 'config.php'; in other php files

to use these variables in other files

Edited by arunpatal
Link to comment
Share on other sites

You should use an array so that it all the values are in one place. This would make it easier to loop over the values (they're all in one array) and to make sure you don't overwrite any existing program variables when you include the file.

 

<?php
// use an array so that you know exactly where the values are at and where they will end up when you include them
$config['handy']  = "nokia";
$config['computer'] = "P4";

// write out the $config array to a .php file
$file = 'config.php';
$content = "<?php\n";
foreach($config as $key=>$value){
   $content .= "\$config['$key'] = '$value';\n";
}
$content .= "?>";
file_put_contents($file,$content);

Link to comment
Share on other sites

You should use an array so that it all the values are in one place. This would make it easier to loop over the values (they're all in one array) and to make sure you don't overwrite any existing program variables when you include the file.

 

<?php
// use an array so that you know exactly where the values are at and where they will end up when you include them
$config['handy'] = "nokia";
$config['computer'] = "P4";

// write out the $config array to a .php file
$file = 'config.php';
$content = "<?php\n";
foreach($config as $key=>$value){
$content .= "\$config['$key'] = '$value';\n";
}
$content .= "?>";
file_put_contents($file,$content);

 

 

Little thing..

 

I make a form in index page and my index.php look like this

 

<?php
require 'config.php';

// use an array so that you know exactly where the values are at and where they will end up when you include them
$config['handy'] = "nokia";
$config['computer'] = "P4";
// write out the $config array to a .php file
$file = 'config.php';
$content = "<?php\n";
foreach($config as $key=>$value){
$content .= "\$config['$key'] = '$value';\n";
}
$content .= "?>";
file_put_contents($file,$content);
?>
<form action="index.php" method="post" enctype="multipart/form-data">
<input name="handy" type="text" />
<input name="computer" type="text" />
<input name="Submit" type="button" value="submit" />
</form>
<?php

echo $handy;
?>

 

I want that when i echo $handy then i print nokia...

How can i do this???

Edited by arunpatal
Link to comment
Share on other sites

In your form processing code, you would assign the value you want to $config['handy'] -

 

$config['handy'] = $_POST['handy'];

 

I did it but still not working........

 

Here is index.php code

 

<?php
require 'config.php';
// use an array so that you know exactly where the values are at and where they will end up when you include them
$config['handy'] = $_POST['handy'];
$config['computer'] = $_POST['computer'];
// write out the $config array to a .php file
$file = 'config.php';
$content = "<?php\n";
foreach($config as $key=>$value){
$content .= "\$config['$key'] = '$value';\n";
}
$content .= "?>";
file_put_contents($file,$content);
?>

<form action="index.php" method="POST" enctype="multipart/form-data">
<input name="handy" type="text" />
<input name="computer" type="text" />
<input name="Submit" type="button" value="submit" />
</form>

 

here is output of config.php

 

<?php
$config['handy'] = '';
$config['computer'] = '';
?>

Link to comment
Share on other sites

Hey Kid, that's not form processing code. Form processing code - 1) tests if a form has been submitted, 2) filters and validates the submitted data, 2) then uses that data the way you want.

 

:-\ for me not easy to understand...... if you can just show me the code then i can try to understand it....

I am very new to php

Link to comment
Share on other sites

:-\ for me not easy to understand...... if you can just show me the code then i can try to understand it....

I am very new to php

 

OK now i can submit the value

 

index.php

 

<?php
// use an array so that you know exactly where the values are at and where they will end up when you include them
$config['handy'] = $_POST['handy'];
$config['computer'] = $_POST['computer'];
// write out the $config array to a .php file
$file = 'config.php';
$content = "<?php\n";
foreach($config as $key=>$value){
$content .= "\$config['$key'] = '$value';\n";
}
$content .= "?>";
file_put_contents($file,$content);
?>
<form name="Form1" method="post" action="" enctype="multipart/form-data" target="_self" id="Form1">
<input type="text" id="Editbox1" name="handy" value="">
<input type="text" id="Editbox2" name="computer" value="">
<input type="submit" id="Button1" name="" value="Submit">
</form>
<?php
echo $config['handy']
?>

 

config.php look like this

 

<?php
$config['handy'] = 'nokia';
$config['computer'] = 'P4';
?>

 

now i can echo $config['handy']

 

but is there a way to make config look like this

$handy = "nokia";

$computer = "p4";

 

it just make little easy.... :)

 

 

And thanks a lot for the help also........

The main problem is solved :)

Edited by arunpatal
Link to comment
Share on other sites

This code work just fine now

 

but when i enter index.php page again or refresh it then it shows error massage

 

Notice: Undefined index: handy in C:\xampp\htdocs\index.php on line 5

 

Notice: Undefined index: computer in C:\xampp\htdocs\index.php on line 6

 

These are line 5 and 6

 

$config['handy'] = $_POST['handy'];

$config['computer'] = $_POST['computer'];

 

 

<?php
// use an array so that you know exactly where the values are at and where they will end up when you include them

$config['handy'] = $_POST['handy'];
$config['computer'] = $_POST['computer'];
// write out the $config array to a .php file
$file = 'config.php';
$content = "<?php\n";
foreach($config as $key=>$value){
    $content .= "\$config['$key'] = '$value';\n";
}
$content .= "?>";
file_put_contents($file,$content);
?>

<form name="Form1" method="post" action="" enctype="multipart/form-data" target="_self" id="Form1">
<input type="text" id="Editbox1"  name="handy" value="1">
<input type="text" id="Editbox2"  name="computer" value="1">
<input type="submit" id="Button1" name="" value="Submit">
</form>
<?php
echo $config['handy'];
echo $config['computer'];
?>

Link to comment
Share on other sites

That's because the $_POST array has not been sent/created yet by your form.

 

Verify its existence by checking for form submission:

 

if (isset($_POST['submit'])) {
   // create your variables

   $config['handy'] = $_POST['handy'];
   $config['computer'] = $_POST['computer'];

   // etc
}

 

And add submit to the name of your submit button:

 

<input type="submit" id="Button1" name="submit" value="Submit">

Link to comment
Share on other sites

That's because the $_POST array has not been sent/created yet by your form.

 

Verify its existence by checking for form submission:

 

if (isset($_POST['submit'])) {
// create your variables

$config['handy'] = $_POST['handy'];
$config['computer'] = $_POST['computer'];

// etc
}

 

And add submit to the name of your submit button:

 

<input type="submit" id="Button1" name="submit" value="Submit">

 

Thanks.........

 

work perfectly :happy-04:

Link to comment
Share on other sites

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.