arunpatal Posted November 22, 2012 Share Posted November 22, 2012 Hello everybody 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..... Quote Link to comment https://forums.phpfreaks.com/topic/271050-variables/ Share on other sites More sharing options...
arunpatal Posted November 22, 2012 Author Share Posted November 22, 2012 This is the code of index page <?php $name = "200px"; ?> <table border="1" bordercolor="#999999" width="<?php echo $name ?>"> <td> HELLO </td> </table> How can i change value of $name which is 200px to 500px from another page via form ??? Quote Link to comment https://forums.phpfreaks.com/topic/271050-variables/#findComment-1394516 Share on other sites More sharing options...
devWhiz Posted November 22, 2012 Share Posted November 22, 2012 Well, You since you're using a form, you can pass the variable via $_POST[] Quote Link to comment https://forums.phpfreaks.com/topic/271050-variables/#findComment-1394520 Share on other sites More sharing options...
arunpatal Posted November 22, 2012 Author Share Posted November 22, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/271050-variables/#findComment-1394526 Share on other sites More sharing options...
Muddy_Funster Posted November 23, 2012 Share Posted November 23, 2012 lookup how to use sessions Quote Link to comment https://forums.phpfreaks.com/topic/271050-variables/#findComment-1394573 Share on other sites More sharing options...
PFMaBiSmAd Posted November 23, 2012 Share Posted November 23, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/271050-variables/#findComment-1394606 Share on other sites More sharing options...
arunpatal Posted November 23, 2012 Author Share Posted November 23, 2012 (edited) 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 November 23, 2012 by arunpatal Quote Link to comment https://forums.phpfreaks.com/topic/271050-variables/#findComment-1394622 Share on other sites More sharing options...
PFMaBiSmAd Posted November 23, 2012 Share Posted November 23, 2012 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); Quote Link to comment https://forums.phpfreaks.com/topic/271050-variables/#findComment-1394625 Share on other sites More sharing options...
arunpatal Posted November 23, 2012 Author Share Posted November 23, 2012 (edited) 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 November 23, 2012 by arunpatal Quote Link to comment https://forums.phpfreaks.com/topic/271050-variables/#findComment-1394626 Share on other sites More sharing options...
arunpatal Posted November 23, 2012 Author Share Posted November 23, 2012 (edited) Sorry.... again... i want that when i type in input textfield samsung then the variable change into samsung like this $handy = samsung in config.php file Edited November 23, 2012 by arunpatal Quote Link to comment https://forums.phpfreaks.com/topic/271050-variables/#findComment-1394628 Share on other sites More sharing options...
PFMaBiSmAd Posted November 23, 2012 Share Posted November 23, 2012 In your form processing code, you would assign the value you want to $config['handy'] - $config['handy'] = $_POST['handy']; Quote Link to comment https://forums.phpfreaks.com/topic/271050-variables/#findComment-1394633 Share on other sites More sharing options...
arunpatal Posted November 23, 2012 Author Share Posted November 23, 2012 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'] = ''; ?> Quote Link to comment https://forums.phpfreaks.com/topic/271050-variables/#findComment-1394635 Share on other sites More sharing options...
PFMaBiSmAd Posted November 23, 2012 Share Posted November 23, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/271050-variables/#findComment-1394638 Share on other sites More sharing options...
arunpatal Posted November 23, 2012 Author Share Posted November 23, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/271050-variables/#findComment-1394643 Share on other sites More sharing options...
arunpatal Posted November 23, 2012 Author Share Posted November 23, 2012 (edited) :-\ 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 November 23, 2012 by arunpatal Quote Link to comment https://forums.phpfreaks.com/topic/271050-variables/#findComment-1394646 Share on other sites More sharing options...
arunpatal Posted November 23, 2012 Author Share Posted November 23, 2012 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']; ?> Quote Link to comment https://forums.phpfreaks.com/topic/271050-variables/#findComment-1394656 Share on other sites More sharing options...
mrMarcus Posted November 23, 2012 Share Posted November 23, 2012 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"> Quote Link to comment https://forums.phpfreaks.com/topic/271050-variables/#findComment-1394662 Share on other sites More sharing options...
arunpatal Posted November 23, 2012 Author Share Posted November 23, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/271050-variables/#findComment-1394664 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.