Php提交自返回源码
我试图向 self 提交一个 php 表单,但提交后页面返回页面的源代码,而不是处理后的数据。
我有一个问题来检查表单是否已提交,然后在同一页面上有函数来处理提交的数据。
这是代码:
if(isset($_POST['submit'])){ if ($_POST['name' == 'px']) { $pxValue = $_POST['value']; $value = convertToEm($pxValue); } if ($_POST['name' == 'em']) { $emValue = $_POST['value']; $value = convertToPx($emValue); } function convertToEm($value) { $base_font = 16; $em_value = $value / $base_font; return $em_value; } }
这是表单:
<form action="" id="converterPx" method="POST">
<h3>Convert Pixels to Ems:</h3>
<label>PX:</label>
<input type="text" name="value" value="" />
<?php echo 'Result:'. $value; ?>
<input type="hidden" name="type" value="px" />
<input type="submit" name="submit" id="submit-px" />
</form>
尝试在同一页面上处理表单
使用浏览器检查器,我看到 POST 已提交值。
任何对此的帮助都会很棒
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
要在同一页面中进行自我操作,请在表单操作中使用此操作 $_SERVER['REQUEST_URI']
//这是您问题的确切答案代码
To make a self action in the same page use this in the form action $_SERVER['REQUEST_URI']
//This is the exact answer code of your question
如果 PHP 源出现在返回的页面上,则可能是因为您忘记了标签
,或者是因为服务器未配置为正确执行 PHP
(或者文件名的扩展名错误)
If PHP source appears on the returned page it is either because you forgot the tags
Or because of the server not being configured to execute PHP correctly
(Or the file name has the wrong extension)
这是您代码中的拼写错误,您使用了名称而不是类型
This is a typo in your code ,,you used name instead of type
$_POST['name' == 'px']
可能应该是$_POST['name'] == 'px']
(在类似的构造)。您正在尝试使用两个字符串之间的比较结果(这将是错误的)作为数组索引。
$_POST['name' == 'px']
should probably be$_POST['name'] == 'px']
(with a similar change being made on the similar construct).You are trying to use the result of the comparison between two strings (which will be false) as the array index.
您是否忘记了
标签?另外
$_POST['name' == 'px']
应该是$_POST['name'] == 'px'
,convertToPx 函数缺失,你没有表单上的参数名为 name,并且您没有回显任何内容。Are you forgetting the
<?php ?>
tags? also$_POST['name' == 'px']
should be$_POST['name'] == 'px'
, convertToPx function is missing, you have no param called name on your form annnnd your not echoing anything.您不是在提交给自己,而是在什么都提交:
编辑: 正如劳伦斯所指出的,人们应该避免使用我最初在下面描述的方法。请改为使用
$_SERVER['SCRIPT_NAME'];
,有关详细信息,请参阅 http://www.webadminblog.com/index.php/2010/02/23/a-xss-vulnerability-in-almost-every-php-form-ive-ever-writing/原文:根据您调用表单的方式,您可以尝试:
更多详细信息:http://www.html-form-guide.com/php-form/php-form-action-self.html
You're not submitting to self, you're submitting to nothing:
<form action="" id="converterPx" method="POST">
Edit: As Lawrence has pointed out, one should avoid using the method I originally described below. Instead, use
$_SERVER['SCRIPT_NAME'];
and for more information, see http://www.webadminblog.com/index.php/2010/02/23/a-xss-vulnerability-in-almost-every-php-form-ive-ever-written/Original: Depending on how you call the form, you can try:
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" id="converterPx" method="POST">
More details here: http://www.html-form-guide.com/php-form/php-form-action-self.html