/    Sign up×
Community /Pin to ProfileBookmark

PhP + JS = How the hell do I do this

Is it possible to use a php variable in your JS event, for instance:

[CODE]$worked = “worked$month/$day/$year”;
onchange=”document.timesheet.$worked.value-=document.timesheet.off.value”[/CODE]

I know this is kind of outside simple php, but I thought someone might know.

Thanks!

to post a comment
PHP

10 Comments(s)

Copy linkTweet thisAlerts:
@bubbisthedogAug 22.2007 — [code=php]
$worked = 'worked$month/$day/$year';
[/code]

<i>
</i>onchange="document.timesheet.&lt;?php echo $worked; ?&gt;.value-=document.timesheet.off.value"
Copy linkTweet thisAlerts:
@Doc_ThirstauthorAug 22.2007 — Naw, its not that simple. I wish though. Here is the entire code snippet:


[CODE]echo "<input type=text size=2 value=8 name=vac$month/$calday/$year disabled>";
echo "<br><br>Out:<input type=text value=0 size=2 name=out$month/$calday/$year onchange=document.timesheet.vac$month/$calday/$year.value-=document.timesheet.out$month/$calday/$year.value onfocus=document.timesheet.vac$month/$calday/$year.value=8>";[/CODE]


it works here, without the dynamic variable names:
[CODE]
echo "<input type=text size=2 value=8 name=worked disabled>";
echo "<br><br>Out:<input type=text size=2 name=off onchange=document.timesheet.worked.value-=document.timesheet.off.value onfocus=document.timesheet.worked.value=8>";[/CODE]
Copy linkTweet thisAlerts:
@bubbisthedogAug 22.2007 — Well, now, that's quite different. :rolleyes: Try this:
[code=php]
echo '<input type="text" size="2" value="8" id="vac'.$month.'/'.$calday.'/'.$year.'" name="vac'.$month.'/'.$calday.'/'.$year.'" disabled="disabled" />';
echo '<br><br>Out:<input type="text" value="0" size="2" id="out'.$month.'/'.$calday.'/'.$year.'" name="out'.$month.'/'.$calday.'/'.$year.'" onchange="document.getElementById('vac'.$month.'/'.$calday.'/'.$year.'').value -= document.getElementById('out'.$month.'/'.$calday.'/'.$year.'').value" onfocus="document.getElementById('vac'.$month.'/'.$calday.'/'.$year.'').value="8">';
[/code]
Copy linkTweet thisAlerts:
@Doc_ThirstauthorAug 23.2007 — [CODE]echo '<input type="text" size="2" value="8" id="vac'.$month.'/'.$calday.'/'.$year.'" name="vac'.$month.'/'.$calday.'/'.$year.'" disabled="disabled" />';
echo '<br><br>Out:<input type="text" value="0" size="2" id="out'.$month.'/'.$calday.'/'.$year.'" name="out'.$month.'/'.$calday.'/'.$year.'" onchange="document.getElementById('vac'.$month.'/'.$calday.'/'.$year.'').value -= document.getElementById('out'.$month.'/'.$calday.'/'.$year.'').value" onfocus="document.getElementById('vac'.$month.'/'.$calday.'/'.$year.'').value="8">';[/CODE]


This doesn't fly either, is a php protected character, so the line 'vac'.$month.'/'.$calday.'/'.$year.'' for instance looks like vac'.$month.'/'.$calday.'/'.$year.' to the system.

This seems way to hard, there has to be an easier solution.
Copy linkTweet thisAlerts:
@bubbisthedogAug 23.2007 — An approach that I often take is doing the PHP inline. That's what I tried to show you earlier. The following [I]should[/I] work, but it's untested.
[code=php]
<input type="text" size="2" value="8" id="vac.<?php echo $month; ?>/<?php echo $calday; ?>/<?php echo $year; ?>" name="vac.<?php echo $month; ?>/<?php echo $calday; ?>/<?php echo $year; ?>" disabled="disabled" />
<br><br>Out:<input type="text" value="0" size="2" id="out<?php echo $month; ?>/<?php echo $calday;?>/<?php echo $year;?>" name="out<?php echo $month; ?>/<?php echo $calday;?>/<?php echo $year;?>"
onchange="document.getElementById('vac<?php echo $month; ?>/<?php echo $calday; ?>/<?php echo $year; ?>').value -= document.getElementById('out<?php echo $month; ?>/<?php echo $calday; ?>/<?php echo $year; ?>').value" onfocus="document.getElementById('vac<?php echo $month; ?>/<?php echo $calday; ?>/<?php echo $year; ?>').value="8">
[/code]
Copy linkTweet thisAlerts:
@Doc_ThirstauthorAug 23.2007 — An approach that I often take is doing the PHP inline. That's what I tried to show you earlier. The following [I]should[/I] work, but it's untested.
[code=php]
<input type="text" size="2" value="8" id="vac.<?php echo $month; ?>/<?php echo $calday; ?>/<?php echo $year; ?>" name="vac.<?php echo $month; ?>/<?php echo $calday; ?>/<?php echo $year; ?>" disabled="disabled" />
<br><br>Out:<input type="text" value="0" size="2" id="out<?php echo $month; ?>/<?php echo $calday;?>/<?php echo $year;?>" name="out<?php echo $month; ?>/<?php echo $calday;?>/<?php echo $year;?>"
onchange="document.getElementById('vac<?php echo $month; ?>/<?php echo $calday; ?>/<?php echo $year; ?>').value -= document.getElementById('out<?php echo $month; ?>/<?php echo $calday; ?>/<?php echo $year; ?>').value" onfocus="document.getElementById('vac<?php echo $month; ?>/<?php echo $calday; ?>/<?php echo $year; ?>').value="8">
[/code]
[/QUOTE]



OK man, you pretty much nailed it. I believe the value="8" is really all i HAD to change to get it to work. changed to value='8'" if I recall.

Regardless, I cleaned it up a bit (prob ended up closer to the first few suggestions) and here is the final product (line breaks for easier reading).

[CODE]?>
<input type="text" size="2" value="8" id="<?php echo "in$month/$calday/$year"; ?>" name="<?php echo "in$month/$calday/$year"; ?>" readonly />

<br><br>Out:<input type="text" value="0" size="2" id="<?php echo "out$month/$calday/$year"; ?>" name="<?php echo "out$month/$calday/$year"; ?>"

onchange="document.getElementById('<?php echo "in$month/$calday/$year"; ?>').value-=document.getElementById('<?php echo "out$month/$calday/$year"; ?>').value"
onfocus="document.getElementById('<?php echo "in$month/$calday/$year"; ?>').value='8';select()">
<?php
[/CODE]


Thanks a bunch everybody, its nice to put this two day issue to rest.
Copy linkTweet thisAlerts:
@bokehAug 23.2007 — [code=php]
$worked = 'worked$month/$day/$year';
[/code]
[/QUOTE]
This is wrong for variable expansion. You need double quotes, and for best practice use complex notation.[code=php]
$worked = "worked{$month}/{$day}/{$year}";
[/code]
Copy linkTweet thisAlerts:
@bubbisthedogAug 23.2007 — bokeh, I always appreciate your critiques. ? Thank you. But what in the world is "complex notation" with regards to PHP syntax? I Google it and can't seem to find anything.
Copy linkTweet thisAlerts:
@bubbisthedogAug 24.2007 — [I]Interesting.[/I] I had no idea. Thanks a lot, bokeh; let's hope the OP takes this advice.
×

Success!

Help @Doc_Thirst 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.2,
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: @Yussuf4331,
tipped: article
amount: 1000 SATS,

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

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...