how to pass text set in a javascript?

  • Follow


In my php page, I have this text area whose text data I want to pass to the php code set in action field. 
Of course, this is working. 
But then, I placed a little button below the text area, so that if a user clicks on it, an example text message gets on there. 
Of course, it is expected that if I click on submit button, this should be able to be passed as data. 
But I find it not working. 

OK, let me summarize with a code snippet. 

I have the following form. 
<form name="input" action="next.php" method="post" enctype="multipart/form-data" class="SearchForm" target="_self">
<textarea id="fgData" name="fgData" class="xxlarge" rows="10" disabled="">
<input type="button" value="Example load" onclick="loadExample();">
</form>

And loadExample is declared on ahead as in the following:
<script type="text/javascript">
<!--
function loadExample() {
document.forms["input"].fgData.value = "asdf";
}
//-->
</script>

It actually puts "asdf" on fgData text area. 
However as I click the submit button and echo $fgData;, it doesn't print anything, 
meaning it is not passed over. 

What am I missing to pass the data?

Thanking in advance, 
Justpark.
0
Reply thejustpark (7) 8/15/2012 3:51:23 AM

thejustpark wrote:
> In my php page, I have this text area whose text data I want to pass to the php code set in action field. 
> Of course, this is working. 
> But then, I placed a little button below the text area, so that if a user clicks on it, an example text message gets on there. 
> Of course, it is expected that if I click on submit button, this should be able to be passed as data. 
> But I find it not working. 
>
> OK, let me summarize with a code snippet. 
>
> I have the following form. 
> <form name="input" action="next.php" method="post" enctype="multipart/form-data" class="SearchForm" target="_self">
> <textarea id="fgData" name="fgData" class="xxlarge" rows="10" disabled="">
> <input type="button" value="Example load" onclick="loadExample();">
> </form>
>
> And loadExample is declared on ahead as in the following:
> <script type="text/javascript">
> <!--
> function loadExample() {
> document.forms["input"].fgData.value = "asdf";
> }
> //-->
> </script>
>
> It actually puts "asdf" on fgData text area. 
> However as I click the submit button and echo $fgData;, it doesn't print anything, 
> meaning it is not passed over. 
>
> What am I missing to pass the data?
>
> Thanking in advance, 
> Justpark.
>   

Perhaps the superglobal $_POST? You've posted this data to your script, 
so look in $_POST['fgData'].

-- 
*****************************
 Chuck Anderson � Boulder, CO
  http://cycletourist.com
 Turn Off, Tune Out, Drop In
*****************************
0
Reply cycletourist (73) 8/15/2012 4:30:04 AM


On 8/15/2012 5:51 AM, thejustpark wrote:
> In my php page, I have this text area whose text data I want to pass to the php code set in action field.
> Of course, this is working.
> But then, I placed a little button below the text area, so that if a user clicks on it, an example text message gets on there.
> Of course, it is expected that if I click on submit button, this should be able to be passed as data.
> But I find it not working.
>
> OK, let me summarize with a code snippet.
>
> I have the following form.
> <form name="input" action="next.php" method="post" enctype="multipart/form-data" class="SearchForm" target="_self">
> <textarea id="fgData" name="fgData" class="xxlarge" rows="10" disabled="">
> <input type="button" value="Example load" onclick="loadExample();">
> </form>
>
> And loadExample is declared on ahead as in the following:
> <script type="text/javascript">
> <!--

The above script-hiding techniques stopped making sense over 10 years ago.
I think you can safely stop using that nowadays.

> function loadExample() {
> document.forms["input"].fgData.value = "asdf";

In my humble opinion, "input" is a confusing name for a form.
Also consider addressing the elements of the form like this:
document.forms["input"].elements["fgData"].value = "asdf";


> }
> //-->
> </script>
>
> It actually puts "asdf" on fgData text area.
> However as I click the submit button and echo $fgData;, it doesn't print anything,
> meaning it is not passed over.
>
> What am I missing to pass the data?

