简体   繁体   中英

How to add user input into a existing SPARQL query

I want to integrate the user's input into a SPARQL query. Here's my existing code but I cannot get it to work.

javascript in external JS file:

    $('#submit99').on('click', function(e) {
    var userInput = $('#messageInput').val();

    var query = "PREFIX own: <http://www.owl-ontologies.com/travel.owl#>" +
                "PREFIX trvl: <http://www.owl-ontologies.com/travel.owl#>" +
                "select distinct ?label where {" +
                "?s a trvl:" + userInput + " ."
                "OPTIONAL {" +
                "?type rdfs:label ?label }" +
                "}";
    var endpoint = 'http://localhost:5820/finalProject/query';
    var format = 'JSON';


    $.get('/sparql',data={'endpoint': endpoint, 'query': query, 'format': format}, function(json){
        var pre = $('<pre></pre>');
        pre.text(JSON.stringify(json));
        $('#linktarget99').html(pre);
    });
});

my form:

<div class="header">
    <div class="container">
        <h1> Zoek de plek voor je favoriete activiteiten </h1>
        <p class="beschrijving"> Wil je weten op welke plek jij bepaalde activiteiten kunt doen? Vul hieronder de activiteit in en wij zoek het voor je uit! <a href="tutorial.html"> tutorial </a> </p>
        <form role='form'>
            <div class='form-group'>
                <textarea class='form-control' id='messageInput' placeholder='type some nifty schilderij' row='1'> </textarea>
            </div>
        </form>
        <div><a id="submit99" class='btn btn-primary'>Zoek de locatie van het schilderij</a></div>
    </div>
</div>

This is the error I get from the terminal:

127.0.0.1 - - [14/Oct/2015 20:02:52] "GET /sparql?endpoint=http%3A%2F%2Flocalhost%3A5820%2FfinalProject%2Fquery&query=PREFIX+own%3A+%3Chttp%3A%2F%2Fwww.owl-ontologies.com%2Ftravel.owl%23%3EPREFIX+trvl%3A+%3Chttp%3A%2F%2Fwww.owl-ontologies.com%2Ftravel.owl%23%3Eselect+distinct+%3Flabel+where+%7B%3Fs+a+trvl%3A++BedAndBreakfast.&format=JSON HTTP/1.1" 200 -

It's fixed. The value given by the userinput container spaces which (...) up the code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM