【问题标题】:display image in a button and send hidden values in a form with PHP在按钮中显示图像并使用 PHP 在表单中发送隐藏值
【发布时间】:2015-01-14 20:42:51
【问题描述】:

对不起,我的英语不好,但我需要在按钮中显示图像,然后在表单中发送隐藏值。

小米码:

echo "<form method='post' action='index.php'>";
echo '<input id="prueba" type="submit" value="Submit">​​​​';
echo "<input type='hidden' name='opt' value='3'>";
echo "<input type='hidden' name='sede' value='".$sede."'>";
echo '</form>';

在我的 CSS 中:

#prueba{
    background-image:url(back.png);
    font-size:0;
    width:50px;
    height:50px;
}​

请帮帮我。

【问题讨论】:

  • 这段代码有什么问题?这应该改变prueba提交按钮的背景图片
  • 有什么问题?发送表格?在按钮中显示图像?
  • 尝试为图片网址加上引号:background-image:url("back.png");
  • 即使这与您的问题无关,您也可能需要考虑使用 PHP Sessions 或 cookie 来存储和使用不同页面的数据。
  • 我的问题是:图像没有显示在按钮中并且出现这些字符“​​​​”。但是提交按钮工作正常。

标签: php html css image button


【解决方案1】:

简单地显示一张图片并使用javascript点击提交按钮怎么样?

示例 1:使用“document.form.submit()'

echo "<form id='myform1' method='post' action='index.php'>";
echo '<a href="javascript:void(0);" ';
echo   ' onclick="document.getElementById(\'myform1\').submit();">';
echo  '<img src="back.png" width="50" height="50" />';
echo '</a>';
echo "<input type='hidden' name='opt' value='3' />";
echo "<input type='hidden' name='sede' value='".$sede."' />";
echo '</form>';

或者,示例 2:使用隐藏的提交按钮(需要 jQuery)

echo "<form method='post' action='index.php'>";
echo '<a href="javascript:void(0);" onclick="$(\'#prueba\').trigger(\'click\');">';
echo  '<img src="back.png" width="50" height="50" />';
echo '</a>';
echo '<input id="prueba" type="submit" value="Submit" style="display:none" />​​​​';
echo "<input type='hidden' name='opt' value='3' />";
echo "<input type='hidden' name='sede' value='".$sede."' />";
echo '</form>';

或者,示例 3:使用 CSS 显示图像

echo "<form id='myform1' method='post' action='index.php'>";
echo '<a id="prueba" href="javascript:void(0);" ';
echo   ' onclick="document.getElementById(\'myform1\').submit();" />';
echo "<input type='hidden' name='opt' value='3' />";
echo "<input type='hidden' name='sede' value='".$sede."' />";
echo '</form>';

【讨论】:

    【解决方案2】:

    而不是输入类型=提交使用

      <input type="image" src="back.png" alt="Submit">
    

    【讨论】:

    • 但这不会自动提交表单。点击什么都不会发生。但是你可以在那里添加一些 jQuery ;)
    • errr ,是的,根据 w3 here 和 w3schools,我引用 type=image 将图像定义为提交按钮 here
    猜你喜欢
    • 1970-01-01
    • 2021-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多