-1
    <p>Todays Date: <? echo $date; ?></p>
    <p>Are you applying for a day, evening, or weekend class? 
    <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() {
{ setInterval(function () {

if ($("#wclass").val()=='day')

  {   $('#dates').html("<? echo <select name='date'>
<option value ='date1'> $start1 </option>
 <option value ='date2'>$start2</option>
 <option value ='date3'>$start3</option>
 <option value ='date4'>$start4</option>
  <option value ='date5'>$start5</option>
   <option value ='date6'>$start6</option>
   <option value ='date7'>$start7</option>
  </select> }?>");} 
  }, 1000); 
});

My issue is that i am not sure how to display php using javascript. The variables are all correct, the issue is to get the php to display as html would in my .html. All i want to do is display the php variables which i fetched in the beginning. So the variables have been defined in php now i want to turn them into a html list based on my javascript. You may notice that the echo is missing quotes, thats because i dont know how to put quotes, i cant use " or ' because they both interup quotes that are already there.

Osman
  • 1,771
  • 4
  • 24
  • 47
  • 4
    What is `$(...).php()` here? Why do you think such a function exists? If it is a plugin that does some magic, then tell us. I think you are missing some basic understanding of when PHP and JavaScript are executed. PHP runs on the server side and JavaScript on the client side. You can generate HTML, text and JavaScript with PHP, but once the data leaves the server, PHP's job is done. Besides, what you wrote is not even valid PHP code. – Felix Kling Aug 19 '11 at 18:51
  • What is .php() that you're calling on the dates selector? Also, you should put some escaped quotes around the echo text. – CashIsClay Aug 19 '11 at 18:54
  • Can more information be provided, such as where `$start1` and the rest of those similar variables are constructed? I don't think we have the full picture. Although, as described in my answer below, I'm guessing your problem comes from using short tags (disabled by default) and thus the PHP code isn't running. If the PHP were actually running, you'd be getting errors. – Guttsy Aug 19 '11 at 18:57
  • oh sorry it was a wild try on my part forgot to change it back to .html – Osman Aug 19 '11 at 19:01
  • "You may notice that the echo is missing quotes, thats because i dont know how to put quotes" No, they don't. Wrapping a string in quotes in PHP does nothing to the HTML output -- the browser won't see that. – Guttsy Aug 19 '11 at 19:10

4 Answers4

3

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.

Jared Ng
  • 4,891
  • 2
  • 19
  • 18
  • no what i did was i got the information from the server and saved it as variables. Now i want to display these variables, thats it. – Osman Aug 19 '11 at 19:03
3

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>
Joseph Silber
  • 214,931
  • 59
  • 362
  • 292
  • You didn't `echo` any of the `$start` variables. An explanation to your answer wouldn't hurt either. – Guttsy Aug 19 '11 at 19:02
  • Much better. Also, +1 for actually realizing what OP is trying to do here IMO. Just including information... – Guttsy Aug 19 '11 at 19:08
  • this is what im trying to do , thanks. However its giving me some trouble, im working on it now but this is definitely on the right track ill post again if i get it to work. – Osman Aug 19 '11 at 19:14
0

Checklist of simple, simple things that we don't want to overlook:

  • You saved the file with a .php extension.
  • Short tags are enabled.

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.

Guttsy
  • 2,130
  • 1
  • 18
  • 29
0

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.

Roman Pietrzak
  • 635
  • 3
  • 15
  • the variables have been defined in the page earlier using php, not i want to use them to make a html list. Without the javascript the list works,how do i get the list to work when its part of the javascript. So your saying AJAX is the only way i can display the php, i already have the information i need when the page loads, i just need to display it, AJAX is needed when you want to check something or things that are dynamic and cant be loaded with the page, my variables are set dates and i already have them defined. – Osman Aug 19 '11 at 19:00
  • So simply use php to render the javascript you want... javascript and HTML is totally transparently the same for PHP. If your code is able to render HTML, then it is also able to render JS. Can you just see "view source" on your HTML viewer and see if the JS is properly rendered ? – Roman Pietrzak Aug 19 '11 at 19:16
  • and - in your code - the "echo – Roman Pietrzak Aug 19 '11 at 19:18
  • @yosh or possibly `echo $something;` for variables, of course. Actually, `echo SOMETHING;` would work if you defined SOMETHING with `define();` Otherwise PHP will just complain at you. – Guttsy Aug 19 '11 at 19:21