Newbie PHP form question.

Discussion in 'PHP' started by LongBeachIsland, Apr 1, 2012.

  1. #1
    ok So I finally decided to sit down and learn some php. It is goind much better then expected. Then the thought crossed my mind about putting html and php code in a form and sending it to process.php to output the results. As expected the html I input to the form rendered correctly. I checked out the htmlspecialchars() function on the php manual and got it working.

    I then tried to input some basic php code to the form to see if it rendered. It does not I tried with something simple like
    <?php echo '1234'; >
    Code (markup):
    . So my question is why doesn't it output to the browser? I thought I would need to use the strip_tags() function or something similar. Does it automatically strip the tags? I am running this on a local server. I just figured I would ask I did look around a little bit for the information?
     
    LongBeachIsland, Apr 1, 2012 IP
  2. samyak

    samyak Active Member

    Messages:
    280
    Likes Received:
    7
    Best Answers:
    4
    Trophy Points:
    90
    #2
    For starter, the code shoule be
    , but I am guessing that its just the typo here.
    The actual problem might be where you are putting this code in the form. Are you putting this code inside the "value" of the input?

    Also, are you viewing this file with http:// protocal or with file:/// ?
     
    samyak, Apr 1, 2012 IP
  3. LongBeachIsland

    LongBeachIsland Peon

    Messages:
    67
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Yes that was just a typo. I was running the file on wamp server localhost/form.php

    The Form
    The Script
    <?php
    $a= 'Just a simple test';
    $userInput = $_POST["abc"];
    echo "$userInput";	
    ?>
    Code (markup):
    I realize the question is kind of a simple basic question. I just wanted to understand what was going on as I anticipated it would display the value of the variable.
     
    LongBeachIsland, Apr 1, 2012 IP
  4. samyak

    samyak Active Member

    Messages:
    280
    Likes Received:
    7
    Best Answers:
    4
    Trophy Points:
    90
    #4
    Did you enter a value on the input box and click on submit. Only then your php code will show the value of that variable.
     
    samyak, Apr 2, 2012 IP
  5. Zinosi

    Zinosi Well-Known Member

    Messages:
    75
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    118
    #5
    You need to submit the form first! $_POST['abc'] would be empty in your case. Your doing it wrong. Fill in a value in in the "<input name="abc" type="text" />" and press the submit button.

    On the top of the page you can write "<?php echo $_POST['abc']; ?>". This would echo whatever is in the form text field.
     
    Zinosi, Apr 2, 2012 IP
  6. LongBeachIsland

    LongBeachIsland Peon

    Messages:
    67
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Yes thank you for the response. I am doing it the correct way. I was trying to see if the php code could be passed through the form and then displayed.
    I unserstand how to display the data from the form. I was attempting to see if you could pass through a php code through the form.

    I already defined $a

    So in the form itself I tried inputting <?php echo "$a"; ?> to see if it would then display that data

    I am just going to have to assume that php tags are automatically stripped from the form input when submitted.
     
    LongBeachIsland, Apr 2, 2012 IP
  7. Zinosi

    Zinosi Well-Known Member

    Messages:
    75
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    118
    #7
    If you want $a to be displayed on the form you need to enter :


    $a= 'Just a simple test';<input name="abc" type="text" value ="<?php echo $a; ?>"/>
     
    Zinosi, Apr 3, 2012 IP
    wptheme likes this.