How to pass text from one textbox to another using php

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
cool30
Forum Newbie
Posts: 12
Joined: Fri Jun 05, 2009 12:55 am

How to pass text from one textbox to another using php

Post by cool30 »

I am new to php and I want to create a form that has 3 textboxes.
e.g The user enters in the textbox1 - Green
The user enters in the textbox2 - Bird
When the user clicks the button, the third textbox should display the text"Green", "Bird".
(The third textbox is on the same page as the other two)
How can I achieve this ?
Thanks
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: How to pass text from one textbox to another using php

Post by requinix »

Use $_POST to get the values of the two textboxes. Then write the HTML for the third text box accordingly.
cool30
Forum Newbie
Posts: 12
Joined: Fri Jun 05, 2009 12:55 am

Re: How to pass text from one textbox to another using php

Post by cool30 »

I am using the _POST but it doesn't seem to be working. The code with which I am working is below
I appreciate you help on this

Code: Select all

 
<form id="form1" name="form1" method="post" action="">
  <label>First Name
  <input type="text" value = "First Name " name="textfield" />
  </label>
</form>
 
<form id="form2" name="form2" method="post" action="">
  <label>Last Name
  <input type="text" value = "Last Name" name="textfield2" />
  </label>
</form>
 
 <?PHP
  if(isset($_POST['Submit']))
  {
   $_POST['textfield'];
   $_POST['textfield2
  // I want to get the values from above textboxes into textfield15.
  }
 ?>
<label>
  <textarea name="textfield15" cols="100" rows="20" type = "text" value = "<?= $_POST['textfield']; ?>" ></textarea>
  </label>
  
  
 
 
 
Last edited by Benjamin on Fri Jun 05, 2009 11:16 am, edited 1 time in total.
Reason: Changed code type from text to php.
Reviresco
Forum Contributor
Posts: 172
Joined: Tue Feb 19, 2008 4:18 pm
Location: Milwaukee

Re: How to pass text from one textbox to another using php

Post by Reviresco »

This is a job for javascript:

Code: Select all

<form>
<input type="text" id="textbox1" />
<input type="text" id="textbox2" />
<input type="text" id="textbox3" />
<input type="button" onclick="change_text();" value="Click me." />
</form>
 
<script type="text/javascript">
var textbox1 = document.getElementById('textbox1');
var textbox2 = document.getElementById('textbox2');
var textbox3 = document.getElementById('textbox3');
 
function change_text() {
textbox3.value = textbox1.value + ', ' + textbox2.value;
}
</script>
User avatar
mikemike
Forum Contributor
Posts: 355
Joined: Sun May 24, 2009 5:37 pm
Location: Chester, UK

Re: How to pass text from one textbox to another using php

Post by mikemike »

<textarea> doesn't have a value attribute, you put the content between the tags. Try the following:

Code: Select all

<form id="form1" name="form1" method="post" action="">
   <label>First Name
   <input type="text" value = "First Name " name="textfield" />
   </label>
 
   <label>Last Name
   <input type="text" value = "Last Name" name="textfield2" />
   </label>
    <button type="submit">Submit</button>
 </form>
  
<!-- Removed your PHP block as it is irrelevant and never runs due to a poor if-condition -->
  
 <label>
   <textarea name="textfield15" cols="100" rows="20"><?php echo $_POST['textfield'].$_POST['textfield2']; ?></textarea>
   </label>
cool30
Forum Newbie
Posts: 12
Joined: Fri Jun 05, 2009 12:55 am

Re: How to pass text from one textbox to another using php

Post by cool30 »

I tried the above code . When the user types the value in the first name and the last name , instead of displaying the values in the third textbox, it's giving the following error in the third textbox.I appreciate your help on this.

Code: Select all

 
 
<br />
<b>Notice</b>:  Undefined index:  textfield in <b>C:\Program Files\EasyPHP 3.0\www\adword\AdwordEditor.php</b> on line <b>98</b><br />
<br />
<b>Notice</b>:  Undefined index:  textfield2 in <b>C:\Program Files\EasyPHP 3.0\www\adword\AdwordEditor.php</b> on line <b>98</b><br />
 >
 
 
cool30
Forum Newbie
Posts: 12
Joined: Fri Jun 05, 2009 12:55 am

Re: How to pass text from one textbox to another using php

Post by cool30 »

The above code is working when I write it in notepad but somehow gives an error when I am using Macromedia dreamweaver.I am new to php and my first time using Dreamweaver, if someone could tell me the code which is working in notepad why doesn't it seem to be working in dreamweaver?
Thanks.
User avatar
mikemike
Forum Contributor
Posts: 355
Joined: Sun May 24, 2009 5:37 pm
Location: Chester, UK

Re: How to pass text from one textbox to another using php

Post by mikemike »

I can't tell you why your IDE is playing up, but I can tell you why you get those errors.

$_POST will only be set once your form has been posted. Try the following:

Code: Select all

<textarea name="textfield15" cols="100" rows="20"><?php echo (!empty($_POST['textfield']) ? $_POST['textfield'] : '') . (!empty($_POST['textfield2']) ? $_POST['textfield2'] : ''); ?></textarea>
cool30
Forum Newbie
Posts: 12
Joined: Fri Jun 05, 2009 12:55 am

Re: How to pass text from one textbox to another using php

Post by cool30 »

Thank you very much mikemike for your help. It works great. Is there any way that I can put the user input for textfield and textfield2 in double quotes with comma in between them. I really appreciate your help.
Thanks.
User avatar
mikemike
Forum Contributor
Posts: 355
Joined: Sun May 24, 2009 5:37 pm
Location: Chester, UK

Re: How to pass text from one textbox to another using php

Post by mikemike »

I don't normally do people's work for them...

Code: Select all

<textarea name="textfield15" cols="100" rows="20"><?php echo (!empty($_POST['textfield']) ? '"'.$_POST['textfield'].'"' : '') . ( (!empty($_POST['textfield']) && !empty($_POST['textfield2'])) ? ',' : '') . (!empty($_POST['textfield2']) ? '"'.$_POST['textfield2'].'"' : ''); ?></textarea>
cool30
Forum Newbie
Posts: 12
Joined: Fri Jun 05, 2009 12:55 am

Re: How to pass text from one textbox to another using php

Post by cool30 »

Thank you, it works. I was trying to do it but was not able to.
Thanks once again.
Post Reply