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>
kenorb
168k95 gold badges711 silver badges793 bronze badges
asked Feb 13, 2014 at 13:42
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

answered Feb 13, 2014 at 13:58
1

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

$(document).ready(function() {
 $.get('cityOptions.html', function(data) {
 $('#chooseCity').html(data);
 }); 
});
answered Feb 13, 2014 at 14:02
1
  • 1
    @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 Commented Feb 13, 2014 at 14:57
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>
answered Feb 13, 2014 at 13:45
1
  • thank you for your answer, but it is not solving the problem. Do you think the rest is ok? Commented Feb 13, 2014 at 13:48

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.