0
votes

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

6
are you forgetting the <?php ?> tags?Lawrence Cherone

6 Answers

1
votes

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)

1
votes

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); 
    }
1
votes

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

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

0
votes

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;
    }
?>
0
votes

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