0

I have been trying to append html data from a html file to a html page. It seems to be something very simple, but it is not working! Can you help me?

<html>
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.3.2.js"></script>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.4.1.js"></script>
<script type="text/javascript">
    $(document).ready(function() {
        var theCitiesList="<?php echo htmlentities(file_get_contents("cityOptions.html")); ?>";
        $("select#chooseCity").html(theCitiesList);                 
    });
</script>
</head>
<body> 
    <FORM id="searchForm">
        <h2>Selecione uma cidade: </h2>            
        <select id="chooseCity"></select>
    </FORM>
</body>  
</html>     

cityOptions.html is basically a long list of cities such as:

        <option value="" selected></option>
        <option value="1">California</option>
        <option value="2">New York</option>
5
  • Instead of require, check out file_get_contents instead us1.php.net/file_get_contents Commented Feb 13, 2014 at 13:45
  • 2
    why include two jquery (1.3.2 and 1.4.1) in one file? Commented Feb 13, 2014 at 13:46
  • @Chen-TsuLin thank u! I have just removed the oldest! Commented Feb 13, 2014 at 13:50
  • Having PHP code in a javascript variable that is then used in .html() won't cause the PHP code to be executed. Perhaps using jQuery's .ajax() methods you can retrieve the cityOptions.html content, and insert it? Commented Feb 13, 2014 at 13:51
  • @SLoW Sorry, what do you mean? The php require part of the code was commented! I removed it! Commented Feb 13, 2014 at 13:52

3 Answers 3

2

You can use JQuery .load() like;

$("select#chooseCity").load("cityOptions.html");

This will load cityOptions.html in to your specified html area

Sign up to request clarification or add additional context in comments.

Comments

1

.load() is shortcut of $.get():

$(document).ready(function() {
    $.get('cityOptions.html', function(data) {
        $('#chooseCity').html(data);
    });            
});

1 Comment

@DanielTheRocketMan I have sent answer before this answer. You were able to do it with .load(). However, you have select longer version of .load(). This seems strange
0

cityOptions.html had newlines \n. replacing it helps.

<html>
<head>       
    <script type="text/javascript" src="http://code.jquery.com/jquery-1.4.1.js"></script>
    <script type="text/javascript">
        $(document).ready(function() {
            var theCitiesList='<?php echo str_replace(array("\r\n", "\n", "\r"), "",file_get_contents("cityOptions.html"));  ?>';
            $("select#chooseCity").html(theCitiesList);                 
        });
    </script>
</head>
<body>
    <form id="searchForm">
        <h2>Selecione uma cidade: </h2>            
        <select id="chooseCity">

        </select>
    </form>
 </body>

1 Comment

thank you for your answer, but it is not solving the problem. Do you think the rest is ok?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.