0

I wanted that when the user will choose which brand the model choices will also show by brand. My question is how can I get the model name base on the chosen brand when It won't show because I needed the response from the first one.

<div class="col-sm">
<form class="needs-validation" method="post" novalidate>

    <div class="form-row">
    <div class="col-md-6 mb-10">
    <label for="validationCustom03">Select Brand</label>
    <select class="form-control custom-select" name="brandname" required>
    <option value="">Select Brand</option>
    <?php

    $ret=mysqli_query($con,"select brandName from tbl_brand");
    while($row=mysqli_fetch_array($ret))
    {?>

    <option value="<?php echo $row['brandName'];?>" ><?php echo $brandnameSelect = $row['brandName'];?></option>
    <?php } ?>
    </select>
    <div class="invalid-feedback">Please select Item.</div>
    </div>
    </div>
    <div class="form-row">
    <div class="col-md-6 mb-10">
    <label for="validationCustom03">Model</label>
    <select class="form-control custom-select" name="modelname" required>
    <option value="">Select Model</option>
    <?php

    $ret=mysqli_query($con,"select modelName from tbl_model WHERE brandID='$brandnameSelect'");
    while($row=mysqli_fetch_array($ret))
    {?>

    <option value="<?php echo $row['modelName'];?>" ><?php echo $row['modelName'];?></option>
    <?php } ?>
    </select>
    <div class="invalid-feedback">Please select Item.</div>
    </div>
    </div>
<div class="form-row">
<div class="col-md-6 mb-10">
<label for="validationCustom03">Item Name</label>
<input type="text" class="form-control" id="validationCustom03" placeholder="Brand Name" name="brandname" required>
<div class="invalid-feedback">Please provide a valid item name.</div>
</div>
</div>
<button class="btn btn-primary" type="submit" name="submit">Submit</button>
</form>
</div>
</div> ```
Newbeees
  • 21
  • 2
  • What have you tried so far? Where are you stuck? Also, be warned that the given `SELECT` query is widely open for SQL injection - better use prepared statements to avoid getting hacked – Nico Haase Jan 16 '22 at 11:00
  • So far I'm stuck. yes, I know that it is prone to SQL injection but I got to do it like this for project purposes but I will fix things in the future, for now, I need to figure things out. – Newbeees Jan 16 '22 at 15:11
  • I got progress where I used ajax following this question https://stackoverflow.com/questions/37497157/select-option-get-the-option-value-before-submit but it won't show me the output. just the no value. – Newbeees Jan 16 '22 at 16:05
  • ```".$row['modelName'].""; $flag++; } if($flag==0) { $data .=""; } echo $data; } ?>``` – Newbeees Jan 16 '22 at 16:05
  • **Warning:** You are wide open to [SQL Injections](https://php.net/manual/en/security.database.sql-injection.php) and should use parameterized **prepared statements** instead of manually building your queries. They are provided by [PDO](https://php.net/manual/pdo.prepared-statements.php) or by [MySQLi](https://php.net/manual/mysqli.quickstart.prepared-statements.php). Never trust any kind of input! Even when your queries are executed only by trusted users, [you are still in risk of corrupting your data](http://bobby-tables.com/). [Escaping is not enough!](https://stackoverflow.com/q/5741187) – Dharman Jan 16 '22 at 23:27

1 Answers1

0

you must use javascript and you have just 2 choices :

  1. fetch all models when you are loading the form page, then handle their corresponding select boxes options by js based on the selected model.
  2. use ajax on brands select boxes changes event, fetch related model and add them as options to models select box.

you cannot do anything more to achieve this.

behzad m salehi
  • 1,038
  • 9
  • 23