hello. im not sure if this is the right forum because it is about php echo AND js but since it is about echoing i thought this forum will be better. sorry if i asked in the wrong forum.

Anyway, so i want to know if echoing a js script will work? Like for instance as usual we write js in <head> but if it were to be echoed, is that something that is done?

i was thinking of echoing a script for datetimepicker plugin.

i tested this:

echo "<script src='js/jquery 1.8.2.js'></script>";
echo "<script src='js/jquery-ui 1.8.2.min.js'></script>";
echo 
    "<script type='text/javscript'>
        $(document).ready(function(){
            $(\"#button\").live(\"click\", function(){
                alert(\"click\");
            });
        });
    </script>";

nothing happens on click though, so is it not allowed or something?

yes, im using an older version of js when i can just use the latest and .on() but i will upgrade soon.

TIA!

Dani AI

Generated

Yes, echoing script tags from PHP is fine. The browser only sees the final HTML, so what matters is where and in what order the scripts appear in the output. Common pitfalls are: placing the echoed scripts before jQuery is loaded, outputting them after the closing </html>, typos in the type attribute, and quoting/escaping issues. In HTML5 you can omit type (the default is text/javascript), which avoids spelling mistakes altogether; see MDN: script.

If you are wiring up click handlers or a date/time picker, load jQuery first, then any plugins, then your inline code. Put them near the end of <body> or use DOM-ready. Also note that live() is deprecated and removed in jQuery 1.9; prefer delegated handlers with on() as shown below (jQuery .on, jQuery .live).

<?php
// Echo near the end of </body> so elements exist when code runs.
echo '<script src="/js/jquery-1.12.4.min.js"></script>';
echo '<script src="/js/jquery-ui.min.js"></script>';
echo '<link rel="stylesheet" href="/css/jquery-ui.min.css">';
echo '<script>
  jQuery(function($){
    // Works even if .js-demo is added later
    $(document).on("click", ".js-demo", function(){ alert("clicked"); });
    $("#date").datepicker(); // if you are using jQuery UI
  });
</script>';

For a date/time picker specifically, ensure the plugin’s JS and CSS are included after jQuery and initialize it after load; for jQuery UI see Datepicker docs. Always check your browser console for errors if nothing happens.

Recommended Answers

All 5 Replies

Try not to let this embarrass you too much ok?

You misspelled "javascript"

<script type='text/javscript'> <!-- vowels bro/sis, VOWELS! -->

ah crap. well either way that did not solve it. thanks for pointing that out though. btw should i have posted this in the js forum instead? :s

Well I guess we'll have to determine that after we know what the problem is, however the issue does seem to be JS related.

Is there any other JS on the page that could be broken?

Because here's what I'm testing:

<?php
    echo "<script src='https://code.jquery.com/jquery-1.8.2.min.js'></script>";
    echo "<script src='https://code.jquery.com/ui/1.9.2/jquery-ui.js'></script>";

    echo 
        "<script type='text/javascript'>
            $(document).ready(function(){
                $(\"#button\").live(\"click\", function(){
                    alert(\"click\");
                });
            });
        </script>";
?>

<input type="button" id="button" value="Click Me" />

As you can see, other than changing the path's of jQuery to the hosted one (i even change it back to 1.8.2 to match the version you're using) ... the only other thing I change in your script is the spelling of "javascript" and it works for me.

Everything you're using there should have no version issues either, because even though I would directly use click(), using live() should work because it was not removed until version 1.9

it was actually because of where i placed the echoed script. got confused with these two file im working with. so now i will try with that plugin i mentioned. thanks for your help.

Ahh, glad you got it working! Don't forget to mark the thread solved if you got everything you needed.

Cheers!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.