ma123456 ma123456 - 3 years ago 232
PHP Question

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

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>

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

Here is my JQuery for hiding and showing the text input

var selected_option = $('.states').val();
if (selected_option === 'other') {

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

protected function create(array $data)
$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'],

return $user;

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

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