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 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题