将PHP变量传递给HTML选择“值”

时间:2014-06-05 15:04:15

标签: php html unirest mashape

我试图通过html中的下拉列表传递一些PHP,然后需要使用php来帮助执行更多的PHP。但是我觉得它没有用。任何建议真的很值得赞赏,它一直困扰着我。这不是第一个在此页面上使用帖子的表单,在使用其他以前的表单执行搜索后填充下拉列表。

HTML:

<form method="post" name="results">
<select class="form-control textinput">
    <option value="<?php $response->body->results[0]?>"><?php echo $response->body->results[0]->name; ?></option>
    <option value="<?php $response->body->results[1]?>"><?php echo $response->body->results[1]->name; ?></option>
    <option value="<?php $response->body->results[2]?>"><?php echo $response->body->results[2]->name; ?></option>
    <option value="<?php $response->body->results[3]?>"><?php echo $response->body->results[3]->name; ?></option>
    <option value="<?php $response->body->results[4]?>"><?php echo $response->body->results[4]->name; ?></option>
    </select>

    <button type="submit" class="btn btn-primary" style="margin-left: auto; margin-right: auto; text-align: center;">Go</button>

</form>

PHP:

<?php

//Results from dropdown are put into selection  
$selection = $_POST["results"];

//Selection is put into result var, should look like $response->body->results[x]
$result = $selection;

   // IF Statement to only print result if the api call is successfull
   if ($response->code == 200) {

   if ($result->name == null) {
$printthis = "{$gametitle} returned no results, try and enter the full and accurate name";
}
   else {
$printthis = "{$result->name} has a score of {$result->score} on {$result->platform}";
   }

   } 

    ?>

1 个答案:

答案 0 :(得分:0)

首先,你错过了回声&#39;声明,您也不能将整个对象放入值中。如果

$response->body->results[0]

有一个id,就是你应该使用的

类似这样的事情

<?php echo $response->body->results[0]->id ?>

我会用循环来做这件事。

for($i = 0; $i < count($response->body); $i++) {
    echo '<option value="'.$i.'"><'.$response->body->results[$i]->name.'</option>';
}