jquery - Can I have a coldfusion tag inside a Javascript tag? - Stack Overflow

I'm trying to print out info from a database using JQuery and coldfusion to display info of a stud

I'm trying to print out info from a database using JQuery and coldfusion to display info of a student and their parking permit info, and other things related to the car they have on campus. When I try to load the webpage, I get an error saying that on line 84:

<cfquery name="q_sample" datasource="cars_live">

There is an unexpected token: "<". I'm guessing this is because it's already under the javascript tag. Is there any way to make JS and coldfusion work together, because in order to read what I want from the database, I need <cfquery name="q_sample" datasource="cars_live"> and $(this).text().

Here is the code for the student info page. #plates is just the name of the list with the item that the user clicked that brought them to this page.

<div data-role="page" id = 'Student' data-add-back-btn="true">
    <div data-role="header">
        <h1>Student Info Page</h1>
    </div><!-- /header -->
    <div data-role="content">
        <script type="text/javascript">
            $("#plates li").click(function() {
        <cfquery name="q_sample" datasource="cars_live">
            SELECT FIRST 10 *
            FROM veh_rec WHERE LICENSE=$(this).text()
        </cfquery>       
        <cfoutput query="q_sample">
            <p>License Plate Number: #license#, <br> Permit ID Number: #decal#, Student ID Number: #ID#</p>
        </cfoutput>
            });             
        </script>
    </div> <!-- /content -->
</div> <!--/Student -->

If you need any additional information please let me know!

UPDATE After taking Steve's advice, this is my new code.

joey.cfm

<cfparam name="License" default="">

<cfquery name="q_sample" datasource="cars_live">
  SELECT * FROM veh_rec WHERE LICENSE=<cfqueryparam cfsqltype="cf_sql_varchar" value="#License#">
</cfquery>       
<cfoutput query="q_sample">
  <p>License Plate Number: #license# <br> Permit ID Number: #decal#<br> Student ID Number: #ID#</p>
</cfoutput>

Part of html file

<div data-role="page" id = 'Student' data-add-back-btn="true">
    <div data-role="header">
        <h1>Student Info Page</h1>
    </div><!-- /header -->
    <div data-role="content">
        <script type="text/javascript">
            $("#plates li").click(function() {

                var strLicense=$(this).text();

                $.get("joey.cfm", { license: strLicense})
                .done(function(data) {
                  alert("Data Loaded: " + data);
                  $("#myResults").html(data);
                });
            });       
        </script>
        <div id="myResults"></div>

only problem now is that I cant get a return value unless I hard code in the license plate in the "value" area.

I'm trying to print out info from a database using JQuery and coldfusion to display info of a student and their parking permit info, and other things related to the car they have on campus. When I try to load the webpage, I get an error saying that on line 84:

<cfquery name="q_sample" datasource="cars_live">

There is an unexpected token: "<". I'm guessing this is because it's already under the javascript tag. Is there any way to make JS and coldfusion work together, because in order to read what I want from the database, I need <cfquery name="q_sample" datasource="cars_live"> and $(this).text().

Here is the code for the student info page. #plates is just the name of the list with the item that the user clicked that brought them to this page.

<div data-role="page" id = 'Student' data-add-back-btn="true">
    <div data-role="header">
        <h1>Student Info Page</h1>
    </div><!-- /header -->
    <div data-role="content">
        <script type="text/javascript">
            $("#plates li").click(function() {
        <cfquery name="q_sample" datasource="cars_live">
            SELECT FIRST 10 *
            FROM veh_rec WHERE LICENSE=$(this).text()
        </cfquery>       
        <cfoutput query="q_sample">
            <p>License Plate Number: #license#, <br> Permit ID Number: #decal#, Student ID Number: #ID#</p>
        </cfoutput>
            });             
        </script>
    </div> <!-- /content -->
</div> <!--/Student -->

If you need any additional information please let me know!

UPDATE After taking Steve's advice, this is my new code.

joey.cfm

<cfparam name="License" default="">

<cfquery name="q_sample" datasource="cars_live">
  SELECT * FROM veh_rec WHERE LICENSE=<cfqueryparam cfsqltype="cf_sql_varchar" value="#License#">
</cfquery>       
<cfoutput query="q_sample">
  <p>License Plate Number: #license# <br> Permit ID Number: #decal#<br> Student ID Number: #ID#</p>
</cfoutput>

Part of html file

<div data-role="page" id = 'Student' data-add-back-btn="true">
    <div data-role="header">
        <h1>Student Info Page</h1>
    </div><!-- /header -->
    <div data-role="content">
        <script type="text/javascript">
            $("#plates li").click(function() {

                var strLicense=$(this).text();

                $.get("joey.cfm", { license: strLicense})
                .done(function(data) {
                  alert("Data Loaded: " + data);
                  $("#myResults").html(data);
                });
            });       
        </script>
        <div id="myResults"></div>

only problem now is that I cant get a return value unless I hard code in the license plate in the "value" area.

Share Improve this question edited May 31, 2013 at 18:49 Riot Goes Woof asked May 31, 2013 at 15:59 Riot Goes WoofRiot Goes Woof 3,5145 gold badges22 silver badges35 bronze badges 5
  • 3 Are you really want the cold fusion tags parse on the fly when you click some element on your page?!! You must change your algorithm. You must use ajax. – user1823761 Commented May 31, 2013 at 16:03
  • 1 No, that you are trying wont work. Your query would be loaded up and cause an error when the page is rendered. You would need to use the jquery .get or .post to a server side function and return the text. Also you may want to look and see if the cfdiv tag may be of use to you. – steve Commented May 31, 2013 at 16:06
  • 1 Javascript is executed on the client, Coldfusion is executed on the server. They do not interact in this way. – nullability Commented May 31, 2013 at 16:06
  • 2 I haven't used ColdFusion much, but I understand that it is like PHP or ASP. I think ColdFusion parsing long done by the time the client gets it. Javascript happens on the client side. You can output JS from ColdFusion, but it looks like you're trying to make a CF call with Javascript input. That won't happen without a JSON request or something. – Doug Dawson Commented May 31, 2013 at 16:07
  • Alright, thanks for the info. Guess it wont work like that. I read up on cfdiv, and it looks like I could get somewhere with that. – Riot Goes Woof Commented May 31, 2013 at 16:15
Add a ment  | 

1 Answer 1

Reset to default 7

This code is inplete, but will get you started: I have 2 files here... your main file, and then a very simple file to retrieve the data from the server. You can make a cfc to handle it any way you would like, but this will get you started.

Main File:

<div data-role="page" id = 'Student' data-add-back-btn="true">
    <div data-role="header">
        <h1>Student Info Page</h1>
    </div><!-- /header -->
    <div data-role="content">
        <div id="result">


        </div>
    </div> <!-- /content -->
</div> <!--/Student -->

<script type="text/javascript">
    $("#plates li").click(function() {

        var strLicense=$(this).text();

        $.get("getDetails.cfm", { license: strLicense})
        .done(function(data) {
          alert("Data Loaded: " + data);
          $("#result").text(data);
        });
    });             
</script>

GetDetails.cfm

<cfparam name="License" default="">

<cfquery name="q_sample" datasource="cars_live">
  SELECT FIRST 10 *
  FROM veh_rec WHERE LICENSE=<cfqueryparam cfsqltype="cf_sql_varchar" value="#License#">
</cfquery>       
<cfoutput query="q_sample">
  <p>License Plate Number: #license#, <br> Permit ID Number: #decal#, Student ID Number: #ID#</p>
</cfoutput>

Your click method will pass your string (I made a variable called license) to jquery and then the .get function will call a new file, getdetails.cfm and pass the url.variable as license. Your query will then run and pass the results back to the .get function.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744825529a4595772.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信