/    Sign up×
Community /Pin to ProfileBookmark

[RESOLVED] Set JavaScript Variable value to PHP variable

Hi experts,

I want get the selected text from a drop down box and store the value to a php value, tried this but its not working:

[code=php]<html>

<head>

<script type=”text/JavaScript”>
function jsfunc(x)
{
<?PHP
$y=print(“document.abc.mylist.options[x].text”);
echo (“Value is = “.$y);
?>
}
</script>
</head>

<body>
<form name=”abc” action=”get”>

<p><select size=”1″ name=mylist id=”D1″ onchange=”jsfunc(this.selectedIndex)”>
<option>Value 1</option>
<option>Value 2</option>
<option>Value 3</option>
</select></p>
</form>

</body>

</html>[/code]

Please help.

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@grifter7Aug 11.2009 — [CODE]<form action="yourefile.php" method="get">[/CODE]its set like this
Copy linkTweet thisAlerts:
@nyt1972authorAug 11.2009 — Thanks for the reply, I typed the action attribute in form by mistake, but it gives me the error:

Webpage error details

User Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET CLR 1.0.3705; .NET CLR 1.1.4322)

Timestamp: Tue, 11 Aug 2009 08:12:20 UTC


Message: Expected ';'

Line: 8

Char: 43

Code: 0

URI: http://localhost/testTest.php


Message: Object expected

Line: 15

Char: 1

Code: 0

URI: http://localhost/testTest.php

[/QUOTE]
Copy linkTweet thisAlerts:
@grifter7Aug 11.2009 — what youre trying to do is much more suited to javascript as PHP is server sided and theres no point in sending data to the server just to send it back again but if you must use [CODE]$youretextvariable=$_POST['document.abc.mylist.options[x].text'] only give the text value an id name [/CODE] the just echo this [CODE]$_GET['$youretextvariable'][/CODE]
Copy linkTweet thisAlerts:
@thraddashAug 11.2009 — You will need to actually "submit" the data back to your php server before you can refer to it. PHP does not have direct access to the DOM like you have it.

Use a submit button inside your <form> tags like:

[code=html]<input type="submit" value="Submit" />[/code]

Your data will end up in PHP as [B]$_GET['mylist'][/B] after it is submitted (because your method = "get").
×

Success!

Help @nyt1972 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...