0

How would I send information from a form to a block of PHP code and then back to a text area? I can not find this answer.

2
  • Your question is very very basic. My suggestion would be to follow this tutorial : net.tutsplus.com/articles/news/diving-into-php It'll be really helpful Commented Apr 30, 2011 at 6:29
  • @JohnP I know but what I am trying to do is display my result in a text box but all the basic tutorials I had read or went with really did not displayed that they just say echo "something to document"; Commented Apr 30, 2011 at 6:34

3 Answers 3

5

To print out the text you entered in the textfield on the next request would look like this assuming you render the same page (i.e. myform.php):

<?php
  $fieldValue = htmlentities($_POST['myfield']);
?>

<form action="myform.php" method="post">
  <label for="myfield">Your textfield:</label>
  <input type="text" name="myfield" id="myfield" value="<?php echo $fieldValue; ?>" />
</form>
Sign up to request clarification or add additional context in comments.

4 Comments

Don't forget to escape $textfieldvalue with htmlspecialchars
So I would not put name I would part Id instead?
@Emil Vikström: Yes, good to point that out but I think it is out of scope for the given question.
@daddycardona: Using the name-attribute für form elements is always good because it is not guaranteed that the id is respected by the browser. Using the id-attribute helps to add labels to your form and reference form elements.
0

A very basic example.

Assuming you have a PHP file named index.php

<?php 
  $val = 'Nothing in POST';
  if (!empty($_POST)) { //$_POST is where stuff posted from the FORM is saved
    $val = isset($_POST['text']) ? $_POST['text'] : '';//you're looking for the data with key text which is the name of your textarea element
    $val = 'Got something from POST : ' . $val;
  }
?>


<form action='index.php' method='post'>
  <textarea name='text'><?php echo $val ?></textarea>
</form>

Have a look at this tutorial for the basics : http://net.tutsplus.com/articles/news/diving-into-php/

2 Comments

that is what I am talking about, I want to take like two values add them together and the have my result be posted in the text area.
is that you these tutorials are pretty bad ass so far thanks for your site.
0

use ajax!(and use jquery for that ajax!)

suppose you have this html :

<input type="text" id="input">
<textarea id="result"></textarea>

than the script should be:

$('#input').keypress(function(e){
    if(e.wich != 13)//not enter
        return;
    $.get(your_php_file.php,{param1:val1,param2:val2},function(result){
        $('#result').val(result);
    });
});

when you hit enter on the input field,the ajax function is being called that requests the file your_php_file.php?param1=val1&param2=val2. With the php's result, the callback function is being called which updates your textarea

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.