2017-11-26 1 views
0

Ich versuche, ein einfaches Kontaktformular mit AngularJS und PHP zu erstellen, die an eine E-Mail und an Google - big query database gesendet werden, mit keinem Erfolg.Kontaktformular senden an Datenbank und E-Mail mit PHP, große Abfrage und angularjs

Und es scheint auch, wie gibt es keine Beispiele dafür überall ... mit Liebe jede Hilfe zu bekommen ...

Der Code i zeigt, setzen, was ich mit MySQL zu tun versuchen, ich brauche zu Stellen Sie es auf bigquery ein.

<body ng-app="myapp" ng-controller="formcontroller"> 
    <div class="contact-us-page"> 
     <div class="container"> 
      <div class="row"> 
       <div class="col-sm-4 col-xs-12"> 
        <h1>CONTACT US</h1> 
        <form name="userForm" novalidate ng-submit="insertData()"> 
         <label class="text-success" ng-show="successInsert">{{successInsert}}</label> 
         <div> 
          <label for="username" >Name</label> 
          <input type="text" name="userName" ng-model="insert.user_name" class="form-control" ng-class="{ 'has-error' : userForm.userName.$invalid && userForm.$submitted && userForm.userName.$pristine }" required> 
          <!--<span class="text-danger" ng-show="errorUsername">{{errorUsername}}</span>--> 
         </div> 
         <div> 
          <label for="companyName">Company name</label> 
          <input type="text" name="companyName" ng-model="insert.company_name" class="form-control" ng-class="{ 'has-error' : userForm.companyName.$invalid && userForm.$submitted && userForm.companyName.$pristine }" required> 
          <!--<span class="text-danger" ng-show="errorCompanyname">{{errorCompanyname}}</span>--> 
         </div> 
         <div> 
          <label for="email">Email</label> 
          <input type="email" name="email" ng-model="insert.email" class="form-control" ng-class="{ 'has-error' : userForm.email.$invalid && userForm.$submitted && userForm.email.$pristine }" required> 
          <!--<span class="text-danger" ng-show="errorEmail">{{errorEmail}}</span>--> 
         </div> 
         <div> 
          <label for="phone">Phone</label> 
          <input type="text" name="phone" ng-model="insert.phone" class="form-control" ng-class="{ 'has-error' : userForm.phone.$invalid && userForm.$submitted && userForm.phone.$pristine }" required> 
          <!--<span class="text-danger" ng-show="errorPhone">{{errorPhone}}</span>--> 
         </div> 
         <!--<div> 
          <input type="file" name="upload_cv" value="Upload CV" class="upload-cv"> 
         </div> --> 
         <input type="submit" name="insert" class="button-style" value="Send"> 
        </form> 
       </div> 
      </div> 
     </div> 
    </div> 
    <script src="app.js"></script>  
</body> 

app.js:

var application = angular.module("myapp", []); 
application.controller("formcontroller", function($scope, $http){ 
$scope.insert = {}; 
$scope.insertData = function(){ 
    $http({ 
    method:"POST", 
    url:"insert.php", 
    data:$scope.insert, 
    }).success(function(data){ 
    if(data.error) 
    { 
    $scope.errorUsername = data.error.user_name; 
    $scope.errorCompanyname = data.error.company_name; 
    $scope.errorEmail = data.error.email; 
    $scope.errorPhone = data.error.phone; 
    $scope.successInsert = null; 
    } 
    else 
    { 
    $scope.insert = null; 
    $scope.errorUsername = null; 
    $scope.errorCompanyname = null; 
    $scope.errorEmail = null; 
    $scope.errorPhone = null; 
    $scope.successInsert = data.message; 
    } 
    }); 
} 
}); 

insert.php:

<?php 
//insert.php 
$connect = mysqli_connect("localhost", "root", "", "resonai"); 
$form_data = json_decode(file_get_contents("php://input")); 
$data = array(); 
$error = array(); 

if(empty($form_data->user_name)) 
{ 
$error["user_name"] = "User Name is required"; 
} 

