Thursday, February 17, 2011

A form that spits out the input

I can't for the life of me find a form that DOESNT email the results that you submit.

I'm looking to find a form that I can have users enter simple data that i can then spit back out at them in different arrangements. If they submit First and Last, I'll spit out, amongst other things, FirstLast@domain.com. I'm willing to scrounge the code manually to do this, but I cant find a simple form that would allow me to do this.

EDIT: PHP or similar simple languages. I've never touched .NET before.

From stackoverflow
  • What language/platform/environment are you working in?

    I guess you might be looking for a hosted script or webform (the way that people will host web-to-mail scripts I suppose) but I doubt there would be one out there that does this.

    But if you have a specific framework to work in, e.g. PHP or .net, please update the question and let us know which.

    AlexeyMK : (sitting next to ylight) - basic LAMP/PHP
  • PHP or similar simple languages. I've never touched .NET before.

    S.Lott : Since this is in your question, feel free to delete this useless "non-answer".
  • Thing that simple doens't even need server-side support.

    <form onsubmit="magic(this);return false">
        <p><label>First <input name=first/></label>
        <p><label>Last <input name=last/></label>
        <input type="submit">
    
        <div id="output"></div>
    </form>   
    
    <script type="text/javascript">
        var output = document.getElementById('output');
        function toHTML(text)
        {
            return text.replace(/</g,'&lt;');
        }
    
        function magic(form)
        {
            output.innerHTML = toHTML(form.first.value + form.last.value) + '@domain.com';
        }
    </script>
    
  • Form:

    <form action="process.php" method="post">
        First: <input type="text" name="first" />
        Last: <input type="text" name="last" />
        <input type="submit" />
    </form>
    

    Next page:

    <?php
    
    $first = $_POST['first'];
    $last =  $_POST['last']
    
    echo $first . "." . $last . "@domain.com";
    ?>
    

    See http://www.w3schools.com/php/php_forms.asp for more examples and explanation

    Tom Haigh : You should call htmlentities() or htmlspecialchars() on any user input you spit back to the browser to prevent xss attacks
    alex77 : Totally agree with you on that one. Was just trying to keep the answer as simple and minimal as possible.
    Tomalak : While you are at it... Look for characters invalid in e-mail addresses. Anyway: Wouldn't a form field that asks for the mail address be simpler or am I still missing the point?
    porneL : If you agree, then please don't give bad advice and correct the example to escape properly.
  • Regardless of how you get it, always remember to never trust user input.

    <?php
    
    $sfirst = htmlentities($_POST['first']);
    $slast =  htmlentities($_POST['last']);
    
    echo $first . "." . $last . "@domain.com";
    ?>
    

    Also, running a validator on the final result may be helpful. But please don't write your own email address validator.

    orlandu63 : You shouldn't be using htmlentities in this case. You should be used urlencode..
    porneL : @orlandu63: strictly speaking, both should be applied. First rawurlencode() to convert from text to text-in-URL, and then htmlspecialchars() to convert from URL to URL-in-HTML. In practice first escaping makes second unneccessary, but it doesn't mean that htmlspecialchars() shouldn't be used.
  • If I get your question right, sounds like this might do what you need..

    Note: This PHP code doesn't require any knowledge of the fields in the form that submits to it, it just loops through all of the fields, including multiple-choice fields (like checkboxes), and spits out their values.

    <?php
    // loop through every form field
    while( list( $field, $value ) = each( $_POST )) {
       // display values
       if( is_array( $value )) {
          // if checkbox (or other multiple value fields)
          while( list( $arrayField, $arrayValue ) = each( $value ) {
             echo "<p>" . $arrayValue . "</p>\n";
          }
       } else {
          echo "<p>" . $value . "</p>\n";
       }
    }
    ?>
    

0 comments:

Post a Comment