Display php using javascript

前端 未结 4 1881
太阳男子
太阳男子 2021-01-29 10:35
    

Todays Date:

Are you applying for a day, evening, or weekend class?

提交评论

  • 2021-01-29 10:58

    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.

    0 讨论(0)
  • 2021-01-29 11:06

    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.

    0 讨论(0)
  • 2021-01-29 11:11

    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>
    
    0 讨论(0)
  • 提交回复
    热议问题