if(empty($form_data->company_name)) 
{ 
$error["company_name"] = "Company Name is required"; 
} 

if(empty($form_data->email)) 
{ 
$error["email"] = "Email Name is required"; 
} 

if(empty($form_data->phone)) 
{ 
$error["phone"] = "Phone Name is required"; 
} 

if(!empty($error)) 
{ 
$data["error"] = $error; 
} 
else 
{ 
$user_name = mysqli_real_escape_string($connect, $form_data->user_name); 
$company_name = mysqli_real_escape_string($connect, $form_data->company_name); 
$email = mysqli_real_escape_string($connect, $form_data->email); 
$phone = mysqli_real_escape_string($connect, $form_data->phone); 
$query = " 
    INSERT INTO contactus_page(user_name, company_name, email, phone) VALUES ('$user_name', '$company_name', '$email', '$phone') 
"; 
if(mysqli_query($connect, $query)) 
{ 
    $data["message"] = "Thank you. We will contact you soon!"; 
} 
} 

echo json_encode($data); 

?> 

insert.php mit bigquery- nicht funktioniert ...

<?php 
//insert.php 
composer require google/cloud-bigquery; 

require __DIR__ . '/vendor/autoload.php'; 

use Google\Cloud\BigQuery\BigQueryClient; 

$projectId = 'burnished-data-183409'; 

$form_data = json_decode(file_get_contents("php://input")); 
$data = array(); 
$error = array(); 

if(empty($form_data->user_name)) 
{ 
$error["user_name"] = "User Name is required"; 
} 

if(empty($form_data->company_name)) 
{ 
$error["company_name"] = "Company Name is required"; 
} 

if(empty($form_data->email)) 
{ 
$error["email"] = "Email Name is required"; 
} 

if(empty($form_data->phone)) 
{ 
$error["phone"] = "Phone Name is required"; 
} 

if(!empty($error)) 
{ 
$data["error"] = $error; 
} 
else 
{ 
# [START build_service] 
$bigquery = new BigQueryClient([ 
'projectId' => $projectId 
]); 
# [END build_service] 
# [START run_query] 
$user_name = $form_data->user_name; 
$company_name = $form_data->company_name; 
$email = $form_data->email; 
$phone = $form_data->phone; 

$query = 'insert into test1.cf (user_name, company_name, email, phone) values ('$user_name', '$company_name', '$email', '$phone')'; 
$options = ['useLegacySql' => true]; 
$queryResults = $bigQuery->runQuery($query, $options); 
# [END run_query] 

# [START print_results] 
if ($queryResults->isComplete()) { 
    $data["message"] = "Thank you. We will contact you soon!"; 
    /*$i = 0; 
    $rows = $queryResults->rows(); 
    foreach ($rows as $row) { 
     printf('--- Row %s ---' . PHP_EOL, ++$i); 
     foreach ($row as $column => $value) { 
      printf('%s: %s' . PHP_EOL, $column, $value); 
     } 
    } 
    printf('Found %s row(s)' . PHP_EOL, $i);*/ 
} else { 
    throw new Exception('The query failed to complete'); 
} 
# [END print_results] 
# [END all] 
} 

echo json_encode($data); 

?> 
+0

Was ist das genaue Problem, das Sie konfrontiert sind? – imox

+0

Ich habe die Datei "insert.php" nach der Google-großen Abfrage https://cloud.google.com/bigquery/create-simple-app-api geändert, aber es funktioniert nicht, –

Antwort

0

Ich nehme an, Sie versuchen, die Abfragen auszuführen, die gerade ex sind ausgeführt auf MySQL unter BigQuery. Für diesen Anwendungsfall sollten Sie die BigQuery PHP-Client-Bibliothek verwenden. Weitere Informationen über die Installation und Verwendung finden Sie hier https://cloud.google.com/bigquery/create-simple-app-api [Klicken Sie auf die Registerkarte PHP]

Verwandte Themen