Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

help me to retrive the php array using ajax from one page to another

when ever user start input in that text field,at that time only it has to retrive the data from page2 using ajax

        <!doctype html> //page1
        <html lang="en">
        <head>
          <meta charset="utf-8">
          <title>jQuery UI Autocomplete - Default functionality</title>
          <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
          <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
          <script src="http://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
          <link rel="stylesheet" href="/resources/demos/style.css">
          <script>
          $(function() {
        var movies = <?php echo json_encode($varma); ?>; // here i want to pass that php array using ajax

            alert(JSON.stringify(movies));
            $( "#tags" ).autocomplete({
              source: movies
            });
          });
          </script>
        </head>
        <body>

        <div class="ui-widget">
          <label for="tags">Tags: </label>
          <input id="tags"> // input field
        </div>


        </body>
        </html>



        <?php //page 2
        $varma=array("ActionScript","AppleScript","Java","JavaScript","Lisp","Perl","PHP","Python","Ruby","Scala","Scheme"); //php array
    ?>
share|improve this question
    
I don't understand your question. Not sure this is your question but autocomplete can use ajax too. –  putvande Jan 23 at 13:23
    
@DigitalChris He's using that in his assignment var movies = –  Barmar Jan 23 at 13:24
add comment

closed as unclear what you're asking by putvande, Dhaval Marthak, davidkonrad, gustavohenke, g19fanatic Jan 23 at 19:03

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question.If this question can be reworded to fit the rules in the help center, please edit the question.

3 Answers

The source attribute can have an URL as value. The URL must render json formated for the plugin.

See http://api.jqueryui.com/autocomplete/#option-source

$( "#tags" ).autocomplete({
              source: '/myMovies.php'
            });

/myMovies.php

<?php echo json_encode($varma); ?>;
share|improve this answer
add comment

Here is a more generic method to ajax in php.

Construct the php array

$arReturn = array( 'name' => 'AliasM2K' );
header( 'Content-Type: application/json' );
print json_encode( $arReturn );

Perform ajax

$.ajax({

    url: 'ajaxPhp.php',
    dataType: 'json',
    success: function( oData ) {
        alert( oData.name );
    }

});
share|improve this answer
add comment
<?php 

/*
   I read your problem and your code also and the suggestion from my side is :
    Some how you don't required any second page and doesn't required ajax for all this.
    You can autocomplete your textbox with PHP array's values using below successive code..
    Do yourself and enjoy.
 */
?>
<!doctype html> 
<html lang="en">
<head>
<meta charset="utf-8">
<title>jQuery UI Autocomplete - Default functionality</title>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
<link rel="stylesheet" href="/resources/demos/style.css">
<?php
    $title_name = array();
    $fetch=array("ActionScript","AppleScript","Java","JavaScript","Lisp","Perl","PHP","Python","Ruby","Scala","Scheme"); //php array
    foreach ($fetch as $data)
    {
        array_push($title_name,$data);  } ?>
<script>
    $(function(){ 
                var availableTags =<?php echo json_encode($title_name)?>;
                //PUT TEXTBOX ID here 
                $( "#tags" ).autocomplete({   source: availableTags }); 
            }); 
</script>        
</head>
<body>
    <div class="ui-widget">
        <label for="tags">Tags: </label>
        <input id="tags"> // input field
    </div>
</body>
 </html>
share|improve this answer
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.