0

I'm doing some project about the material science. I stuck on the php coding. My project plan is make a drop down list from the database whicih is connected and worked fine and when I select one metal from the drop down list, it shows the structure type of metal. This part is not working at all and give me some http 500 error when I wrote the code. I think it is something wrong with my code.

select class="form-control" name="metalname" id="metalname">
        <?php
        $sql = mysql_query("SELECT name FROM metal");
        while($row=mysql_fetch_array($sql)){
            echo"<option value=\"name\">" . $row['name']."</option>";
        }
        ?>
        </select>
      </div>
      <div class="form-group">
        <div class="input-group">
          <div class="input-group-addon" id="basic-addon1">Type</div>
           <?php
           $valStructure = $_POST['metalname'];
           $structure = mysql_fetch_array("SELECT structure_type FROM metal WHERE name='$valStructure");
           while($row1=mysql_fetch_array($structure)){
               echo" <input type="text" class="form-control" id="pricefrom" aria-describedby="basic-addon1" value=\"structure_type\">".$row1['structure_type'];
           }
           ?>

So drop down list part is works fine. But when I try to get the metal name from the select class, it gives me some error. Please help me !!

skdiwks
  • 1
  • 2

2 Answers2

0

If you look at this line:

echo" <input type="text" class="form-control" id="pricefrom" aria-describedby="basic-addon1" value=\"structure_type\">".$row1['structure_type'];

The quotes after type= are breaking the echo and causing a syntax error. Change it to one of the following:

1 - Escaped with a backslash

echo" <input type=\"text\" class=\"form-control\" id=\"pricefrom\" aria-describedby=\"basic-addon1\" value=\"structure_type\">".$row1['structure_type'];

2 - use single quotes

echo" <input type='text' class='form-control' id='pricefrom' aria-describedby='basic-addon1' value=\"structure_type\">".$row1['structure_type'];
Matt
  • 2,851
  • 1
  • 13
  • 27
0

I like to build my PHP objects all together, in a section at the top, and then inject them into the DOM as I build the HTML framework below:

<?php
    $selMN = '<select class="form-control" name="metalname" id="metalname">';
    $sql = mysql_query("SELECT name FROM metal");
    while($row=mysql_fetch_array($sql)){
        $selMN .= '<option value="name">' .$row['name']. '</option>';
    }
    $selMN .= '</select>';

    $pf = '';
    $valStructure = $_POST['metalname'];
    $structure = mysql_fetch_array("SELECT structure_type FROM metal WHERE name='$valStructure' ");
    while( $row1 = mysql_fetch_array($structure) ){
        $pf .= '<input type="text" class="form-control" id="pricefrom" aria-describedby="basic-addon1" value="structure_type">' .$row1['structure_type'];
    }

?>

<div class="missing_from_your_code">
    <?php echo $selMN; ?>
</div>

<div class="form-group">
    <div class="input-group">
        <div class="input-group-addon" id="basic-addon1">Type</div>
            <?php echo $pf; ?>
        </div>
    </div>
</div>

If you need to get additional information from the database based on interactions with the user, check out these useful SIMPLE examples at the bottom of this linked answer. (Don't just read them, copy them to your system and try them):

PHP - Secure member-only pages with a login system


Notes:

In addition to what Matt correctly identified in his answer, you also were missing the closing single-quote at the end of this line:

$structure = <<snip>> WHERE name='$valStructure");

(should have been):

$structure = <<snip>> WHERE name='$valStructure' ");
Community
  • 1
  • 1
cssyphus
  • 37,875
  • 18
  • 96
  • 111
  • Hi! Thank you for the help. I got the question about the $_POST. Do I need to add submit input for take value from the drop down list? or it is able to receive the data and can call it with $_POST when i just select from the dropbox list without any submission button for it ? – skdiwks Jan 15 '16 at 08:55
  • $_POST contains data being sent from ANOTHER page. There are two ways $_POST is used: FORMS (the old way) and AJAX (the new way). Read [this post](http://stackoverflow.com/questions/17973386/ajax-request-callback-using-jquery/17974843#17974843) about AJAX - STUDY IT! It is VERY useful - and easy! Because of AJAX, there is no reason to use forms any more. – cssyphus Jan 15 '16 at 15:24