1

I have this form in the view:

<form method="POST" action="<?php echo Yii::$app->request->baseUrl;?>/telephone/addnow/" role="form" enctype="multipart/form-data">
    <label>Upload your photo:</label><input type="file" name="image" ><br>
    <input type="name" name="name" id="name" placeholder="Name" required><br><br>
    <input type="text" name="address" placeholder="Address"><br><br>
    <input type="text" name="telephone" placeholder="Telephone number" required>
    <br><br>
    <div id="dynamicInput">
        <br><input type="text" name="myinputs[]" placeholder="Secondary Phone #1">
        <span class="glyphicon glyphicon-plus" onClick="addInput('dynamicInput');"></span>
        <br><br>  
    </div>
    <input type="text" name="mobile" placeholder="Mobile number" >  <br><br>
    <input type="email" name="email" placeholder="Email">
    <input type="email" name="altemail" placeholder="Alternative Email"><br><BR>
    <input type="text" name="company_name" placeholder="Company Name"><br><BR>
    <input type="text" name="company_address" placeholder="Company Address"><br><br>
    <input type="text" name="company_phone_primary" placeholder="Company Telephone">    
    <input type="text" name="company_phone_secondary" placeholder="Telephone Secondary "><br><br>
    <input type="text" name="company_email" placeholder="Company Email Address"><br><BR>
    <button type="submit" class="btn btn-default">Submit</button><BR><BR>
</form>

allowing the user to insert any number of secondary phones i used this javascript code:

<script>
    var counter = 1;
    var limit = 10;
    function addInput(divName) {
        if (counter == limit)  {
            alert("You have reached the limit of adding " + counter + " inputs");
        } else {
            var newdiv = document.createElement('div');
            newdiv.innerHTML = "Seconday Phone # " + (counter + 1) + " <br><input type='text' name='myinputs[]' placeholder='Secondary Phone '>";
            document.getElementById(divName).appendChild(newdiv);
            counter++;
        }
    }
</script>

And I have this controller code:

public function actionAddnow()
{
    $request = Yii::$app->request;
    $add=new telephone();
    $add->Name=$request->post('name');
    $add->Email=$request->post('email');
    $add->Mobile=$request->post('mobile');  
    $add->Address=$request->post('address');
    $add->Telephone=$request->post('telephone');
    $add->altemail=$request->post('altemail');
    $add->company_name=$request->post('company_name');
    $add->company_address=$request->post('company_address');
    $add->company_phone_primary=$request->post('company_phone_primary');
    $add->company_phone_secondary=$request->post('company_phone_secondary');
    $add->company_email=$request->post('company_email');

    $add->save();

    $getlast=Yii::$app->db->getLastInsertId();
    $myinputs=$request->post('myinputs');
    $totalinputs=sizeof('$myinputs');   

    for ($i=0; $i<=$totalinputs; $i++) {
        $inputs=$myinputs[$i];
        $phones=new phone();
        $phones->secondary_phones=$inputs;
        $phones->id=$getlast;
        $phones->save();    
    }
    return $this->redirect(Yii::$app->request->baseUrl.'/telephone/index');     
}

but only the first two values of $myinputs are inserted in the database.

2
  • have you some validation rules on Phone model? please show this model Commented Jun 16, 2016 at 11:23
  • are you sure this is correct way? $totalinputs=sizeof('$myinputs'); for getting $toalinputs? Commented Jun 16, 2016 at 14:14

1 Answer 1

1

Putting variable $myinputs between single quotation marks ('$myinputs') you will convert your variable to string. sizeof is alias for count function, but as you give argument as string you allways get result as 1. After you will loop with condition ($i=0; $i<=$totalinputs; $i++) meaning that cycle will run 2 times when $i is 0 and when $i is 1.

Instead of:

$totalinputs=sizeof('$myinputs');

You should use

$totalinputs=sizeof($myinputs);

Another error is in your cycle condition.

for ($i=0; $i<=$totalinputs; $i++)

Should be

for ($i=0; $i<$totalinputs; $i++)

Or you could replace for cycle with foreach

foreach($myinputs as $inputs)
{
        $phones=new phone();
        $phones->secondary_phones=$inputs;
        $phones->id=$getlast;
        $phones->save();
}

On Yii side of improvements

1.For form you could use ActiveForm widget

$form = \yii\widgets\ActiveForm::begin([
        'options' => [
           "role" => "form",
           "enctype"=> "multipart/form-data",
        ], ]); 

echo $form->field($add, 'Name'); 
//etc 
$form->end();

2.If you would use ActiveField for creating input fields in view or adding manually field names in format like ModelClassName[ModelFieldName], then you would be able to use Model load for assaign'ing values Example:

$add=new telephone();
if ($add->load(Yii::$app->request->post()))
{
   if ($add->save())
   {//saved
   }
   else
   {//error
   }
}
else
{//no post data
}

3.For urls its probably not needed to add request baseUrl property. Example in redirect you can simply use

$this->redirect('/telephone/index');

4.Using method getLastInsertId() may lead to errors when there are some triggers in database that will create additional rows. So it may be wiser to use:

$getlast=$add->id;//assuming model autoincrement field name is "id"
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.