Php提交自返回源码

发布于 2025-01-07 17:34:20 字数 1071 浏览 5 评论 0 原文

我试图向 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 已提交值。

任何对此的帮助都会很棒

I am trying to submit a php form to self but after submit the page returns the source code of the page and not the processed data.

I have a issset to check if the form has been submitted and then have functions on the same page to process the submitted data.

Here is the code:

    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;

        }
    }

Here is the form:

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

Trying to get the form processed on the same page

Using the Browser Inspector, i see that the POST is submitted with values.

Any help with this would be great

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

顾冷 2025-01-14 17:34:20

要在同一页面中进行自我操作,请在表单操作中使用此操作 $_SERVER['REQUEST_URI']

//这是您问题的确切答案代码

<?php
if(isset($_POST['submitBtn'])){


    function convertToEm($value) {

            $base_font = 16;
            $px_value = $value/$base_font;
        return  $px_value;
        }
        function convertToPx($value) {
            $base_font = 32;
            $em_value = $value/$base_font;
            return  $em_value;
        }

       if (isset($_POST['type']) && $_POST['type']=='px') {

            $pxValue = $_POST['value'];
        $value = convertToEm($pxValue); 

        }

        if (isset($_POST['type']) && $_POST['type'] == 'em') {

            $emValue = $_POST['value'];
            $value = convertToPx($emValue); 

        }


    }

//form data

<form action="<?php echo $_SERVER['REQUEST_URI']; ?>" id="converterPx" method="POST">
    <h3>Convert Pixels to Ems:</h3>
    <label>PX:</label>
    <input type="text" name="value" />
    <?php echo 'Result:'.$value; ?>
    <input type="hidden" name="type" value="px" />
    <input type="submit" name="submitBtn" id="submit-px" />
</form>

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
if(isset($_POST['submitBtn'])){


    function convertToEm($value) {

            $base_font = 16;
            $px_value = $value/$base_font;
        return  $px_value;
        }
        function convertToPx($value) {
            $base_font = 32;
            $em_value = $value/$base_font;
            return  $em_value;
        }

       if (isset($_POST['type']) && $_POST['type']=='px') {

            $pxValue = $_POST['value'];
        $value = convertToEm($pxValue); 

        }

        if (isset($_POST['type']) && $_POST['type'] == 'em') {

            $emValue = $_POST['value'];
            $value = convertToPx($emValue); 

        }


    }

//form data

<form action="<?php echo $_SERVER['REQUEST_URI']; ?>" id="converterPx" method="POST">
    <h3>Convert Pixels to Ems:</h3>
    <label>PX:</label>
    <input type="text" name="value" />
    <?php echo 'Result:'.$value; ?>
    <input type="hidden" name="type" value="px" />
    <input type="submit" name="submitBtn" id="submit-px" />
</form>
臻嫒无言 2025-01-14 17:34:20

如果 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)

随遇而安 2025-01-14 17:34:20

这是您代码中的拼写错误,您使用了名称而不是类型

if ($_POST['type']== 'px') {
        $pxValue = $_POST['value'];
        $value = convertToEm($pxValue); 
    }

    if ($_POST['type'] == 'em') {
        $emValue = $_POST['value'];
        $value = convertToPx($emValue); 
    }

This is a typo in your code ,,you used name instead of type

if ($_POST['type']== 'px') {
        $pxValue = $_POST['value'];
        $value = convertToEm($pxValue); 
    }

    if ($_POST['type'] == 'em') {
        $emValue = $_POST['value'];
        $value = convertToPx($emValue); 
    }
慵挽 2025-01-14 17:34:20

$_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.

清眉祭 2025-01-14 17:34:20

您是否忘记了 标签?另外 $_POST['name' == 'px'] 应该是 $_POST['name'] == 'px',convertToPx 函数缺失,你没有表单上的参数名为 name,并且您没有回显任何内容。

<?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;
        }
        function convertToPx($value) {
            $base_font = 32;
            $em_value = $value / $base_font;
            return $em_value;
        }


    echo $value;
    }
?>

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.

<?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;
        }
        function convertToPx($value) {
            $base_font = 32;
            $em_value = $value / $base_font;
            return $em_value;
        }


    echo $value;
    }
?>
倚栏听风 2025-01-14 17:34:20

您不是在提交给自己,而是在什么都提交

;

编辑: 正如劳伦斯所指出的,人们应该避免使用我最初在下面描述的方法。请改为使用 $_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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文