duanpendan8067 2014-07-23 22:04
浏览 38
已采纳

在没有页面刷新的情况下将变量从JavaScript传递到PHP [重复]

This question already has an answer here:

I'm writing some code that has a variable in JavaScript that must be passed into the PHP script in the same document. The user input will be used to be scraped from some external site.

The JavaScript variable is HtmlLink, and it needs to be passed to the PHP code where it says INSERT HTMLLINK VARIABLE HERE without reloading the page.

<!DOCTYPE HTML>
<HEAD>
   <TITLE>Test Input</TITLE>
   <SCRIPT>
      type = "text/javascript"
      function testResults (form) {
         var TestVar = form.inputbox.value + ".html";
         var HtmlLink = "www.mp3skull.com/mp3/" + TestVar;
         document.write(HtmlLink);
      }
   </SCRIPT>
   <?php
      $contents = file_get_contents('INSERT HTMLLINK VARIABLE HERE');
      $dom = new DOMDocument();
      libxml_use_internal_errors(true);
      $dom->loadHTML($contents);
      libxml_clear_errors();
      $xpath = new DOMXpath($dom);

      $element = $xpath->query('//div[@id="right_song"]/div[3]/div[1]/div[1]/a')->item(0)-                    
      echo $element;  
   ?>  
</HEAD>
<BODY>
   <FORM NAME="myform" ACTION="" METHOD="GET"> Song Name <BR>
      <INPUT TYPE="text" NAME="inputbox" VALUE=""><P>
      <INPUT TYPE="button" NAME="button" Value="Search" onClick="testResults(this.form)">
   </FORM>
</BODY>
</HTML>
</div>
  • 写回答

2条回答 默认 最新

  • duan032225 2014-07-24 01:42
    关注

    If you want to do some searching, first of course build the proper URL first, then from there search/scrape the site, actually the base code is already working so its time to build on that. You can do something like this: Sample Demo

    $main_url = 'http://www.mp3skull.com/mp3/';
    $results = array();
    
    if(isset($_POST['submit'])) {
        // handle input (sample: hot mallets)
        $input = preg_replace('/[\s]+/', '_', strtolower(trim($_POST['input'])));
        $main_url .= $input . '.html'; // turns to hot_mallets.html
    
        $contents = @file_get_contents($main_url);
        if($contents !== false) { // simple error checking
    
            $dom = new DOMDocument();
            libxml_use_internal_errors(true);
            $dom->loadHTML($contents);
            libxml_clear_errors();
            $xpath = new DOMXpath($dom);
    
            $search_results = $xpath->query('//div[@id="song_html"]');
            if($search_results->length > 0) {
                foreach($search_results as $result) {
                    // each row result, put it inside the array
                    $results[] = $xpath->query('//div[@id="right_song"]/div[3]/div[1]/div[1]/a', $result)->item(0)->getAttribute('href');
                }
            } else {
                echo 'Zero results';
            }
    
    
        } else {
            echo 'Some error on getting results from external site.';
            exit;
        }
    }
    
    ?>
    
    <form method="POST">
        <label for="inputbox">Song Name: <input type="text" id="inputbox" name="input"/ ></label>
        <input type="submit" name="submit" />
    </form>
    <?php if(!empty($results)): ?>
    <h3>Search Results:</h3>
    <ul>
        <?php foreach($results as $result): ?>
            <li><?php echo $result; ?></li>
        <?php endforeach; ?>
    </ul>
    <?php endif; ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 对于这个问题的解释说明
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。