无法访问php中的其他变量

时间:2013-06-13 17:53:28

标签: php variables get

我在这里遇到问题。 我需要在 searh_request.php 中获取$pilihdomain以用于 search_response.php 我还没有访问它。

我定义如下:

的settings.php

 <?php
    $dbHost = "localhost";
    $dbUser = "root";
    $dbPass = "";
    $dbname = "skripsi";
    $pilihdomain=$_POST['cmake'];
    global $pilihdomain;
    ?>

search_request.php

 <script src='jquery.min.js'></script>
    <script>
        jQuery(document).ready(function($) {

            // Run when Search button is clicked
            $('#search_button').click(function(){

                // This can take a few seconds so display progress text
                $('#search_results').html('Searching Twitter...');

                // Get the value of the input field
                // Encode it for use in a URL
                var search_value = encodeURIComponent($('input[name=search_terms]').val());

                // Send the search terms to the server in an Ajax request
                // This URL is for illustration only
                // You MUST change it to your server
                $.ajax({
                    url: 'http://localhost/kejar/code/search_response.php?q=' + search_value,
                    success: function(data){

                        // Display the results
                        $('#search_results').html(data);
                    }
                })
            })
        });
    </script>
    <?php require_once 'settings.php'; ?>
    Search: 
    <input name='search_terms' autofocus='autofocus'/>
     <?php
    $db = mysql_connect($dbHost,$dbUser,$dbPass);
    mysql_select_db($dbname,$db);
    $sql = mysql_query("SELECT * FROM namaklasifier");
    while($row = mysql_fetch_array($sql)) {
        $clsfr = $row['username'];
        $sql = mysql_query("SELECT * FROM namaklasifier");
            echo '<select name="cmake" autofocus width="10">';
            echo '<option value="0">-Pilih Domain Klasifikasi-</option>';
            while($row = mysql_fetch_array($sql)) {
                echo '<option ' . ($clsfr==$row['username']) . ' value="'.$row['username'].'">'.$row['username'].'</option>'; 
        }
        echo '</select>';
    }

    ?>
    <button id='search_button'>Search</button>
    <div id='search_results'></div>

search_response.php

&LT;

?php 
 require_once("uClassify.php");
 require_once 'settings.php';

    $uclassify = new uClassify();
    $uclassify->setReadApiKey('8DvvfxwKPdvjgRSrtsTSOawmQ0');
    $uclassify->setWriteApiKey('v4Us59yQFhf9Z0nGrQsrTtzBI5k');
if (!empty($_GET['q'])) {

    // Strip any dangerous text out of the search
    $search_terms = htmlspecialchars($_GET['q'].' -RT -@smartfrenworld -@smartfrencare -from:smartfrencare -from:smartfrenworld');

    // Create an OAuth connection
    require 'app_tokens.php';
    require 'tmhOAuth.php';
    $connection = new tmhOAuth(array(
      'consumer_key'    => $consumer_key,
      'consumer_secret' => $consumer_secret,
      'user_token'      => $user_token,
      'user_secret'     => $user_secret
    ));

    // Run the search with the Twitter API
    $http_code = $connection->request('GET',$connection->url('1.1/search/tweets'), 
                array('q' => $search_terms,
                'count' => 20,
                'lang' => 'in',
                'locale' => 'jakarta',
                'result_type' => 'recent'));

    // Search was successful
    if ($http_code == 200) {

        // Extract the tweets from the API response
        $response = json_decode($connection->response['response'],true);
        $tweet_data = $response['statuses']; 

        // Accumulate tweets from search results
        $tweet_stream = '';     
        foreach($tweet_data as $tweet) {

            // Ignore retweets
            if (isset($tweet['retweeted_status'])) {
                continue;
            }

        $resp = $uclassify->classify($tweet['text'], **$pilihdomain**, 'herlambangp');              
            $value = print_r($resp,true) ;   
            // Add this tweet's text to the results
            $tweet_stream .= $tweet['text'].' | '.$value.'<br/>';
        }

        // Send the result tweets back to the Ajax request
        print $tweet_stream;

    // Handle errors from API request
    } else {
        if ($http_code == 429) {
            print 'Error: Twitter API rate limit reached';
        } else {
            print 'Error: Twitter was not able to process that search';
        }
    }

} else {
    print 'No search terms found';
}

?>

感谢您的帮助。

2 个答案:

答案 0 :(得分:0)

你的文件应该返回JSON,然后你可以使用它并使用jquery来填充你的字段。

答案 1 :(得分:0)

变量范围不是问题所在,因此您可以取出全局$ pilihdomain。

您能够访问该变量值的唯一方法是将其作为GET或POST参数传递。

<强> search_request.php

var pilihdomain = '<?php echo $pilihdomain ?>';
$.ajax({
    url: 'http://localhost/kejar/code/search_response.php?q=' + search_value + '&pilihdomain=' + pilihdomain,
    success: function(data){
        // Display the results
        $('#search_results').html(data);
    }
})

search_response.php 中,

$pilihdomain = $_GET['pilihdomain'];