Your example looks fine to me.

But this becomes a PHP question now.
So, from PHP (the target of the form, being next.php) add this at the 
beginning of your script to check:
<?php
echo "<pre>";
print_r($_POST);
echo "</pre>";
exit;

That way you will see on screen what PHP receives in $_POST.

Regards,
Erwin Moller

>
> Thanking in advance,
> Justpark.
>


-- 
"That which can be asserted without evidence, can be dismissed without 
evidence."
-- Christopher Hitchens
0
Reply erwinmollerusenet (104) 8/15/2012 6:08:41 AM

On Tue, 14 Aug 2012 20:51:23 -0700, thejustpark wrote:

> OK, let me summarize with a code snippet.

The button to load your example text does not need to be a part of the 
form, but your form submit button does. Your "Input" button is not acting 
as a submit button because of the function call. If you want it to act as 
a submit, you need to make the function return the logical value true.

Your textarea should be closed with </textarea>

disabled="" is better written as disabled="disabled". If you don't want 
the textarea disabled, omit the attribute completely. It is better to get 
into good habits now, than into bad ones. People who got into bad habits 
when they started writing html are still making mistakes 15+ years later 
from those same bad habits. (Hi Alexander Baron)

Try this (moving the load text button outside the form, using a submit in 
the form):

<p>
<form name="input" action="next.php" method="post"
enctype="multipart/form-data" class="SearchForm" target="_self">
<textarea id="fgData" name="fgData" class="xxlarge" rows="10" 
disabled=""></textarea><br>
<input type="submit" value="Submit">
</form>
<br>
<input type="button" value="Example load" onclick="loadExample();">
</p>

*OR* try changing the script to this:

<script type="text/javascript">
function loadExample() {
document.forms["input"].fgData.value = "asdf";
return true;
}
</script>

In either case, put the closing </textarea> after the opening one.

One other thing, it seems your question relates to javascript and html, 
but you posted it on a php newsgroup. Please use a more appropriate 
newsgroup next time, or you may find that your reception becomes less 
friendly.

Rgds

Denis McMahon
0
Reply denismfmcmahon (404) 8/15/2012 6:30:09 PM

On 15/08/2012 19:30, Denis McMahon wrote:
> On Tue, 14 Aug 2012 20:51:23 -0700, thejustpark wrote:
>
>> OK, let me summarize with a code snippet.
>
> The button to load your example text does not need to be a part of the
> form, but your form submit button does. Your "Input" button is not acting
> as a submit button because of the function call.

The input button is not acting like a submit button because it's /not/ a 
submit button (type="button"). It is inert, insofar as form actions are 
concerned.

> If you want it to act as a submit, you need to make the function return the logical value true.

That would be true if the intrinsic event handler included a return 
statement but as it doesn't, there would be no cancellation of the 
default action. That is to say:

   <input type="submit" ... onsubmit="doFoo(); return true;">

and:

   <input type="submit" ... onsubmit="doFoo();">

are functionally equivalent. It's the lack of a default action in itself 
that's possibly a problem, but that depends on whether the OP's example 
accurately represents what's being tested.

> Your textarea should be closed with </textarea>

Absolutely!

> disabled="" is better written as disabled="disabled".  [snip]

If the attribute is given a value, that value /must/ be "disabled". In 
HTML (but not XHTML), it would also be valid to simply write the 
attribute name; the value would be assumed.

Kind regards,
-- 
Mike Winter
Replace ".invalid" with ".uk" to reply by e-mail.
0
Reply usenet2579 (22) 8/15/2012 7:35:14 PM

On Wed, 15 Aug 2012 20:35:15 +0100, Mike Winter wrote:

> On 15/08/2012 19:30, Denis McMahon wrote:
>> On Tue, 14 Aug 2012 20:51:23 -0700, thejustpark wrote:
>>
>>> OK, let me summarize with a code snippet.
>>
>> The button to load your example text does not need to be a part of the
>> form, but your form submit button does. Your "Input" button is not
>> acting as a submit button because of the function call.

