input type text values getting blank after form submission

Posted 8 months ago by nitinjohnson

I am working on a laravel application and it was working fine till yesterday but a new issue has come up now and I am totally confused how to solve this issue. I am submitting a form via ajax and and sending the serialized values to the controller but surprisingly the text field values becomes blank before submission. Please help me out.

View blade file

User Details

            {{--Fresh User--}}
            @if($edit_status=="new")
            <div class="row">
                <div class="col-md-4">
                    <label>Languages</label>
                    <select data-placeholder="Select a Language..." multiple class="languages">
                        <?php
                        foreach($languages as $lang){
                        ?>
                            <option value="{{ $lang->name }}">{{ $lang->name }}</option>
                        <?php } ?>
                    </select>
                </div>
                <div class="col-lg-4">
                    <label>Sex</label>
                    <select name="sex" class="form-control">
                        <option value="Male">Male</option>
                        <option value="Female">Female</option>
                    </select>
                </div>
                <div class="col-lg-4">
                    <label>Age</label>
                    <select name="age" class="form-control">
                        @for($i=18;$i<=120;$i++)
                            <option value="{{ $i }}">{{ $i }}</option>
                        @endfor
                    </select>
                </div>
            </div>
    
            <div class="row">
                <div class="col-md-12">
                    <label>Job</label>
                    <input type="text" name="job" class="form-control" />
                </div>
            </div>
    
            <div class="row">
                <div class="col-md-6">
                    <label>Hobbies</label>
                    <select data-placeholder="Select a Language..." multiple class="hobbies">
                        <?php
                        foreach($hobbies as $hobby){
                        ?>
                        <option>{{ $hobby->hobby_name }}</option>
    
                        <?php } ?>
                    </select>
                </div>
                <div class="col-lg-6">
                    <label>Smoking</label>
                    <select name="smoking" class="form-control">
                        <option value="Non-smoker">Non-smoker</option>
                        <option value="Somking sometimes">Somking sometimes</option>
                        <option value="Heavy smoker">Heavy smoker</option>
                    </select>
                </div>
            </div>
    
            <div class="row">
                <div class="col-md-4">
                    <label>Countries</label>
                    <select data-placeholder="Select a Country..." class="countries">
                        <option selected="selected" value=""/>
                        <?php
                        foreach($countries as $country){
                        ?>
                            <option value="{{ $country->id }}">{{ $country->name }}</option>
                        <?php } ?>
                    </select>
                </div>
                <div class="col-md-4">
                    <label>States</label>
                    <select data-placeholder="Select a State..." class="states">
    
                    </select>
                </div>
                <div class="col-md-4">
                    <label>Cities</label>
                    <select data-placeholder="Select a City..." class="cities">
    
                    </select>
                </div>
            </div>
    
            <div class="row">
                <div class="col-md-12">
                    <p>
                        <label>Facebook </label>
                        <input type="text" name="facebook" placeholder="Facebook" class="form-control" />
                    </p>
                    <p>
                        <label>Twitter </label>
                        <input type="text" name="twitter" placeholder="Twitter" class="form-control" />
                    </p>
                    <p>
                        <label>Instagram </label>
                        <input type="text" name="instagram" placeholder="Instagram" class="form-control"  />
                    </p>
                    <p>
                        <label>LinkedIn </label>
                        <input type="text" name="linkedin" placeholder="LinkedIn" class="form-control" />
                    </p>
                </div>
            </div>
    
            <div class="row">
                <div class="col-md-12">
                    <input type="button" class="btn pull-right" onclick="SaveUserDetailsForm(this)" value="Update"/>
                </div>
            </div>
            @else
    
            {{--Old User--}} 
            <div class="row">
                <div class="col-md-4">
                    <label>Languages</label>
                    <select data-placeholder="Select a Language..." multiple class="languages">
                        <?php                            
                            foreach($languages as $lang){                                
                        ?>
                                <option>{{ $lang->name }}</option>                       
                        <?php } ?>
                    </select>                    
                </div>
                <div class="col-lg-4">
                    <label>Sex</label>
                    <select name="sex" class="form-control">
                        <option value="Male" <?php if($user_details->sex=='Male'){?> selected <?php }?>>Male</option>
                        <option value="Female" <?php if($user_details->sex=='Female'){?> selected <?php }?>>Female</option>
                    </select>
                </div>
                <div class="col-lg-4">
                    <label>Age</label>
                    <select name="age" class="form-control">
                        @for($i=18;$i<=120;$i++)
                            <option value="{{ $i }}" <?php if($user_details->age==$i){?> selected <?php } ?>>{{ $i }}</option>
                        @endfor
                    </select>
                </div>
            </div>
    
            <div class="row">
                <div class="col-md-12">
                        <label>Job</label>
                        <input type="text" name="job" class="form-control" value="{{ $user_details->job }}" />
                </div>
            </div>
    
            <div class="row">
                <div class="col-md-6">
                    <label>Hobbies</label>
                    <select data-placeholder="Select a Language..." multiple class="hobbies">
                        <?php
    
                        foreach($hobbies as $hobby){                        
                        ?>
                                <option >{{ $hobby->hobby_name }}</option>                            
                        <?php } ?>
                    </select>
                </div>
                <div class="col-lg-6">
                    <label>Smoking</label>
                    <select name="smoking" class="form-control">
                        <option value="Non-smoker" <?php if($user_details->smoking=='Non-smoker'){?> selected <?php }?>>Non-smoker</option>
                        <option value="Somking sometimes" <?php if($user_details->smoking=='Somking sometimes'){?> selected <?php }?>>Somking sometimes</option>
                        <option value="Heavy smoker" <?php if($user_details->smoking=='Heavy smoker'){?> selected <?php }?>>Heavy smoker</option>
                    </select>
                </div>
            </div>
    
            <div class="row">
                <div class="col-md-4">
                    <label>Countries</label>
                    <select data-placeholder="Select a Country..." class="countries">
                        <option selected="selected" value=""/>
                        <?php
                        foreach($countries as $country){
                        ?>
                        <option value="{{ $country->id }}" <?php if($user_details->country==$country->id){?> selected <?php } ?>>{{ $country->name }}</option>
                        <?php } ?>
                    </select>
                </div>
                <div class="col-md-4">
                    <label>States</label>
                    <select data-placeholder="Select a State..." class="states">
                        <?php foreach($states as $state_key=>$state_value){?>
                            <option value="{{ $state_key }}" <?php if($state_key==$user_details->state){?> selected <?php }?>>{{ $state_value }}</option>
                        <?php } ?>
                    </select>
                </div>
                <div class="col-md-4">
                    <label>Cities</label>
                    <select data-placeholder="Select a City..." class="cities">
                        <?php foreach($cities as $city_key=>$city_name){?>
                            <option value="{{ $city_key }}" <?php if($city_key==$user_details->city){?> selected <?php }?>>{{ $city_name }}</option>
                            <?php } ?>
                    </select>
                </div>
            </div>
    
            <div class="row">
                <div class="col-md-12">
                    <p>
                        <label>Facebook </label>
                        <input type="text" name="facebook" placeholder="Facebook" class="form-control" value="{{ $facebook }}" />
                    </p>
                    <p>
                        <label>Twitter </label>
                        <input type="text" name="twitter" placeholder="Twitter" class="form-control" value="{{ $twitter }}" />
                    </p>
                    <p>
                        <label>Instagram </label>
                        <input type="text" name="instagram" placeholder="Instagram" class="form-control" value="{{ $instagram }}" />
                    </p>
                    <p>
                        <label>LinkedIn </label>
                        <input type="text" name="linkedin" placeholder="LinkedIn" class="form-control" value="{{ $linkedin }}" />
                    </p>
                </div>
            </div>
    
            <div class="row">
                <div class="col-md-12">
                    <input type="button" class="btn pull-right" onclick="SaveUserDetailsForm(this)" value="Update"/>
                </div>
            </div>
    
            @endif
    
            @if($edit_status=="new")
            <input type="hidden" id="selectedlanguages" name="languages" value="" />
            <input type="hidden" id="selectedhobbies" name="hobbies" value="" />
            <input type="hidden" id="selectedcountry" name="country" value="" />
            <input type="hidden" id="selectedstate" name="state" value="" />
            <input type="hidden" id="selectedcity" name="city" value="" />
            @else
            <input type="hidden" id="selectedlanguages" name="languages" value="{{ $known_languages }}" />
            <input type="hidden" id="selectedhobbies" name="hobbies" value="{{ $my_hobbies }}" />
            <input type="hidden" id="selectedcountry" name="country" value="{{ $country_id }}" />
            <input type="hidden" id="selectedstate" name="state" value="{{ $state_id }}" />
            <input type="hidden" id="selectedcity" name="city" value="{{ $city_id }}" />
            @endif
    
    
        </form>
    

    Javascript Code

    function SaveProfileUser(self){ var $self = $(self); var $form = $("#profile_user"); var data = $form.serialize();

                var delete_status=$("#deleteaccountoption").val();
                 DoServerHttpAsyncCall('/save-profile',function(response){
                     //closeProfilePagePopup();
                     var delete_status=$("#deleteaccountoption").val();
                     if(delete_status==="checked"){
                         redirectLocation('/deleteaccount');
                     } else if(delete_status==="notchecked") {
                        redirectLocation('/home');
                }
    
     },null,data,true);
    

    }

    Contorller Code

    public function EditUserDetails(){ $mytime = Carbon::now()->toDateTimeString(); $user_id=$this->getSessionData('CURRENT_USER_ID');

        $check_user_data_status=UserDetail::where('user_id',$user_id)->first();
        $countries=Countries::all();
        $languages=Language::all();
        $hobbies=Hobby::all();
        $data='';
    
        if(count($check_user_data_status)>0){
    
            $current_user_details=UserDetail::where('user_id',$user_id)->first();
            $known_languages=$current_user_details->languages;
            $my_hobbies=$current_user_details->hobbies;
    
            $states_array=State::where('country_id',$current_user_details->country)->get();
            $cities_array=Cities::where('state_id',$current_user_details->state)->get();
    
            $states=array();
            $cities=array();
    
            foreach($states_array as $sta){
                $states[$sta->id]=$sta->name;
            }
    
            foreach($cities_array as $cta){
                $cities[$cta->id]=$cta->name;
            }
    
            $social_media=unserialize($current_user_details->social_media);
            $facebook=$social_media[0];
            $twitter=$social_media[1];
            $instagram=$social_media[2];
            $linkedin=$social_media[3];
    
            $data=array(
                "countries"=>$countries,
                "states"=>$states,
                "cities"=>$cities,
                "languages"=>$languages,
                "hobbies"=>$hobbies,
                "user_details"=>$current_user_details,
                "known_languages"=>$known_languages,
                "my_hobbies"=>$my_hobbies,
                "facebook"=>$facebook,
                "twitter"=>$twitter,
                "instagram"=>$instagram,
                "linkedin"=>$linkedin,
                "edit_status"=>"old",
                "country_id"=>$current_user_details->country,
                "state_id"=>$current_user_details->state,
                "city_id"=>$current_user_details->city
            );
        } else {
            $data=array(
                "countries"=>$countries,
                "languages"=>$languages,
                "hobbies"=>$hobbies,
                "edit_status"=>"new"
            );
        }
    
        return view('travels.edit_user_details')->with($data);
    }
    

    Please sign in or create an account to participate in this conversation.

    Laracasts Mascot

    Hi, Have We Met Yet?

    Did you know that, in addition to the forum, Laracasts includes well over 1000 lessons on modern web development? All for the price of one lunch out per month.

    Sign Me Up

    Channels

    Reply to

    Use Markdown with GitHub-flavored code blocks.