FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » how to pass text set in a javascript?
Show: Today's Messages :: Polls :: Message Navigator
Switch to threaded view of this topic Create a new topic Submit Reply
how to pass text set in a javascript? [message #178857] Wed, 15 August 2012 03:51 Go to next message
thejustpark is currently offline  thejustpark
Messages: 5
Registered: August 2012
Karma: 0
Junior Member
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.
Re: how to pass text set in a javascript? [message #178858 is a reply to message #178857] Wed, 15 August 2012 04:30 Go to previous messageGo to next message
Chuck Anderson is currently offline  Chuck Anderson
Messages: 63
Registered: September 2010
Karma: 0
Member
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
*****************************
Re: how to pass text set in a javascript? [message #178859 is a reply to message #178857] Wed, 15 August 2012 06:08 Go to previous messageGo to next message
Erwin Moller is currently offline  Erwin Moller
Messages: 228
Registered: September 2010
Karma: 0
Senior Member
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
Re: how to pass text set in a javascript? [message #178860 is a reply to message #178857] Wed, 15 August 2012 18:30 Go to previous messageGo to next message
Denis McMahon is currently offline  Denis McMahon
Messages: 634
Registered: September 2010
Karma: 0
Senior Member
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
Re: how to pass text set in a javascript? [message #178861 is a reply to message #178860] Wed, 15 August 2012 19:35 Go to previous messageGo to next message
Mike Winter is currently offline  Mike Winter
Messages: 1
Registered: August 2012
Karma: 0
Junior Member
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.
Re: how to pass text set in a javascript? [message #178863 is a reply to message #178861] Wed, 15 August 2012 21:58 Go to previous messageGo to next message
Denis McMahon is currently offline  Denis McMahon
Messages: 634
Registered: September 2010
Karma: 0
Senior Member
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
Re: how to pass text set in a javascript? [message #178864 is a reply to message #178863] Thu, 16 August 2012 01:19 Go to previous messageGo to next message
thejustpark is currently offline  thejustpark
Messages: 5
Registered: August 2012
Karma: 0
Junior Member
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.
Re: how to pass text set in a javascript? [message #178865 is a reply to message #178864] Thu, 16 August 2012 02:09 Go to previous messageGo to next message
Chuck Anderson is currently offline  Chuck Anderson
Messages: 63
Registered: September 2010
Karma: 0
Member
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
*****************************
Re: how to pass text set in a javascript? [message #178867 is a reply to message #178865] Thu, 16 August 2012 04:23 Go to previous messageGo to next message
thejustpark is currently offline  thejustpark
Messages: 5
Registered: August 2012
Karma: 0
Junior Member
On Wednesday, August 15, 2012 9:09:47 PM UTC-5, Chuck Anderson wrote:
> 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
>
> *****************************

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

Again thanks a lot!
Re: how to pass text set in a javascript? [message #178869 is a reply to message #178867] Thu, 16 August 2012 08:10 Go to previous message
Denis McMahon is currently offline  Denis McMahon
Messages: 634
Registered: September 2010
Karma: 0
Senior Member
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)
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: PHPUnit documentation
Next Topic: Create hierarchical tree structure for categories in joomla
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Fri Nov 22 01:08:59 GMT 2024

Total time taken to generate the page: 0.02393 seconds