【问题标题】:Form submission not changing data表单提交不更改数据
【发布时间】:2013-08-17 15:15:27
【问题描述】:

我在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>

";
?>

代码确实在网站上提交,但不会更改数据。该表单是使用选择选项样式制作的。代码如下:

<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 缺少一些信息存在问题。我的代码有什么问题?

【问题讨论】:

  • 先生,您需要正确设置基本的 php/html/mysql。
  • 您正在使用; 分隔echo 中的内容;它们应该是逗号。
  • 你没有引用你的字符串。
  • 你没有缩进你的块或间隔你的操作符。
  • $_SERVER["PHP_SELF"]不安全,使用htmlspecialchars删除危险字符!

标签: php


【解决方案1】:

有很多错误,但这里有一些修复

$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

【讨论】:

  • 不,我的意思是在这个中你有一个引号而不是一个点。
【解决方案2】:

这里有很多问题!与您的可能问题最相关的是:

  • ; $style; 可能不会像你认为的那样做
  • &lt;li value="…"&gt; 没有意义,不会随表单一起提交(&lt;li&gt;&lt;ul&gt; 之外无效)

因此,这两个都固定使用 PHP 的标签和 &lt;input type="submit"&gt;,以及一些代码样式更改 - 假设您使用的是 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 对这类事情同样适用,而且链接比按钮更容易设置样式。

【讨论】:

  • 那也没用。看起来 foreach 是围绕这种风格的,但它不工作
  • @user2692270:那么页面是什么样的?任何 PHP 错误?你把它们打开了吗?
  • 我相信我缺少语法 ;if($style == $theme1)
  • @user2692270:不,除非您的样式表没有命名为business.cssmodern.cssweb2.css。另外,您的样式表是否在同一目录中?是他们没有加载的问题吗?
  • 连代码都没有只是在开始业务或现代时添加单词
【解决方案3】:

是的,你是对的。您错过了在 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变量。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-05-30
    • 2015-02-18
    • 1970-01-01
    • 2023-03-26
    • 2012-03-03
    • 2016-06-18
    • 1970-01-01
    • 2014-07-19
    相关资源
    最近更新 更多