douliwang6896 2014-01-05 06:19
浏览 114
已采纳

将变量传递到url中

Okay, I have been stuck for a few days on this...Have been trying to find an answer on how to pass my variable into the url of the php code and update the url each time the form is submitted. However, all I van find is how to pass the variable through a url...Not really been able to implement this as I feel I need a different solution. So here is the code, everything does seem to be working except for the passing of my $var into the URL and reupdating the script to run with the new URL.

For example the original currently looks like this:

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

I want someone to enter a entry into the form and for the $var to update each time.

So: Say someone wants to search for "ziggs" or "jinx", they would enter the name into the form and the form will need to update the script to run the parser with the new $var.

Much appreciated to all help!!

<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."   ";}   

?>
  • 写回答

3条回答 默认 最新

  • dtcrw26206 2014-01-05 06:33
    关注

    Your problem is on this line:

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

    Single quotes don’t handle string substitution. You can read up on how PHP handles strings here. So the full value of that URL—including that literal {$var}—are being set instead of what you expect. You can tackle this issue a few different ways. Such as using double-quotes which allow string substitution:

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

    Or just concatenating the values like this:

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

    But I prefer to use sprintf which allows you to use strings as templates like this:

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

    Beyond that, your code is a bit sloppy & inconsistent, so here is my cleanup. It might not seem like a big deal, but chaotic code is simply hard to read which makes it hard to debug. Cleaner code allows you to spot flaws quicker. Also, mixing HTML & PHP is a bit confusing. In simple cases like this err on the side of what the bulk of the code is. And in this case, err on the side of just parsing it all through 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 . " ";
    }   
    
    ?>
    

    EDIT I just noticed something else. Look at the code right a the beginning of yours or the cleaned up version I just did above:

    // 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'];
    

    So in your <form … >/</form> tags you are placing the value of $var into '<input type="text" value="' . $var . '" name="var" />'. But that $var is not assigned a value until the after the form is rendered? So shouldn’t that just be removed to be like this?

    // Echo the form.
    echo '<form action="test.php" method="post">'
       . '<input type="text" value="" name="var" />'
       . '<input type="submit" value="Send" />'
       . '</form>'
       ;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序