apply $variable to existing textarea without reload

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
sbrocks
Forum Newbie
Posts: 3
Joined: Fri Sep 25, 2009 10:24 am

apply $variable to existing textarea without reload

Post by sbrocks »

I have a form that has a textarea on it. The user fills in the textarea, then on submit that text is added to a postgressql database. All works great - BUT, if the user hits the 'back' button of the browser I want to check to see if they have already added a memo for that record, and if they have re-display the text that they had entered before submitting the form the first time. This works for the textbox but I can't figure out how to place the value returned into the textarea.
Here is the code that involves the query and replacing the data---

Code: Select all

 
 If(empty($firstTime)) {
 if (!empty($id)){
 //Get all info entered and re-diplay in the correct form box
        $querySel1  ="select * from stud_safety ";
        $querySel1 .="where id = $id;";
        $querySel   = $querySel1.$querySel;
        $resultsSel = pg_query($db_conn, $querySel);
//if there are results from the query
    if (pg_num_rows($resultsSel) >0) {  
      while ($summary = pg_fetch_array($resultsSel))     {
              $desc = stripslashes($summary['behavior_desc']);
              echo("<document.forms[0].getElementsById('behaviorDesc').value =\"$desc\">");
          $_POST['missingOn'] = stripslashes($summary['missingon']);
          $_POST['missingOff'] = stripslashes($summary['missingoff']);
       } //end while fetch array
    }//end query returned results   
    } //end id number exisis    
 }  //end if firstTime
 
This is the part that I can't seem to get:

Code: Select all

 
        $desc = stripslashes($summary['behavior_desc']);
        echo("<document.forms[0].getElementsById('behaviorDesc').value =\"$desc\">");
 
I've verified that $desc returns a value.
I feel like I'm close, I no longer get errors, but I don't get anything showing in the textarea either! The other 2 boxes fill up just fine.

Thanks!
User avatar
jazz090
Forum Contributor
Posts: 176
Joined: Sun Apr 12, 2009 3:29 pm
Location: England

Re: apply $variable to existing textarea without reload

Post by jazz090 »

well how about that you actually do that without JS.

Code: Select all

<textarea name="desc" rows="2" cols="20"><?php echo htmlentities($desc); ?></textarea>
the textarea is obviously the one you want for the text to appear in.
sbrocks
Forum Newbie
Posts: 3
Joined: Fri Sep 25, 2009 10:24 am

Re: apply $variable to existing textarea without reload

Post by sbrocks »

Well, not using JavaScript is an excellent point, but I couldn't find a way to achieve what I wanted using HTML, then even if I did I'm challenged by the prospect of putting the HTML in the middle of a PHP while loop (this is my first PHP project!).
The part that is making this difficult for me is the fact that when this code fires, the textarea already exists. What I'm attempting to do is place text into a textarea that already exists. When I search for that on line, examples using JavaScript is all I can find.
Can I assign a value to an existing textarea using HTML embedded into a PHP while loop?

Thanks
User avatar
jazz090
Forum Contributor
Posts: 176
Joined: Sun Apr 12, 2009 3:29 pm
Location: England

Re: apply $variable to existing textarea without reload

Post by jazz090 »

it does not matter where the html code is and where the php code is (unless you are echoing stuff). i suggest to you that yo initiate $desc before the textarea is created and then you assign it that value. and yes you can add the value on a while loop but the textarea needs to be created in the while loop .
sbrocks
Forum Newbie
Posts: 3
Joined: Fri Sep 25, 2009 10:24 am

Re: apply $variable to existing textarea without reload

Post by sbrocks »

Doh! I get it! I used your textarea, passed the variable to it, works like a champ!

Thank you very much! :D
Post Reply