1

I'm having trouble getting my code to do what I want.

  <select name="Item" id="Item" onChange="this.form.Amount.placeholder = <?php 
    $Selected = "this.form.Item.options[this.form.Item.selectedIndex].value";
    if (strpos($Selected, ':') !== FALSE){
        $ID = explode(':', $Selected);
        $_POST["Durability"] = $ID[1];
        echo $ID[0];
    }else{
        echo $Selected;
    }
    ?>">
      <option value="">
        Select an Item
      </option>
      <?php       
    $file = fopen("Items.txt", "r");
    while(!feof($file)){
        $line = fgets($file);
        echo '<option value='.$line.'>'.$line.'</option>';
 }
 fclose($file); 
 ?>
    </select>

... So what It should do, is when I select a Item from the menu, it changes the Amount form to match the value of the menu.

The items the menu prints out are things like:

 2 Grass
 3 Dirt
 3:1 Dirt (No Grass)
 3:2 Podzol
 4 Cobblestone
 5 Wooden Plank (Oak)
 5:1 Wooden Plank (Spruce)
 5:2 Wooden Plank (Birch)
 5:3 Wooden Plank (Jungle)
 5:4 Wooden Plank (Acacia)
 5:5 Wooden Plank (Dark Oak)

And as you can see it sets the option value as the line, but when it does the onChange it doesn't even notice the words after the numbers, and It doesn't want to split at ':'

To see what I'm doing, just visit: http://www.sniperzciinema.site50.net/Test/

Bimmr
  • 33
  • 6
  • 2
    Remember that all of the PHP code executes *before* the page is sent to the user's browser. Then only JavaScript code will run once it's on the client side until another request is sent back to the server. If you want to respond to selection dynamically you need to be doing all this logic in JavaScript. – mellamokb Apr 02 '14 at 18:01
  • `$Selected = "this.form.Item.options[this.form.Item.selectedIndex].value";` As mellamokb pointed out, this will not work! – Phidelux Apr 02 '14 at 18:02
  • Oh.. ok, so is there anyway I can make it $_POST[Durability] when onChange? – Bimmr Apr 02 '14 at 18:12
  • You have to use an AJAX call in Javascript to a PHP script, then handle the response in Javascript. Wikipedia has an example: http://en.wikipedia.org/wiki/Ajax_%28programming%29#Example – Dissident Rage Apr 02 '14 at 18:17
  • I'm just gonna cheat and use $_GET[] :P It seems to much easier lol – Bimmr Apr 02 '14 at 18:29

0 Answers0