0

I am trying to pass some php via a dropdown in html , the php then needs to be used to aid the execution of more php. However I do not think it is working. Any suggestions would really be appreciated, its been bugging me a while. This is not the first form to use post on this page, the dropdown is populated after a search has been performed using another previous form.

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

   } 

    ?>
user3702402
  • 5
  • 1
  • 3
  • 1
    Judging by `$response->body->results[0]->name`, it seems safe to assume that `$response->body->results[0]` is an object and that therefore you can't just dump it into the `value` attribute - you might need to get a property of the object? – Niet the Dark Absol Jun 05 '14 at 15:06
  • 4
    You miss `echo` everywhere in `value=""`... – marekful Jun 05 '14 at 15:06
  • Pull all of your values into an array first, then in a single for loop, build each option set using each value of the array. Also, you have to actually echo or print your values to the screen – VikingBlooded Jun 05 '14 at 15:07

1 Answers1

0

First, you missed the 'echo' statement, and you also can't put an entire object into a value. If

$response->body->results[0]

has an id, thats what you should be using

something like this

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

I would do this with a loop.

for($i = 0; $i < count($response->body); $i++) {
    echo '<option value="'.$i.'"><'.$response->body->results[$i]->name.'</option>';
}
Brobin
  • 3,241
  • 2
  • 19
  • 35
  • Thanks!, My only problem now is this second post method makes my previous post which is used in later code, null values :( – user3702402 Jun 05 '14 at 15:31
  • You could use session variables. THat way they wouldn't get overwritten. [php session documentation](http://www.php.net/manual/en/ref.session.php) – Brobin Jun 05 '14 at 16:16
  • Here's another resource on sessions, with exmples. http://www.w3schools.com/php/php_sessions.asp – Brobin Jun 05 '14 at 16:18
  • Thanks! I have started to use a session now, it still isn't working properly though but thanks a lot for your help, I never even knew about sessions! (new to php). If you have any more suggestions I have posted a new question but your help so far has been great. http://stackoverflow.com/questions/24078431/php-2-step-form-post-issue-maybe-my-session-variables – user3702402 Jun 06 '14 at 12:56