从这里开始是程序员,所以我很抱歉,如果这浪费您的时间。
我制作了一个如下所示的HTML表单:
<form action="post.php" method="post">
<label for="artist">Artist:</label><br>
<input title="" type="text" name="artist" id="artist" value="Muse" required><br>
<label for="album">Album:</label><br>
<input title="" type="text" name="album" id="album" value="Live At Rome Olympic Stadium" required><br>
<label for="genre">Genre:</label><br>
<input title="" type="text" name="genre" id="genre" value="Rock" required><br>
<label for="year">Year:</label><br>
<input title="" type="number" name="year" id="year" value="2013" required><br>
<label for="tracks">Tracks:</label><br>
<input title="" type="number" name="tracks" id="tracks" value="13" required><br><br>
<input title="" type="submit" name="submit" value="Submit">
</form>
提交的信息将发送到post.php,如下所示:
if ($_POST['artist'] ['album'] ['genre'] ['year'] ['tracks']) {
echo "The artist name is " . $_POST['artist'];
echo "<br>";
echo "The album name is " . $_POST['album'];
echo "<br>";
echo "The genre is " . $_POST['genre'];
echo "<br>";
echo "The year of release is " . $_POST['year'];
echo "<br>";
echo "The number of tracks is " . $_POST['tracks'];
echo "<br>";
} else {
echo "You have not filled out all the forms";
}
(我不确定是否使用isset,但这是另一回事)
根据我的编辑器PHPStorm,$ _ POST中的参数格式没有错误,但是我的浏览器给我以下错误:
警告:第3行的C:\ xampp \ htdocs \ php1 \ week2test \ week2music \ post.php中的字符串偏移量'album'
警告:第3行上C:\ xampp \ htdocs \ php1 \ week2test \ week2music \ post.php中的字符串偏移'genre'非法
警告:第3行的C:\ xampp \ htdocs \ php1 \ week2test \ week2music \ post.php中的字符串偏移量'year'
警告:第3行的C:\ xampp \ htdocs \ php1 \ week2test \ week2music \ post.php中的字符串偏移量'tracks'非法
我应该如何在$ _POST中设置参数格式以消除这些错误?或者,如果这不是问题所在,那么我不知道问题出在哪里,您有见识吗?
在这种情况下我应该使用isset吗?
谢谢。
答案 0 :(得分:3)
if ($_POST['artist'] ['album'] ['genre'] ['year'] ['tracks'])
在您当前的上下文中不存在,这将是一个5深度的数组。
您的意思是if ($_POST['artist'] && $_POST['album'] && $_POST['genre'] && $_POST['year'] && $_POST['tracks'])
或者,甚至更好的是,使用isset()
:if (isset($_POST['artist']) && isset($_POST['album']) && isset($_POST['genre']) && isset($_POST['year']) && isset($_POST['tracks']))
但是我更喜欢使用更性感的解决方案,将要检查的$_POST
的密钥存储在数组中,然后在该数组上循环:
$KeysToCheck = array('artist', 'album', 'genre', 'year', 'tracks');
$AllFieldsAreGood = true;
foreach($KeysToCheck as $KeyToCheck)
if (!isset($_POST[$KeyToCheck]))
$AllFieldsAreGood = false;
if (!$AllFieldsAreGood)
echo "You have not filled out all the forms";
else
{
// success ! Manage it.
}
请注意,仅在选中复选框时使用isset()
就是true
。
答案 1 :(得分:1)
如果要使用从POST接收到的非NULL值,最好使用isset
import scrapy
class MarketSpider(scrapy.Spider):
name = 'market'
allowed_domains = ['coinmarketcap.com']
start_urls = ['http://coinmarketcap.com/']
def parse(self, response):
Coin = response.xpath('//*[@class="currency-name-container link-secondary"]/@href').extract()
for link in Coin:
absolute_url = response.urljoin(link)
yield scrapy.Request(absolute_url,callback=self.website_link)
def website_link(self,response):
link = response.xpath('//*[@class="list-unstyled details-panel-item--links"]/li[2]/a/@href').extract()
name = response.xpath('normalize-space(//h1)').extract()
yield{'Name': name ,'Link': link}
答案 2 :(得分:0)
isset()
用于检查变量是否存在,并且不为空。
因此,始终首选使用它。就您而言,它可以像这样使用:
if (isset($_POST['artist']) && isset($_POST['album']) && isset($_POST['genre']) && isset($_POST['year']) && isset($_POST['tracks'])) {
echo "The artist name is " . $_POST['artist'];
echo "<br>";
echo "The album name is " . $_POST['album'];
echo "<br>";
echo "The genre is " . $_POST['genre'];
echo "<br>";
echo "The year of release is " . $_POST['year'];
echo "<br>";
echo "The number of tracks is " . $_POST['tracks'];
echo "<br>";
} else {
echo "You have not filled out all the forms";
}