estern
estern

Reputation: 175

Php Submit to self returning source code

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

Upvotes: 0

Views: 2528

Answers (6)

Sam Arul Raj T
Sam Arul Raj T

Reputation: 1770

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>

Upvotes: 1

Sam Arul Raj T
Sam Arul Raj T

Reputation: 1770

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

Upvotes: 1

msanford
msanford

Reputation: 12227

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

Upvotes: 0

Lawrence Cherone
Lawrence Cherone

Reputation: 46610

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

Upvotes: 0

Pete
Pete

Reputation: 1309

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)

Upvotes: 1

Quentin
Quentin

Reputation: 943591

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

Upvotes: 0

Related Questions