0

I have two questions:

Q1) How can I pass array values into PHP and echo out same values in PHP? Q2) How to insert into values into database where I array key is the name of MySQL field and array value is the data for that particular field?

My console.log shows following output:

[]
firstname: "apple"
lastname: "banana"
length: 0

But I don't get these values displayed with PHP....

I have an HTML form which displays custom fields:

<form>

            <div ng-repeat="field in fields">

               <div ng-if="field.required == 'yes'">                

                  <label>
                    {{ field.fieldlabel }}
                  </label>


                <input type = "{{ field.fieldtype }}" ng-model="values[field.fieldname]">



               </div>        

            </div>  

             <div id ="entryform-button">
                <input type="submit" class="btn-primary btn" id="save" name="submit" value = "Save" ng-click="insertTrans()">
             </div>

        </form>

And Angular controller which pass data to PHP:

$scope.values = []; 

    $scope.insertTrans = function () {

        console.log($scope.values);

        //JSON.stringify($scope.values) 

        $http.post(
            "model/Form.php",
            { 'formvalues' : $scope.values }        
        ).then (function(data) {
            alert (data.data);
        });


    }

But in PHP I have tried json_decode, splash, and try to display with var_dump, echo, print_r but unable to see the data....

<?php      

$data = json_decode(file_get_contents("php://input"));  
//$data = json_decode($_POST['formvalues'], ture);

if(count($data) > 0)  {     

    var_dump($data);
    //print_r($data['formvalues']);
    //echo $data['formvalues']; 
    //echo implode(" ", $data);
    //print_r (explode(" ",$data));

//  foreach ($data['formvalues'] as $value) {
//      echo $value;
//  }

    //  $sql= "INSERT INTO trans (  ) VALUES ( )";
    //  $conn->exec($sql);
    //  echo "Data Inserted into Database";         

}

PHP returns:

array(1) {
  ["formvalues"]=>
  array(0) {
  }
}
5
  • You seem to be posting an empty array to your php. The point at which your have your console, the the values array is still empty and that's why your console message too has an empty array. The length is zero. This means you haven't yet passed data into your $scope.values array at the point of you posting that data Commented May 1, 2018 at 20:26
  • thank you @Clint_A, your response make alot more sense to me... can you suggest how to pass data to array......so length cannot be zero...thanks Commented May 1, 2018 at 21:56
  • Try and remove this line -> $scope.values = []; and see if it works. Commented May 2, 2018 at 11:47
  • Thank you @Clint_A , it worked when I pass integer values field.id....I have to now do a little work around into PHP like to call field id from one table where id is equal to field.id and then i insert into another table....thanks for you help Commented May 2, 2018 at 16:25
  • Cool, glad to help. I've posted it as an answer so you can accept it. Commented May 3, 2018 at 5:12

1 Answer 1

0

You seem to be posting an empty array to your php. The point at which your have your console, the the values array is still empty and that's why your console message too has an empty array. The length is zero. This means you haven't yet passed data into your $scope.values array at the point of you posting that data.

Try and remove this line >> $scope.values = []; and see if it works.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.