简体   繁体   中英

how can i get the value of the array using JQuery and PHP

<div class="row">
                    <div class="col-sm-4 ">
                                        <label>Date of Birth:</label>

                    </div>

                    <div class="row">

                    <div class="col-sm-4">
                        <?php
                            // set start and end year range
                            $dayArray = range(01, 31);
                            ?>
                            <!-- displaying the dropdown list -->
                            <select id="dob[]" class="form-control">
                                <option value="">Date</option>
                                <?php
                                foreach($dayArray as $day) {
                    $selected = (day == 01) ? 'selected' : '';
                                    echo '<option '.$selected.'value="'.$day.'">'.$day.'</option>';
                                }
                                ?>
                            </select>
                    </div>
                    <div class="col-sm-4">
                        <select id="dob[]" class="form-control">
                                <option selected="selected">Month</option>
                                <?php

                                $month = array("Jan", "Feb", "Mar", "Apr", "May",  "Jun","Jul","Aug","Sep","Oct","Nov","Dec" );             
                                foreach($month as $item){
                                ?>
                                <option value="<?php echo strtolower($item); ?>"><?php echo $item; ?></option>
                                <?php
                                }
                                ?>
                            </select>
                    </div>
                    <div class="col-sm-4">
                        <?php

                            $yearArray = range(1960, 2050);
                            ?>

                            <select id="dob[]" class="form-control">
                                <option value="">Year</option>
                                <?php
                                foreach ($yearArray as $year) {
                                    // if you want to select a particular year
                                    $selected = ($year == "") ? 'selected' : '';
                                    echo '<option '.$selected.' value="'.$year.'">'.$year.'</option>';
                                }
                                ?>
                            </select>
                        </div>
                        </div>
                    </div>
                    </div>

i have a requirement to get the value of the array using jquery , how can i get the value of the array dob[] using , $("#dob").val() ;

tried using json_encode, json_decode its not working. when i tried to alert the val it shows as undefined .

First of all, you have wrong HTML you have set id=dob[] it should be name=[]

 $(document).ready(function() { $("#getValues").click(function() { var month = $("#month").val(); var year = $("#year").val(); var dob = month + " " + year; alert(dob); }); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div class="col-sm-4"> <select name="dob['name']" class="form-control" id="month"> <option selected="selected" disabled>Month</option> <option value="1">Jan</option> <option value="2">Feb</option> <option value="3">Mar</option> </select> </div> <div class="col-sm-4"> <select name="dob['year']" class="form-control" id="year"> <option selected disabled>Year</option> <option value="2001">2001</option> <option value="2002">2002</option> <option value="2003">2003</option> </select> <button id="getValues">Get Values</button> </div> 

This is the basic code I have given! Hope you get the idea and proceed with your coding

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM