Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

javascript to php in the same page

20 views
Skip to first unread message

alex

unread,
Feb 14, 2016, 6:48:00 AM2/14/16
to
<html>
<body>
Select a file to upload:
<input type="file" id="myFile" onchange="myFunction()" size="50" >
<p id="demo"></p>
<script>
function myFunction() {
var vx = document.getElementById("myFile").value;
document.getElementById("demo").innerHTML = vx;
}
</script>
</body>
</html>

<?php
if (isset($value)){echo $value;}

?>


the variable vx I ned to use in the php $value

when I have this value the all page is loaded so is need to recall the
page php, but so I lose the value already selected;
I find some solutions: akax, filed hidden, cookie; but not know how use
in my code

Luuk

unread,
Feb 14, 2016, 9:00:49 AM2/14/16
to
The output of the php-stuff is 'converted' to 'text' before you can
press the 'submit' button.

http://php.net/manual/en/intro-whatis.php, says:
What distinguishes PHP from something like client-side JavaScript is
that the code is executed on the server, generating HTML which is then
sent to the client. The client would receive the results of running that
script, but would not know what the underlying code was. You can even
configure your web server to process all your HTML files with PHP, and
then there's really no way that users can tell what you have up your
sleeve.


The documentation is wrong in the part where is claims 'generating HTML'.
The output of the php-code is plain-text, which also might be valid HTML.

Ben Bacarisse

unread,
Feb 14, 2016, 10:32:06 AM2/14/16
to
I think you may be gradually going down the wrong path because you are
asking very narrow questions. This is made worse by the fact that your
questions are not very clear so people are just suggesting thing you
might try rather than guiding you to the best solution.

I suggest you back up a bit. What are you doing overall? What is it
you are trying to do that has led you to think that a file control, some
client side script and a bit of PHP is the solution?

--
Ben.

alex

unread,
Feb 14, 2016, 10:45:51 AM2/14/16
to
<html>
<body>
Select a file to upload:
<input type="file" id="myFile" onchange="myFunction()" size="50" >
<p id="demo"></p>
<script>
function myFunction() {
var vx = document.getElementById("myFile").value;
document.getElementById("demo").innerHTML = vx;
}
</script>
</body>
</html>


with the code above I can select a file in my pc and can have its name
inner the variable vx;
I need have(to pass) this value (file's name), inner a php variable.



Lew Pitcher

unread,
Feb 14, 2016, 11:08:25 AM2/14/16
to
On Sunday February 14 2016 10:45, in comp.lang.php, "alex" <al...@nomailno.it>
wrote:
If you are not willing to reconsider your design, then...

You need to write your javascript so that it invokes a data transfer to the
webhost where the PHP runs.

On the web-browser side,you will need to
1) write an html <form> that names the target host webpage (where your PHP
code resides) and invocation method (GET or POST)
2) write javascript to invoke document.<formname>.submit() (where <formname>
is the name of the html <form> from step 1

On the web-server side, you will need to write a host webpage in PHP that will
(when run from a GET or a POST) will retrieve the passed data (from the
<form> query, submitted through your javascript) and process it.


--
Lew Pitcher
"In Skills, We Trust"
PGP public key available upon request

Ben Bacarisse

unread,
Feb 14, 2016, 11:43:57 AM2/14/16
to
Well that's not really what I was asking but I don't think you are going
to tell me what you are really trying to do. I have no idea what an
inner variable is -- PHP has variables -- but you can give this value to
a server-side script simply by making the value of a form which gets
submitted:

<!DOCTYPE html>
<html>
<head>
<script>
function myFunction() {
var vx = document.getElementById("myFile").value;
document.getElementById("filename").value = vx;
document.getElementById("name_form").submit();
}
</script>
</head>
<body>
Select a file to upload:
<input type="file" id="myFile" onchange="myFunction()" size="50" >
<p id="demo"></p>
<form id="name_form" action="file.php" method=post>
<input type=text id=filename name=filename
value="<?= $_POST["filename"] ?>">
</form>
File name: <?= $_POST["filename"]; ?>
</body>
</html>

This omits all kind of important things like quoting and validating the
form contents, checking for errors and so on.

--
Ben.

Luuk

unread,
Feb 14, 2016, 11:50:50 AM2/14/16
to
On 14-02-16 17:43, Ben Bacarisse wrote:
> File name: <?= $_POST["filename"]; ?>

http://php.net/manual/en/language.basic-syntax.phptags.php
PHP also allows for short open tag <? (which is discouraged since it is
only available if enabled using the short_open_tag php.ini configuration
file directive, or if PHP was configured with the --enable-short-tags
option).

Christoph M. Becker

unread,
Feb 14, 2016, 12:17:32 PM2/14/16
to
This syntax works independently of the setting short_open_tag as of PHP
5.4, see <php.net/ChangeLog-5.php#5.4.0>.

--
Christoph M. Becker

Luuk

unread,
Feb 14, 2016, 1:43:03 PM2/14/16
to
I see that i confused the '<?' with the '<?=' ....

Ben Bacarisse

unread,
Feb 14, 2016, 4:40:21 PM2/14/16
to
Tim Streater <timst...@greenbee.net> writes:

> In article <87lh6n4...@bsb.me.uk>, Ben Bacarisse
> <ben.u...@bsb.me.uk> wrote:
>
>>to tell me what you are really trying to do. I have no idea what an
>>inner variable is -- PHP has variables -- but you can give this value to
>
> When he says: " ... can have its name inner the variable ... "
>
> he means: " ... can have its name in a variable ... "
>
> That's what my parser says, anyway.

Oh yes! I see. My dyslexia inserted words to make "inner" parse in
that position, but I see that trying other token would have been better.

--
Ben.

Arno Welzel

unread,
Feb 15, 2016, 7:51:58 AM2/15/16
to
alex schrieb am 2016-02-14 um 16:45:

> <html>
> <body>
> Select a file to upload:
> <input type="file" id="myFile" onchange="myFunction()" size="50" >
> <p id="demo"></p>
> <script>
> function myFunction() {
> var vx = document.getElementById("myFile").value;
> document.getElementById("demo").innerHTML = vx;
> }
> </script>
> </body>
> </html>
>
>
> with the code above I can select a file in my pc and can have its name
> inner the variable vx;

Why is "myFile" not sufficient?

> I need have(to pass) this value (file's name), inner a php variable.

This is already done by the <input type="file"> element! You don't need
*any* JavaScript at all to handle file uploads in PHP.

See here as an example:

<http://www.w3schools.com/php/php_file_upload.asp>


--
Arno Welzel
http://arnowelzel.de
http://de-rec-fahrrad.de
http://fahrradzukunft.de

Erwin Moller

unread,
Feb 15, 2016, 8:49:01 AM2/15/16
to
Just curious...
Can you be a programmer when you "suffer" from dyslexia?

(Sounds like an endless source of confusion to me.
But I don't have dyslexia, so maybe it isn't that bad.)

Regards,
Erwin Moller

--
"That which can be asserted without evidence, can be dismissed without
evidence."
-- Christopher Hitchens

Ben Bacarisse

unread,
Feb 15, 2016, 2:20:18 PM2/15/16
to
Erwin Moller <erwinmol...@xs4all.nl> writes:

> On 2/14/2016 10:40 PM, Ben Bacarisse wrote:
>> Tim Streater <timst...@greenbee.net> writes:
>>
>>> In article <87lh6n4...@bsb.me.uk>, Ben Bacarisse
>>> <ben.u...@bsb.me.uk> wrote:
>>>
>>>> to tell me what you are really trying to do. I have no idea what an
>>>> inner variable is -- PHP has variables -- but you can give this value to
>>>
>>> When he says: " ... can have its name inner the variable ... "
>>>
>>> he means: " ... can have its name in a variable ... "
>>>
>>> That's what my parser says, anyway.
>>
>> Oh yes! I see. My dyslexia inserted words to make "inner" parse in
>> that position, but I see that trying other token would have been better.
>>
>
> Just curious...
> Can you be a programmer when you "suffer" from dyslexia?

I think so. I have a very good eye for syntax and detail, provided it's
not in natural language. I'll find it hard to see variables that are
misspelled, but that's very rarely been a problem.

Then again (and I am no expert) I suspect there are many closely related
conditions that go by the general term dyslexia, and of course it varies
in how strongly one affected. Mine is mild, so far as I know. It's
quite possible that there is a symbol-processing form that would make
programming quite a challenge.

<snip>
--
Ben.

praid...@gmail.com

unread,
Aug 17, 2016, 6:23:14 AM8/17/16
to
Exactly!

Why do you even NEED js here?

Use HTML to $GET or $POST input from a form...

and

direct it to a php script for processing after you hit [ENTER]
0 new messages