Haripriya
Haripriya

Reputation: 101

How to get the selected dropdown value on submit in Laravel?

In Laravel, I am trying to get the value of selected drop-down value and send it to another controller. And by using that drop-down value below values need to be changed by fetching the data from the database.

Below is the code I have tried.

<form action="{{{ url("/getDetailsBynumber/$_POST['number']") }}}" method="post">
<select name="number" id="number">
    <option selected="selected">Select number id</option>
    <?php     
    $numberArray = json_decode($number_id, true);       

    for ($i = 0 ; $i<=$number_count ; $i++) {
    ?>
    <option value="<?php echo $numberArray[$i]["number_value"] ?>"><?php echo $numberArray[$i]["number_value"]; ?></option>
    <?php
    }
    ?>
</select>
<input type="submit" value="Submit">

Upvotes: 2

Views: 10566

Answers (3)

user7773364
user7773364

Reputation:

This is my code to get state name after submit if validation fail incase then it works on load of page you can get the country id like this

var countryid = $('#opt_country').val();

after that base on that you have to find state_id

like this

if (countryid != '')
{

    $.ajax({
        url: base_path + 'getstate',
        type: "POST",
        data: {in_country_id: countryid},
        async: true,
        cache: false,
        success: function (statedata) {
            var obj = jQuery.parseJSON(statedata);
            console.log(obj);
            if (obj.SUCC_FLAG == 1)
            {
                for (var i in obj)
                {
                    console.log(obj);
                    var id = obj[i].in_state_id;
                    var name = obj[i].st_state_name;
                    if (id == stateid)
                    {
                        $("#opt_state").append("<option value='" + id + "' selected >" + name.toString() + "</option>");
                    } else {
                        $("#opt_state").append("<option value='" + id + "'>" + name.toString() + "</option>");
                    }
                }
            }
            else
            {
                confirm('We are unable to load State');
            }
        },
        error: function () {
            alert("server error");
        }

    });
}

and after that you can get the state so here is my example to get state after submit the page

Upvotes: 0

Orgil
Orgil

Reputation: 711

You need to add name attribute to select tag. As a result when form submitted it will send selected value with it's name. For example:

<select name="product_id">
   <option value="1">Cup</option> <!-- Suppose this option selected -->
   <option value="2">Pen</option>
   <option value="3">Book</option>
</select>

If you submit form, you can get selected value in your controller as follows:

public function methodName(Request $request)
{
  // $request->product_id is name attribute of your select tag
  print_r($request->product_id); // It will print out 1 which is value of Cup
}

Upvotes: 7

ShaneDaugherty
ShaneDaugherty

Reputation: 427

<form action="{{ url("/getDetailsBynumber/") . $_POST['number'] }}" method="post">
<select name="number" id="number">
    <option selected="selected">Select number id</option>
    <?php     
    $numberArray = json_decode($number_id, true);       

    for ($i = 0 ; $i<=$number_count ; $i++) {
    ?>
    <option value="<?php echo $numberArray[$i]["number_value"] ?>"><?php echo $numberArray[$i]["number_value"]; ?></option>
    <?php
    }
    ?>
</select>
<input type="submit" value="Submit">

Upvotes: 1

Related Questions