【问题标题】:passing variable into a url将变量传递到 url
【发布时间】:2014-01-22 17:24:22
【问题描述】:

好的,我在这个问题上被困了几天......一直在尝试找到有关如何将我的变量传递到 php 代码的 url 并在每次提交表单时更新 url 的答案。但是,我发现的只是如何通过 url 传递变量......因为我觉得我需要一个不同的解决方案,所以我真的无法实现这一点。 所以这是代码,除了将我的 $var 传递到 URL 并重新更新脚本以使用新 URL 运行之外,一切似乎都在工作。

例如,原件目前看起来是这样的:

    $html = file_get_html('http://www.championselect.net/champ/{$var}');

我希望有人在表单中输入一个条目,并让 $var 每次都更新。

所以: 假设有人想要搜索“ziggs”或“jinx”,他们会在表单中输入名称,并且表单需要更新脚本以使用新的 $var 运行解析器。

非常感谢所有帮助!

<form action="test.php" method="post">
<input type="text" value="<?php echo $var?>" name="var" />
<input type="submit" value="Send" />
</form>
<?php

$var = $_POST['var'];
echo $var;


// Include the library to use it.
include_once('simple_html_dom.php');

// Get div from site. ... Need to implement user selected queries.

$html = file_get_html('http://www.championselect.net/champ/{$var}');

// Put all of the <a> tags into an array named $result
$result = $html -> find('h2,img[class=CS_Champ_Image],.counterName' );

// Run through the array using a foreach loop and print each link out using echo
foreach($result as $element) {
  echo $element."   ";}   

?>

【问题讨论】:

    标签: php css parsing


    【解决方案1】:

    你的问题出在这一行:

    $html = file_get_html('http://www.championselect.net/champ/{$var}');
    

    单引号不处理字符串替换。你可以阅读PHP handles strings here。因此,该 URL 的完整值(包括文字 {$var})正在设置,而不是您所期望的。您可以通过几种不同的方式解决此问题。例如使用允许字符串替换的双引号:

    $html = file_get_html("http://www.championselect.net/champ/{$var}");
    

    或者只是像这样连接值:

    $html = file_get_html('http://www.championselect.net/champ/' . $var);
    

    但我更喜欢使用sprintf,它允许您像这样使用字符串作为模板:

    $html = file_get_html(sprintf('http://www.championselect.net/champ/%s', $var));
    

    除此之外,您的代码有点草率且不一致,所以这是我的清理工作。这似乎没什么大不了的,但是混乱的代码很难阅读,这使得调试变得很困难。更简洁的代码可以让您更快地发现缺陷。此外,混合 HTML 和 PHP 有点令人困惑。在像这样的简单情况下,大部分代码都是错误的。在这种情况下,如果只通过 PHP 解析它,那就错了:

    <?php
    
    // Echo the form.
    echo '<form action="test.php" method="post">'
       . '<input type="text" value="' . $var . '" name="var" />'
       . '<input type="submit" value="Send" />'
       . '</form>'
       ;
    
    // Set the $var variable.
    $var = $_POST['var'];
    
    // Include the library to use it.
    include_once('simple_html_dom.php');
    
    // Get div from site. ... Need to implement user selected queries.
    $html = file_get_html(sprintf('http://www.championselect.net/champ/%s', $var));
    
    // Put all of the <a> tags into an array named $result
    $result = $html -> find('h2,img[class=CS_Champ_Image],.counterName' );
    
    // Run through the array using a foreach loop and print each link out using echo
    foreach($result as $element) {
      echo $element . " ";
    }   
    
    ?>
    

    编辑我刚刚注意到别的东西。查看您开头的代码或我刚刚在上面所做的清理版本:

    // Echo the form.
    echo '<form action="test.php" method="post">'
       . '<input type="text" value="' . $var . '" name="var" />'
       . '<input type="submit" value="Send" />'
       . '</form>'
       ;
    
    // Set the $var variable.
    $var = $_POST['var'];
    

    因此,在您的&lt;form … &gt;/&lt;/form&gt; 标签中,您将$var 的值放入'&lt;input type="text" value="' . $var . '" name="var" /&gt;'。但是在呈现表单之后,$var 才没有赋值?所以不应该直接去掉就变成这样吗?

    // Echo the form.
    echo '<form action="test.php" method="post">'
       . '<input type="text" value="" name="var" />'
       . '<input type="submit" value="Send" />'
       . '</form>'
       ;
    

    【讨论】:

    • +1 很好的解释!从中学习了各种其他传递 URL 的方法。
    • 对作品背后逻辑的精彩解释!我衷心感谢可靠的​​帮助!
    • 谢谢! @tiptop 我刚刚注意到您的代码中有另一个错误。看看我上面的编辑。希望有帮助!
    【解决方案2】:

    你也可以这样做

    $html = file_get_html('http://www.championselect.net/champ?var=' . $var);
    

    【讨论】:

      【解决方案3】:

      试试……

      $html = file_get_html('http://www.championselect.net/champ/'.$var);
      

      单引号内不解析变量

      【讨论】:

      • 或者只是一个改变:$html = file_get_html("http://www.championselect.net/champ/{$var}");
      • 我发现将变量放在字符串中会降低代码的可读性。连接显示字符串和变量的清晰描述。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-09-14
      • 2012-11-06
      • 1970-01-01
      • 2014-03-16
      • 2021-06-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多