简体   繁体   English

无法访问PHP中的其他变量

[英]Can't access other variable in php

i have problem here. 我在这里有问题。 i need to get $pilihdomain in searh_request.php to use in search_response.php i haven't access it yet. 我需要在searh_request.php中获得$pilihdomain才能在search_response.php中使用,我尚未访问它。

i define like this : 我这样定义:

settings.php settings.php

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

search_request.php 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 search_response.php

< <

?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';
}

?>

thanks for your help. 谢谢你的帮助。

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

Variable scope is not the problem here so you can take out global $pilihdomain. 可变范围不是这里的问题,因此您可以删除全局$ pilihdomain。

The only way that you'll be able to access the value of that variable is if you pass it as a GET or POST parameter. 访问该变量值的唯一方法是将其作为GET或POST参数传递。

search_request.php 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);
    }
})

In search_response.php , search_response.php中

$pilihdomain = $_GET['pilihdomain'];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM