Dita Aji Pratama
Dita Aji Pratama

Reputation: 720

Put URL on Select Form

On my expectation, I want to make an Add button on the bottom of option.

Here is my code:

<select class="form-control" name="id_sales" style="margin-bottom:15px;">

  <option value="" selected disabled>Sales</option>

  <?php
  $result = mysqli_query($con,"SELECT * FROM sales");
  while ($row = mysqli_fetch_array($result)) {
  ?>

  <option value="<?= $row['id_sales']; ?>"><?= $row['id_sales']; ?> - <?= $row['nama_sales']; ?></option>

  <?php } ?>

  <a href="index.php?page=sales" class="btn btn-primary">
    <span class="fa fa-plus"></span> Add new sales
  </a>

</select>

How to make it works?

Upvotes: 0

Views: 53

Answers (2)

DEarTh
DEarTh

Reputation: 1005

You can use Jquery fo this, As you can't put an anchor in option tag Here is the solution for question,

<select class="form-control" name="id_sales" id="id_sales" style="margin-bottom:15px;">
<option value="" selected disabled>Sales</option>
<option value="" >sales 1</option>
<option value="" >sales 2</option>
<option value="" >sales 3</option>
<option value="add_sales" id="add_sales">Add new sales</option>

<script type="text/javascript">
$(document).on('change','#id_sales', function (){
   var selected_value = $(this).val();
   if(selected_value=="add_sales"){
       window.location.replace("url where you want to go on clicking 'Add new sales'");
   }
});
</script>

Upvotes: 2

Parantap Parashar
Parantap Parashar

Reputation: 2010

In HTML, a possible element inside a select can only be a options element. You can not put anything inside the select tag except this.

This also makes sense as select and options define a single element. They represent a single entity.

So, first of all you need to declare the anchor outside the select tag to be able to see the link.

And, Using HTML a data can only be submitted using a form element. There is no substitute of it.

So, you need to wrap your fields in a form element too or you should use ajax. But Ajax also has its own use case so shouldn't be use if not necessary.

To make this work first wrap your element inside a HTML form and change the anchor to a submit type input/button. And, define the script url in form's action tag. And, if you must use anchor to submit the form do it using JavaScript.

Hope this is what you asked.

Upvotes: 0

Related Questions