Page 1 of 1

How to pass text from one textbox to another using php

Posted: Fri Jun 05, 2009 1:03 am
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

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

Posted: Fri Jun 05, 2009 3:04 am
by requinix
Use $_POST to get the values of the two textboxes. Then write the HTML for the third text box accordingly.

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

Posted: Fri Jun 05, 2009 8:04 am
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>
  
  
 
 
 

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

Posted: Fri Jun 05, 2009 11:10 am
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>

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

Posted: Fri Jun 05, 2009 11:25 am
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>

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

Posted: Fri Jun 05, 2009 11:21 pm
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 />
 >
 
 

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

Posted: Fri Jun 05, 2009 11:34 pm
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.

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

Posted: Sat Jun 06, 2009 10:38 am
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>

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

Posted: Sun Jun 07, 2009 11:48 am
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.

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

Posted: Mon Jun 08, 2009 9:41 am
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>

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

Posted: Mon Jun 08, 2009 2:59 pm
by cool30
Thank you, it works. I was trying to do it but was not able to.
Thanks once again.