ma123456 ma123456 - 3 years ago 219
PHP Question

Save Drop Down or Text Input Value in Same Column(Using Laravel 5.5)

Scenerio:
If user select "other" option from the states Drop Down,text input will appear then user will enter the state manually....

My Issue is if user write state name in the text input,the value must go into the same column where the value of Drop Down going.In short,I have one column of state in my database and i want to save the state name given by user whether its coming from drop down or text input....

State Input and Drop Down.....

<select class="states" name="state" size="1" required>
@include('auth.partials._states')
</select>

<tr class="hidden state-input">
<td align="right">&nbsp;&nbsp;</td>
<td><input type="text" name="state" size="30" maxlength="100" value=""></td>
</tr>


Here is my JQuery for hiding and showing the text input

$('.states').change(function(){
var selected_option = $('.states').val();
if (selected_option === 'other') {
$('.state-input').removeClass('hidden');
}
else
{
$('.state-input').addClass('hidden');
}
});


Here is my create new user function where state is going with other fields

protected function create(array $data)
{
if(Input::hasFile('image'))
{
$email = Input::get('email');
$image = Input::file('image');
$image_extension = $image->guessClientExtension();
$image_path = $image->storePubliclyAs('public/uploads/profile_images/images', $email . ".{$image_extension}");
}


$user = new User([
'firstname' => $data['firstname'],
'lastname' => $data['lastname'],
'company' => $data['company'],
'classification' => $data['classification'],
'email' => $data['email'],
'password' => bcrypt($data['password']),
'image_url' => $image_path,
'skills' => $data['skills'],
'interests' => $data['interests'],
'about' => $data['about'],
'website' => $data['website'],
'address' => $data['address'],
'phone' => $data['phone'],
'country' => $data['country'],
'zipcode' => $data['zipcode'],
'city' => $data['city'],
'state' => $data['state'],
]);

$user->save();
return $user;
}

Answer Source
$('#submit_button').click(function(e) {
    e.preventDefault();
    var newValue = $('custom_state_text_box_selector').val(); // Modify me
    $('.states').val(newValue);
    $('your_form_selector').submit();
});

And also there is no need to set the name of the text field for enter the custom state. And also you give the same name as of the select dropdown. Remove it. Or You can also do it on the server side. Give your text box name "custom_state" and then

'state' => $data['state'] == "other" ? $data['custom_state'] : $data['state']
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download