bloodgoat Posted March 22, 2009 Share Posted March 22, 2009 Excuse me, I'm extremely drowsy as it is (was at work all night, haven't slept yet). I'm trying to recreate an old script based purely off of foggy memory (it was from... oh, I don't know, 5-6 years ago, before I stopped scripting). As you can imagine, it's proving quite difficult for me to remember. I still understand the theory behind it but I'm getting boned when executing it. Basically: You have an index file chalk full of input fields. This fields all pull variables from a central variable file. You can freely edit these fields and fwrite to change their value. Simple enough, right? Well, shed some help. Just something I jotted up in the last 10-15 minutes... here are my three files: INDEX.PHP <?php include("vars.php"); $template = <<< html <form action="write.php" method="post"> <input type="text" value="$varone" name="input1"><br> <input type="text" value="$vartwo" name="input2"> <input type="submit" value="Make Changes"> html; echo $template; ?> VARS.PHP <?php $varone = "variable one"; $vartwo = "variable two"; ?> WRITE.PHP <?php include("index.php"); $array_vars = array( "$varone = ".$_POST['input1']."", "$vartwo = ".$_POST['input2']."" ); foreach($array_vars as $var){ $write .= $var.";\n"; } $handle = fopen("vars.php", "w+"); fwrite($handle, "<?php\n\n"); fwrite($handle, "$write"); fwrite($handle, "\n?>"); fclose($handle); echo "<meta http-equiv=\"refresh\" content=\"0;index.php\">"; ?> My problem: let's say I change the value in the first input field to "num one" and the value in the second input field to "num two", and then hit submit. My vars.php turns to this: <?php variable one = num one; variable two = num two; ?> Nevermind the fact the variables don't maintain their quotations (which is a huge problem in itself), but the previous value of the variable is then made the new value of the variable (except it's not even technically a variable because it doesn't have a $). Halp plz Link to comment https://forums.phpfreaks.com/topic/150618-solved-fwriting-through-input-fields/ Share on other sites More sharing options...
trq Posted March 22, 2009 Share Posted March 22, 2009 <?php include("index.php"); $write = "\$varone = '{$_POST['input1']}\n\$vartwo = '{$_POST['input2']}'"; $handle = fopen("vars.php", "w+"); fwrite($handle, "<?php\n\n"); fwrite($handle, $write); fwrite($handle, "\n?>"); fclose($handle); echo "<meta http-equiv=\"refresh\" content=\"0;index.php\">"; ?> Link to comment https://forums.phpfreaks.com/topic/150618-solved-fwriting-through-input-fields/#findComment-791162 Share on other sites More sharing options...
bloodgoat Posted March 22, 2009 Author Share Posted March 22, 2009 I'll try this, and thank you for the new code, but for the benefit of my own learning experience, can you explain what you did and why? I can tell I was putting my backslashes in the wrong spot, you totally refreshed my memory in that regard, but this time around I was taking a new approach to the actual writing. Is it not possible with a foreach() loop through an array? Or is it just not as feasible? Link to comment https://forums.phpfreaks.com/topic/150618-solved-fwriting-through-input-fields/#findComment-791167 Share on other sites More sharing options...
trq Posted March 22, 2009 Share Posted March 22, 2009 Its pretty simple. There was no need for any loop. $write = "\$varone = '{$_POST['input1']}\n\$vartwo = '{$_POST['input2']}'"; Backslashes can be used to escape chars. So we use a backslash before the $ so that php know its part of the string (and won't try to interpolate it as a variable. As for the rest, complext variables (ie arrays) should be within {} when being interpolated in a string, and I simply wrapped single quotes around the parts that need it. Link to comment https://forums.phpfreaks.com/topic/150618-solved-fwriting-through-input-fields/#findComment-791171 Share on other sites More sharing options...
bloodgoat Posted March 22, 2009 Author Share Posted March 22, 2009 All the best, then! Thanks for a quick response. I'm going to get some much needed shut-eye and then complete my script when I wake up now that I have the bare-bones complete. Link to comment https://forums.phpfreaks.com/topic/150618-solved-fwriting-through-input-fields/#findComment-791172 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.