0

I have an input field that I want to populate with a bunch of possible sources, but I need to grab these sources from several different areas: a file on my server and some stuff that is already in jQuery.

The input field is going to show up inside an iFrame, so I have a .php that will echo some html when it is called up as a source.

<?php
$array = array('Louisville, KY', 'Denver, CO');
echo "
<html>
<head>
<link rel='stylesheet' href='//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css'>
  <script src='//code.jquery.com/jquery-1.10.2.js'></script>
  <script src='//code.jquery.com/ui/1.10.4/jquery-ui.js'></script>
  <link rel='stylesheet' href='/resources/demos/style.css'>
  <script>
  $(function() {
    var availableTags = [
      'Boston, MA',
      'New York City, NY',
      'Los Angelos, CA'
    ];
    availableTags = $.merge(availableTags, ".$array.");

    $( '#tags' ).autocomplete({
      source: availableTags
    });
  });
  </script>
  </head>
<body>
Select a voting option.
<br>
<span id='options'>
<form method='post' action='/creations/tools/poll/pollstorage.php' target='mainframe'>
<input id='tags' name='score'/>
<input type='submit' value='Vote'/>
</form>
<br>
</span>
</body>
</html>
"
?>

In the jQuery that I will be echoing to the iframe, you can see that I try to merge together an existing jQuery array and the output of a PHP array: availableTags = $.merge(availableTags, ".$array.");. I tried it and none of the jQuery works.

What am I doing wrong here? How should I go about merging the PHP array and the jQuery array?

5
  • PHP arrays are very different then Javascript arrays. Commented May 13, 2014 at 14:19
  • ik, thats why im asking how to do this. Commented May 13, 2014 at 14:19
  • 2
    Sidenote: You have a missing closing semi-colon just before your closing PHP tag. Commented May 13, 2014 at 14:21
  • 1
    If you just want to send php array then convert it to json by using json_encode($arrayList); in php and return than json to jquery. Json can be used as simple as array in jQuery Commented May 13, 2014 at 14:21
  • 1
    thanks for catching that @Fred-ii- Commented May 13, 2014 at 14:22

4 Answers 4

1

Use json_encode():

$.merge(availableTags, ". json_encode($array).");
Sign up to request clarification or add additional context in comments.

3 Comments

The rest of the jQuery code has started to work again, but the arrays are still not merged.
Oh, nevm... I got it! THANKYOUTHANKYOUTHANKYOU. Geez, this site is a lifesaver...
Just for future reference, availableTags was overwritten. I'll update the answer.
1

You can convert php array in to json file and use it in java-Script or jquery. use php json_encode

Comments

1
var php_array = [<?php echo '"'.implode('","',  $array).'"' ?>];
availableTags = $.merge(availableTags,php_array );

Comments

0

I think the best solution is using json.

In PHP create a page than output a json data that you need:

page data.json

<?php 
    $obj['jsArray']= array('Boston, MA','New York City, NY','Los Angelos, CA');
    echo json_encode($obj);
?>

In your Html page using getJSON from load your page with json data (I am used a pseudo code not tested):

$.getJSON( "data.json", function( data ) {
  var availableTags = data.jsArray 
});

or, if you have availableTags and will add new data:

$.getJSON( "data.json", function( data ) {
  availableTags.push(data.jsArray)
});

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.