将变量传递到url中 [英] passing variable into a url

查看:95
本文介绍了将变量传递到url中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,我已经在此问题上停留了几天...一直在寻找如何将变量传递到php代码的url以及每次提交表单时都更新url的答案.但是,我所能找到的只是如何通过url传递变量……我无法真正实现这一点,因为我觉得我需要一个不同的解决方案. 因此,这里是代码,除了将$ var传递到URL中并重新更新脚本以使用新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}');

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

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

所以: 假设有人要搜索"ziggs"或"jinx",他们将在表单中输入名称,表单将需要更新脚本以使用新的$ var运行解析器.

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.

非常感谢所有帮助!

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

?>

推荐答案

您的问题在此行上:

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

单引号不能处理字符串替换.您可以在此处阅读 PHP如何处理字符串的方式.因此,将设置该URL的完整值(包括文字{$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);

但是我更喜欢使用 sprintf ,它允许您将字符串用作这样的模板: /p>

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));

除此之外,您的代码还有些草率&不一致,所以这里是我的清理工作.看起来似乎没什么大不了的,但是混乱的代码简直很难阅读,这使得调试变得很困难.更清晰的代码使您可以更快地发现缺陷.另外,混合HTML和PHP有点令人困惑.在像这样的简单情况下,大部分代码都是错误的.而且在这种情况下,在通过PHP解析所有内容的同时犯了错误:

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

因此,在您的<form … >/</form>标记中,您将$var的值放入了'<input type="text" value="' . $var . '" name="var" />'中.但是,在呈现表单之后的$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>'
   ;

这篇关于将变量传递到url中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