通过表单提交更改PHP变量/值

时间:2018-07-12 21:26:22

标签: php html mysql forms

好,所以我基本上想更改html页面上的某些值。喜欢我的社区名称。我输入值,然后单击提交按钮。我希望那个小按钮可以根据我在表单上所做的更改来更改变量的值(如果可以的话)。如果有帮助,我正在使用Bootstrap(最新版本)。

config.php

$communityName = "My Community";
$communityLinks = array("Home", "Store", "Forum");

functionality.php

if (isset($_POST['communityName'])) {
   $communityName = ($_POST['communityName']);
}
if (isset($_POST['communityLinks'])) {
   $communityLinks = str_replace("\r\n", '","',($_POST['communityLinks']));
}

index.php

<?php
include ("config.php");
include ("functionality.php");
?>
<div id="accordion" class="container">
    <div class="card b-light-shadow">
        <div class="card-header" id="headingOne">
            <h5 class="mb-0">
                <button class="accord-btn btn btn-link" data-toggle="collapse" data-target="#collapseOne" aria-expanded="true" aria-controls="collapseOne">
                Basics
                </button>
            </h5>
        </div>
        <div id="collapseOne" class="collapse" aria-labelledby="headingOne" data-parent="#accordion">
            <div class="card-body">
                <form action="" method="post">
                    <div class="form-group">
                        <label for="exampleFormControlTextarea1">Community Name</label>
                        <input class="form-control" id="exampleFormControlTextarea1" rows="1" name="communityName" placeholder="Community Name" value="<?= $communityName ?>">
                    </div>
                    <div class="form-group">
                        <label for="exampleFormControlTextarea1">Community Lins</label>
                        <p class="form-description">Change your links here.</p>
                        <textarea class="form-control" rows="3" name="communityLinks" type="text"><?php $i = 0; $len = count($communityLinks); foreach ($communityLinks as $cL) { echo $cL; if ($i != $len - 1) { echo "&#013;"; } $i++;  } ?></textarea>
                    </div>
                </form>
            </div>
        </div>
    </div>
    <div class="row" style="margin-top: 3em;">
        <div class="container">
           <button class="subm-btn btn btn-link" type="submit" name="action">Save</button>
        </div>
    </div>
</div>

我在这里做错什么了吗?我可能已经花了7个多小时来为此进行研究,这将是我的最后一个想法。无论您认为哪种方式,我都想听听!如果您需要更多信息,请告诉我!谢谢您的帮助!

2 个答案:

答案 0 :(得分:0)

将您的“提交”按钮放入表单中。现在它什么也没做。

答案 1 :(得分:0)

看来您已经很接近了。但是,您并没有使用$communityName来显示HTML代码。

为此,只需替换一下:

<label for="exampleFormControlTextarea1">Community Name</label>

与此:

<label for="exampleFormControlTextarea1"><?= $communityName ?></label>

现在,如果您成功发布到此页面,则名称应更改。

接下来,如@ user3783243所述。 str_replace将为您提供一个字符串,但是您将$communityLinks用作数组,因此需要一个链接数组。您应该使用 explode() 函数从用户输入中获取数组。

类似的事情可能起作用:

$communityLinks = explode("\r\n", $_POST['communityName']);