开发者

HTML form input to update paragraph

I have a multi line input text box in a webpage and want all the entered text to update another area of text in the page. How would I be best to achieve this? At the moment, I have it t开发者_Python百科hat when you submit the form, php writes the input to a small text file which is then embedded in the page.

But, do I need to sanitize this so that people don't inject html. Also, do I need to make it add <p>...</p> tags for every line in the input box?

Thanks.


What you do depends on your requirements. You should definitely use htmlspecialchars() or strip_tags() to nullify any errant HTML. After that you can simply use nl2br() to insert some <BR> to space everything or you can explode() your string and add <p> tags.


Depending on how the update is being saved (just that instance, permanentlyfor that user, or permanently for all users), the options range from Javascript/Jquery simply updating that other text area to AJAX calls to a php backend script that sanitizes and saves the update.

For the <p>, you can use Javascript to split() based on newlines and then add a each element as a <p>text</p> using a for loop. The concept should work with any language.


To sanitize your input, please use http://htmlpurifier.org/ over strip_tags().


This depends on what you are trying to accomplish. is this a type of BLOG entry where the text that was already there remains and what you have typed is appended? is all the text being replaced? will this text be needed anywhere else besides on this page (database, recalled at a later time?)

once we have a few of those questions answered we can move forward :)

Also, what technology are you using? pure HTML, Javascript, PHP, ASP? that will help determin the best method for what you're trying to accomplish. (I did see php, just wondering what our options are)

elaborate and we'll figure it out :)... This is a simple AJAX call to load a text file into a div ... the premise should work (if i got the code right off the top of my head) ....

<html>
    <head>
        <script src="http://code.jquery.com/jquery-1.5.js"></script>
    </head>
    <body>
        <div id="media">
        </div>
        <form id="myform" method="POST">
            <input type="textarea" name="MyParagraph"></textarea>
            <input type="button" value="Change Text" onclick='$.get("SterilizeMyText.php",{ nbRandom: Math.random() }, function(data){ $("#media").html(data); });'>
        </form>
    </body>
</html>

you can change the onclick to be a php file that will take form variables ($_REQUEST['blah']) ahd then output the cleansed text if you like.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