Todays Date: echo $date; ?>
Are you applying for a day, evening, or weekend class?
Do you understand completely, the difference between running scripts on server vs running HTML/JavaScript on web-client ?
PHP is executed on server. It renders HTML and JS. Then HTML/JS goes to a viewer (web-client) and is executed on client's machine... You can't "run PHP inside JS" the way you want.
If you need some data from PHP injected to your JS code you may try running AJAX.
Checklist of simple, simple things that we don't want to overlook:
That should work. Make sure it's saved as a .php file, and you should be good. Oh, and preferably you'd use <?php .. ?>
instead of <? ?>
since shorttags are advised against for compatibility reasons.
Note If your PHP was actually executing, you'd be getting syntax errors thrown into your page because your echo
doesn't have the string in quotes, among other things.
PHP is a server-side language. PHP is executed by the server, and the result (usually some HTML) is sent to the client for display. If you're trying to display PHP code literally, then it's definitely possible. I get the feeling, however, that you're trying to execute the PHP code in Javascript. You can't do that, because Javascript is executed client-side -- that is, after the page has already been sent to the client's browser from the server.
Your javascript does not have access to your PHP variables, unless you request them via AJAX.
However, you don't have to use AJAX. You could use your PHP code to build the Javascript code before it is sent to the browser!!
Here's one way of doing it:
<?php
$start1 = '01/01/2010';
$start2 = '11/11/2010';
$start3 = '03/12/2010';
// and so on...
?>
<p>Today's Date: <?php echo $date; ?></p>
<p>Are you applying for a day, evening, or weekend class?</p>
<select name="date" id="wclass">
<option value="day">Day</option>
<option value="evening">Evening</option>
<option value="weekend">Weekend</option>
</select>
Program Start Date:
<div id="dates"></div>
<script language="javascript">
$(document).ready(function() {
{
$("#wclass").change(function ()
{
if( $(this).val() == 'day' )
{
$('#dates').html('<select name="date">\
<option value="date1"><?php echo $start1; ?></option>\
<option value="date2"><?php echo $start2; ?></option>\
<option value="date3"><?php echo $start3; ?></option>\
<option value="date4"><?php echo $start4; ?></option>\
<option value="date5"><?php echo $start5; ?></option>\
<option value="date6"><?php echo $start6; ?></option>\
<option value="date7"><?php echo $start7; ?></option>\
</select>');
}
});
});
</script>
An even better option would be to just create the second select
outright, and then show/hide it when needed:
<?php
$start1 = '01/01/2010';
$start2 = '11/11/2010';
$start3 = '03/12/2010';
// and so on...
?>
<p>Today's Date: <?php echo $date; ?></p>
<p>Are you applying for a day, evening, or weekend class?</p>
<select name="date" id="wclass">
<option value="day" selected="selected">Day</option>
<option value="evening">Evening</option>
<option value="weekend">Weekend</option>
</select>
Program Start Date:
<select id="dates" name="date">
<option value="date1"><?php echo $start1; ?></option>
<option value="date2"><?php echo $start2; ?></option>
<option value="date3"><?php echo $start3; ?></option>
<option value="date4"><?php echo $start4; ?></option>
<option value="date5"><?php echo $start5; ?></option>
<option value="date6"><?php echo $start6; ?></option>
<option value="date7"><?php echo $start7; ?></option>
</select>
<script language="javascript">
$(document).ready(function() {
{
$("#wclass").change(function ()
{
$(this).val() == 'day' ? $('#dates').show() : $('#dates').hide()
});
});
</script>