admin管理员组文章数量:1394194
<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.
<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.
-
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 Commented Aug 19, 2011 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 Commented Aug 19, 2011 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 es 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 Commented Aug 19, 2011 at 18:57 - oh sorry it was a wild try on my part forgot to change it back to .html – Osman Commented Aug 19, 2011 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 Commented Aug 19, 2011 at 19:10
4 Answers
Reset to default 3Your 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>
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.
Do you understand pletely, 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:
- 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 patibility 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.
本文标签: Display php using javascriptStack Overflow
版权声明:本文标题:Display php using javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744746938a2622938.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论