> The input button is not acting like a submit button because it's /not/ a
> submit button (type="button"). It is inert, insofar as form actions are
> concerned.

* facedesks *

My bad, I confused <button ....> where the default action (no 
"type='[input|submit|button]'" attribute) is to act as a submit, and 
<input type="button" .... >

I shall stare at a printed copy of the 4.01 strict DTD until I fall 
asleep tonight as penance.

>> disabled="" is better written as disabled="disabled".  [snip]
> 
> If the attribute is given a value, that value /must/ be "disabled". In
> HTML (but not XHTML), it would also be valid to simply write the
> attribute name; the value would be assumed.

Yes, but I sometimes try and lead gently rather than shout ;)

Rgds

Denis McMahon
0
Reply denismfmcmahon (404) 8/15/2012 9:58:13 PM

Thank you guys so much!
I know that in order to answer this, you guys put your time and effort to read, think, and speak.
So I really appreciate this guys. 

I realize that some syntactic errors are in the page.
I don't have them in my code, but when I make up this example, I made such mistakes.
Below is the working example with the same problem.

<script type="text/javascript"> 
<!-- 
function loadExample() { 
document.forms["region"].elements['fgData'].value = "asdf";
alert("asdf");
} 
//--> 
</script> 

<form name="region" action="calculator_sub.php" method="post" enctype="multipart/form-data" target="_self"> 
<textarea id="fgData" name="fgData" class="xxlarge" rows="10" disabled="disabled"> </textarea>
<input type="button" value="Example load" onclick="loadExample();"> 
<input type="submit" class="btn primary" id="submit_button" value="Submit">
</form> 

But in passing the argument to next.php, I see that '$_POST' doesn't deliver fgData to next.php, 
as I have 
<?php
echo "<pre>"; 
print_r($_POST); 
echo "</pre>"; 
?>
in next.php, there isn't even a variable named fgData. 
What do you think I should look into?

P.S. I kinda think it is related to php, because my understanding is that after the javascript function writes it to the textarea, it is php who should read and pass to next.php.

Thanks, 
Justpark.
0
Reply thejustpark (7) 8/16/2012 1:19:26 AM

thejustpark wrote:
> Thank you guys so much!
> I know that in order to answer this, you guys put your time and effort to read, think, and speak.
> So I really appreciate this guys. 
>
> I realize that some syntactic errors are in the page.
> I don't have them in my code, but when I make up this example, I made such mistakes.
> Below is the working example with the same problem.
>
> <script type="text/javascript"> 
> <!-- 
> function loadExample() { 
> document.forms["region"].elements['fgData'].value = "asdf";
>
> alert("asdf");
> } 
> //--> 
> </script> 
>
> <form name="region" action="calculator_sub.php" method="post" enctype="multipart/form-data" target="_self"> 
> <textarea id="fgData" name="fgData" class="xxlarge" rows="10" disabled="disabled"> </textarea>
> <input type="button" value="Example load" onclick="loadExample();"> 
> <input type="submit" class="btn primary" id="submit_button" value="Submit">
> </form> 
>
> But in passing the argument to next.php, I see that '$_POST' doesn't deliver fgData to next.php, 
> as I have 
> <?php
> echo "<pre>"; 
> print_r($_POST); 
> echo "</pre>"; 
> ?>
> in next.php, there isn't even a variable named fgData. 
> What do you think I should look into?
>
> P.S. I kinda think it is related to php, because my understanding is that after the javascript function writes it to the textarea, it is php who should read and pass to next.php.
>   

No. It's your browser that reads the form and sends a request to the 
server for your specified action, calculator_sub.php. Php does not run 
on the client. Php only runs on the server.

And the browser is not posting the textarea value because the the 
textarea is disabled.
(You could add - 
document.forms["region"].elements['fgData'].disabled=false - wherever 
you see fit, but this is strictly a JavaScript question, not Php.)

