0

Sorry for my bad English, I don't understand javascript but I need a function. I try this code

<div class="well col-md-5">
   <input id="demo2" type="text" class="col-md-12 form-control" placeholder="Search cities..." autocomplete="off" />
</div>
<div class="col-md-7">
    <pre class="prettyprint">
    </pre>
</div>
<script>

$(function() {

    function displayResult(item) {
        $('.alert').show().html('You selected <strong>' + item.value + '</strong>: <strong>' + item.text + '</strong>');
    }                    

    $('#demo2').typeahead({
        source: [
            {ID: 1, Name: 'Toronto'},
            {ID: 2, Name: 'Montreal'},
            {ID: 3, Name: 'New York'},
            {ID: 4, Name: 'Buffalo'},
            {ID: 5, Name: 'Boston'},
            {ID: 6, Name: 'Columbus'},
            {ID: 7, Name: 'Dallas'},
            {ID: 8, Name: 'Vancouver'},
            {ID: 9, Name: 'Seattle'},
            {ID: 10, Name: 'Los Angeles'}
        ],
        displayField: 'Name',
        valueField: 'ID',
        onSelect: displayResult
    });    

});
</script>

It works. But I have a file clients.php updated 2 times each day with this code

<?php
$client_listing ="
{ID: 1, Name: 'Toronto'},
{ID: 2, Name: 'Montreal'},
{ID: 3, Name: 'New York'},
{ID: 4, Name: 'Buffalo'},
{ID: 5, Name: 'Boston'},
{ID: 6, Name: 'Columbus'},
{ID: 7, Name: 'Dallas'},
{ID: 8, Name: 'Vancouver'},
{ID: 9, Name: 'Seattle'},
{ID: 10, Name: 'Los Angeles'}";
?>

I try to replace $client_listing to my first code but it doesn't work

<?php include('data/clients.php'); ?>
<script>

$(function() {

    function displayResult(item) {
        $('.alert').show().html('You selected <strong>' + item.value + '</strong>: <strong>' + item.text + '</strong>');
    }                    

    $('#demo2').typeahead({
        source: [
            <?php echo $client_listing; ?>
        ],
        displayField: 'Name',
        valueField: 'ID',
        onSelect: displayResult
    });    

});
</script>

Thank you for the help

Didier

2 Answers 2

1

You should try with:

<?php 
 $client_listing ='
{"ID": 1, "Name": "Toronto"},
{"ID": 2, "Name": "Montreal"},
{"ID": 3, "Name": "New York"},
{"ID": 4, "Name": "Buffalo'},
{"ID": 5, "Name": "Boston"},
{"ID": 6, "Name": "Columbus"},
{"ID": 7, "Name": "Dallas"},
{"ID": 8, "Name": "Vancouver"},
{"ID": 9, "Name": "Seattle"},
{"ID": 10, "Name": "Los Angeles"}';
?>
Sign up to request clarification or add additional context in comments.

Comments

0

Try this in data/clients.php

<?php
function getClients()
{
    $clients = array(
        array('ID' => 1, 'Name' => 'Toronto'),
        array('ID' => 2, 'Name' => 'Montreal'),
        array('ID' => 3, 'Name' => 'New York'),
        array('ID' => 4, 'Name' => 'Buffalo'),
        array('ID' => 5, 'Name' => 'Boston'),
        array('ID' => 6, 'Name' => 'Columbus'),
        array('ID' => 7, 'Name' => 'Dallas'),
        array('ID' => 8, 'Name' => 'Vancouver'),
        array('ID' => 9, 'Name' => 'Seattle'),
        array('ID' => 10, 'Name' => 'Los Angeles')
    );

    return json_encode($clients);
}
// no end php tag

In your js section

$('#demo2').typeahead({
    source: <?php echo getClients(); ?>,
    displayField: 'Name',
    valueField: 'ID',
    onSelect: displayResult
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.