-4

liデータが変更されていないというだけで、コードの送信に問題があります。コードは次のとおりです。

<?php
$theme1 = business;
$theme2 = modern;
$theme3 = web2;
if(isset($_POST['style']))
{setcookie('style', $_POST['style'], time()+(60*60*24*1000));
$style=$_POST['style'];}
elseif(isset($_COOKIE['style']))
{$style=$_COOKIE['style'];}
else
{$style=$theme1;} 

echo "
<link href='"; $style; echo".css' rel='stylesheet' type='text/css' />

<form  action='".$_SERVER['PHP_SELF']."' method='post' id='myForm'>
<li onclick='myForm.submit();' value='$theme1'> business</li>
</form>

";
?>

コードは Web サイトに送信されますが、データは変更されません。フォームは選択オプション スタイルを使用して作成されました。コードは次のとおりです。

<form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post"> 
<select name="style"> 
<option type="submit" <?php echo "value='$theme1'";if($style == $theme1){echo    "selected='selected'";}?>><?php echo $theme1; ?></option>
<option type="submit" <?php echo "value='$theme2'";if($style == $theme2){echo "selected='selected'";}?>><?php echo $theme2; ?></option>
<option type="submit" <?php echo "value='$theme3'";if($style == $theme3){echo "selected='selected'";}?>><?php echo $theme3; ?></option>
</select>
<input type="submit">
</form>

li一部の情報が欠落しているという問題があると思います。コードの何が問題になっていますか?

4

3 に答える 3

0

エラーがたくさんありますが、ここにいくつかの修正があります

$theme1 = 'business';
$theme2 = 'modern';
$theme3 = 'web2'; //qoute strings
if(isset($_POST['style'])){
setcookie('style', $_POST['style'], time()+(60*60*24*1000));
    $style=$_POST['style'];
}
elseif(isset($_COOKIE['style'])){
$style=$_COOKIE['style'];
}else{
$style=$theme1;} 

echo "<link href='". $style . ".css' rel='stylesheet' type='text/css' />"; //this line was way off
于 2013-08-17T15:22:49.810 に答える
0

ここにはたくさんの問題があります!考えられる問題に最も関連するものは次のとおりです。

  • ; $style;おそらくあなたが思っていることをしないでしょう
  • <li value="…">は意味がなく、フォームで送信されません (また、<li>そもそも a 以外では は有効ではありません<ul>) 。

PHP のタグ と を使用するように修正されたものと<input type="submit">、いくつかのコード スタイルの変更を次に示します — PHP 5.4 以降を使用していると仮定します。

<?php
$themes = ['business', 'modern', 'web2'];
if(isset($_POST['style'])) {
    setcookie('style', $_POST['style'], time() + (60 * 60 * 24 * 1000));
    $style = $_POST['style'];
} elseif(isset($_COOKIE['style'])) {
    $style = $_COOKIE['style'];
} else {
    $style = $themes[0];
} 
?>

<link href="<?= $style ?>.css" rel="stylesheet" type="text/css" />

<form method="POST" id="myForm">
    <ul>
        <?php foreach($themes as $theme): ?>
            <li><input type="submit" name="style" value="<?= $theme ?>" /></li>
        <?php endforeach; ?>
    </ul>
</form>

POST を使用しないことをお勧めしますか? 誰かがページを更新しようとすると、迷惑になります。GET はこの種のものに適しています。また、リンクはボタンよりも簡単にスタイルを設定できます。

于 2013-08-17T15:40:05.183 に答える
-1

はい、あなたは正しいです。あなたはliで変数を割り当てるのを逃しました。次のコードを変更し、変数 ($theme1、$theme2、$theme3) に値を割り当てるときに、すべてが文字列であるため、一重引用符を使用して値を割り当てます。

echo "<link href='" . $style . ".css' rel='stylesheet' type='text/css' /><form  action='" . $_SERVER['PHP_SELF'] . "' method='post' id='myForm'><li onclick='myForm.submit();' value='" . $theme1 . "'>" . $style . "</li></form>";

注: ここでは、静的な値 "business" の代わりに、$style 変数に変更します。

于 2013-08-17T15:24:30.243 に答える