0

I have the most annoying PHP ever encountered. I need to submit the page TWICE before the changes are being made to the page. For example; if you submit the page when you enable a tool, it will show the tools' settings: for these settings to show up you need to submit the page twice. What is wrong with this code?

Another example: enter image description here


CODE:

<?php
if (isset($_POST["submit"]))
{
    $string = '<?php
    $customoptions = ' . $_POST["customoptions"] . ';
    $primarycolor = "' . $_POST["primarycolor"] . '";
    $adminbg = "' . $_POST["adminbg"] . '";
    ?>';

    $fp = fopen("includes/userstyle.php", "w");
    fwrite($fp, $string);
    fclose($fp);
}
?>
   <form action="" name="customopt" method="post">
    <table>
        <tr>
            <td>Panel language</td>
            <td>
                <select onchange="this.options[this.selectedIndex].value && (window.location = this.options[this.selectedIndex].value);">
                    <option><?php echo $lang['chooselanguage']; ?></option>
                    <option value="dashboard.php?lang=en">English</option>
                    <option value="dashboard.php?lang=nl">Dutch</option>
                </select>
            </td>
        </tr>
        <tr>
            <td>Custom Style</td>
            <td>
                <select name="customoptions" id="customoptions">
                    <option <?php echo ($customoptions == true) ? 'selected' : '' ?> value="true">
                        <?php echo $lang['enabled']; ?>
                    </option>
                    <option <?php echo ($customoptions == true) ? 'selected' : '' ?> value="false">
                        <?php echo $lang['disabled']; ?>
                    </option>
                </select>
            </td>
        </tr>
        <?php
        if ($customoptions)
        {
            ?>
            <tr>
                <td>Primary Color</td>
                <td><input name="primarycolor" type="text" id="primarycolor" value="<?php echo $primarycolor; ?>"></td>
            </tr>
            <tr>
                <td>Background Color</td>
                <td><input name="adminbg" type="text" id="adminbg" value="<?php echo $adminbg; ?>"></td>
            </tr>
            <?php
        }
        ?>
    </table>
    <input type="submit" name="submit" value="<?php echo $lang['ok']; ?>">
</form>

PS: I know this isn't a good way to save settings in a php file but this is just a test, it will never go live.

Mia
  • 817
  • 2
  • 8
  • 13
  • 1
    When you define $string remove from inside the string. You are already inside a php tag! – Lelio Faieta Feb 08 '16 at 22:01
  • While I don't understand why you're doing what you're doing, the reason you have to submit it twice is because the first time you submit it runs the script and updates the file. However, you won't see the updated file until the next time you run the script. If that makes any sense. – WheatBeak Feb 08 '16 at 22:03
  • @LelioFaieta That's correct, it needs to write the few strings to a PHP file, that shouldn't be the problem, I'm also using the script on other pages where it works fine, I'm so confused o.o – Mia Feb 08 '16 at 22:04
  • @WheatBeak Weird is, I'm using this exact script on other pages where it works fine. It's just this page which doesn't want to update the first time which is so confusing. There must be something wrong with a line of code but I'm looking for a few hours now and can't find anything.. anything.. – Mia Feb 08 '16 at 22:05
  • Locate the line `include "includes/userstyle.php";` (that must exist, but you are not showing in your question) and place it **under** the `if (isset($_POST["submit"])){}` block; – Steve Feb 08 '16 at 22:27

1 Answers1

0

OK, try to do it has to do with the top that says if (isset($_POST['submit'])) Try and give it a default value if it isn't set. if (!(isset($_POST['submit']))) I'm referrin to the $customoptions variable $primarycolor and $adminbg

Jimmy Canadezo
  • 161
  • 1
  • 10