-- 
*****************************
 Chuck Anderson � Boulder, CO
  http://cycletourist.com
 Turn Off, Tune Out, Drop In
*****************************
0
Reply cycletourist (73) 8/16/2012 2:09:47 AM

On Wednesday, August 15, 2012 9:09:47 PM UTC-5, Chuck Anderson wrote:
> thejustpark wrote:
>=20
> > Thank you guys so much!
>=20
> > I know that in order to answer this, you guys put your time and effort =
to read, think, and speak.
>=20
> > So I really appreciate this guys.=20
>=20
> >
>=20
> > I realize that some syntactic errors are in the page.
>=20
> > I don't have them in my code, but when I make up this example, I made s=
uch mistakes.
>=20
> > Below is the working example with the same problem.
>=20
> >
>=20
> > <script type=3D"text/javascript">=20
>=20
> > <!--=20
>=20
> > function loadExample() {=20
>=20
> > document.forms["region"].elements['fgData'].value =3D "asdf";
>=20
> >
>=20
> > alert("asdf");
>=20
> > }=20
>=20
> > //-->=20
>=20
> > </script>=20
>=20
> >
>=20
> > <form name=3D"region" action=3D"calculator_sub.php" method=3D"post" enc=
type=3D"multipart/form-data" target=3D"_self">=20
>=20
> > <textarea id=3D"fgData" name=3D"fgData" class=3D"xxlarge" rows=3D"10" d=
isabled=3D"disabled"> </textarea>
>=20
> > <input type=3D"button" value=3D"Example load" onclick=3D"loadExample();=
">=20
>=20
> > <input type=3D"submit" class=3D"btn primary" id=3D"submit_button" value=
=3D"Submit">
>=20
> > </form>=20
>=20
> >
>=20
> > But in passing the argument to next.php, I see that '$_POST' doesn't de=
liver fgData to next.php,=20
>=20
> > as I have=20
>=20
> > <?php
>=20
> > echo "<pre>";=20
>=20
> > print_r($_POST);=20
>=20
> > echo "</pre>";=20
>=20
> > ?>
>=20
> > in next.php, there isn't even a variable named fgData.=20
>=20
> > What do you think I should look into?
>=20
> >
>=20
> > P.S. I kinda think it is related to php, because my understanding is th=
at after the javascript function writes it to the textarea, it is php who s=
hould read and pass to next.php.
>=20
> >  =20
>=20
>=20
>=20
> No. It's your browser that reads the form and sends a request to the=20
>=20
> server for your specified action, calculator_sub.php. Php does not run=20
>=20
> on the client. Php only runs on the server.
>=20
>=20
>=20
> And the browser is not posting the textarea value because the the=20
>=20
> textarea is disabled.
>=20
> (You could add -=20
>=20
> document.forms["region"].elements['fgData'].disabled=3Dfalse - wherever=
=20
>=20
> you see fit, but this is strictly a JavaScript question, not Php.)
>=20
>=20
>=20
> --=20
>=20
> *****************************
>=20
>  Chuck Anderson =95 Boulder, CO
>=20
>   http://cycletourist.com
>=20
>  Turn Off, Tune Out, Drop In
>=20
> *****************************

Oh thank you.=20
With your explanation, I finally get it pass the text!

Again thanks a lot!
0
Reply thejustpark (7) 8/16/2012 4:23:30 AM

On Wed, 15 Aug 2012 21:23:30 -0700, thejustpark wrote:

>> And the browser is not posting the textarea value because the the
>> textarea is disabled.

Which begs the question:

"Why is the textarea disabled"?

If the OP doesn't want data being entered directly into the textarea, 
then surely setting its readonly attribute would be more appropriate?

Rgds

Denis McMahon (straying way off topic for php now, xpd & fu set to ciwah)
0
Reply denismfmcmahon (404) 8/16/2012 8:10:19 AM

9 Replies
24 Views

(page loaded in 0.119 seconds)


Reply: