Luiggi
Luiggi

Reputation: 368

ng-select just return the word 'array"

i have a ng-select in a contact form, I recieve correctly the form except ng-select.

The system just return me the word "array", but I need the selected option from the ng-select.

This is my portion of html

<div ng-controller="ContactController" class="panel-body">
        <form ng-submit="submit(contactform)" name="contactform" method="post" action="" class="form-horizontal" role="form">
            <div class="row half" ng-class="{ 'has-error': contactform.inputName.$invalid && submitted }">
                <div class="12u">
                    <input ng-model="formData.inputName" type="text" class="form-control" id="inputName" name="inputName" placeholder="Nombre" required>
                </div>
            </div>
            <div class="row half" ng-class="{ 'has-error': contactform.inputEmail.$invalid && submitted }">
                <div class="12u">
                    <input ng-model="formData.inputEmail" type="email" class="form-control" id="inputEmail" name="inputEmail" placeholder="Email de contacto" required>
                </div>
            </div>
            <!-- select -->
            <div class="row half" ng-class="{ 'has-error': contactform.myDepartment.$invalid && submitted }">
                <div class="12u">
                    <select ng-model="formData.myDepartment" ng-options="departamento.name for departamento in departamento" id="myDepartment" name="myDepartment">
                            <option value="">-- Tipo de mensaje --</option>
                    </select>
                </div>
            </div>
            <!-- select -->
            <div class="row half" ng-class="{ 'has-error': contactform.inputSubject.$invalid && submitted }">
                <div class="12u">
                    <input ng-model="formData.inputSubject" type="text" class="form-control" id="inputSubject" name="inputSubject" placeholder="Asunto del mensaje" required>
                </div>
            </div>
            <div class="row half" ng-class="{ 'has-error': contactform.inputMessage.$invalid && submitted }">
                <div class="12u">
                    <textarea ng-model="formData.inputMessage" class="form-control" rows="4" id="inputMessage" name="inputMessage" placeholder="Mensaje" required></textarea>
                </div>
            </div>
            <div class="row">
                <div class="12u">
                    <input type="submit" name="submit" id="submit_btn" value="Enviar mensaje" ng-disabled="submitButtonDisabled"/>
                </div>
            </div>
        </form>
        <p ng-class="result" style="padding: 15px; margin: 0;">{{ resultMessage }}</p>
</div>

This is the code of controller.js

app.controller('ContactController', function ($scope, $http) {
    $scope.result = 'hidden'
    $scope.resultMessage;
    $scope.formData; //formData is an object holding the name, email, subject, and message
    $scope.submitButtonDisabled = false;
    $scope.submitted = false; //used so that form errors are shown only after the form has been submitted
    $scope.departamento = [
        {name:'Selecciona departamento'},
        {name:'RRHH'},
        {name:'Soporte'},
        {name:'Postventa'},
        {name:'Comercial'},
        {name:'Desarrollo'}
      ];

    $scope.submit = function(contactform) {
        $scope.submitted = true;
        $scope.submitButtonDisabled = true;
        if (contactform.$valid) {
            $http({
                method  : 'POST',
                url     : '/contact-form.php',
                data    : $.param($scope.formData),  //param method from jQuery
                headers : { 'Content-Type': 'application/x-www-form-urlencoded' }  //set the headers so angular passing info as form data (not request payload)
            }).success(function(data){
                console.log(data);
                if (data.success) { //success comes from the return json object
                    $scope.submitButtonDisabled = true;
                    $scope.resultMessage = data.message;
                    $scope.result='bg-success';
                    $scope.myDepartment = $scope.departamento.name; // red

                } else {
                    $scope.submitButtonDisabled = false;
                    $scope.resultMessage = data.message;
                    $scope.result='bg-danger';
                }
            });
        } else {
            $scope.submitButtonDisabled = false;
            $scope.resultMessage = 'Failed :( Please fill out all the fields.';
            $scope.result='bg-danger';
        }
    }
});

And this is the Php

<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
require_once 'phpmailer/PHPMailerAutoload.php';

if (isset($_POST['inputName']) && isset($_POST['inputEmail']) && isset($_POST['myDepartment']) && isset($_POST['inputSubject']) && isset($_POST['inputMessage'])) {

    //check if any of the inputs are empty
    if (empty($_POST['inputName']) || empty($_POST['inputEmail']) || empty($_POST['myDepartment']) || empty($_POST['inputSubject']) || empty($_POST['inputMessage'])) {
        $data = array('success' => false, 'message' => 'Completa el formulario antes de enviar');
        echo json_encode($data);
        exit;
    }

    //create an instance of PHPMailer
    $mail = new PHPMailer();

    $mail->From = $_POST['inputEmail'];
    $mail->FromName = $_POST['inputName'];
    $mail->AddAddress('[email protected]'); //recipient 
    $mail->Subject = $_POST['inputSubject'];
    $mail->Body = "Name: " . $_POST['inputName'] . "\r\n\r\nDepartment: " . $_POST['myDepartment'] . "\r\n\r\nMessage: " . stripslashes($_POST['inputMessage']);

    if (isset($_POST['ref'])) {
        $mail->Body .= "\r\n\r\nRef: " . $_POST['ref'];
    }

    if(!$mail->send()) {
        $data = array('success' => false, 'message' => 'El mensaje no puede ser enviado. Error: ' . $mail->ErrorInfo);
        echo json_encode($data);
        exit;
    }

    $data = array('success' => true, 'message' => 'Gracias, hemos recibido tu mensaje.');
    echo json_encode($data);

} else {

    $data = array('success' => false, 'message' => 'Rellena el formulario completamente.');
    echo json_encode($data);

}

Can you help me?

Thanks, Luiggi

Upvotes: 0

Views: 116

Answers (2)

azium
azium

Reputation: 20614

Firstly, your ng-options syntax is wrong. It should be

ng-options="singular.title for singular in plural"

If you had an array of objects like this:

$scope.departamento = [{name: dep1}, {name: dep2}]

You would write ng-options like this:

ng-options="d.name for d in departamento"

Where d is any variable name.

Also I don't think you should be doing this

 data : $.param($scope.formData)

Because $scope.formData is already a javascript object, and $http, unlike $.ajax automatically stringify's your objects.

Upvotes: 0

Mike
Mike

Reputation: 1246

You are having the entire object be set for $scope.myDepartment. Instead of

ng-options="departamento.name for departamento in departamento" 

try using

ng-options="departamento.name as departamento.name for departamento in departamento" 

this should set $scope.myDepartment to the actual string selected, rather than an object of {name: selection}

Upvotes: 1

Related Questions