Skip to content Skip to sidebar Skip to footer

How To Convert Get Perticular Field From Php Array To An Array In Javascript?

After click on search button get the mobile number and hit database. if result not null then this fname,lname....all field open with fill form like update form but if result not g

Solution 1:

in your code there are a number of non-recommended practices. for example the fact to directly access the $ _POST instead of creating a suitable model and load the values with model-> load or the failure to use the rendering of views to make a ajax action for Update Your data .. in any case the discussion for every aspect would be too long so I give you the code available in the way that I think is closer to the way you write.

You action create could be somethings like this

publicfunctionactionCreate()
{
    if(isset($_POST['mobilenumber'])) {
        $modelArray = Customers::find()->where(['cust_mobile' => $_POST['mobilenumber']])->one();
        /*Here I got object in which whatever mobile number we enter 
        in text box. as per mobile number i got result and I check a condition is, if result count grater than 0 then 
        form open with fill text field else open with empty field  */if(count($modelArray) > 0) {
             $
            /*Here I wanna set data which is comming from response i.e in object($modelArray)
            on same form i.e is like a update form i.e here update logic i want to apply*/$modelArray->attribute1 = $_POST['your_attribute1'];
             ......
             $newModel->attributeN = $_POST['your_attributeN'];
             $newModel->save()  
             // redirect or render what you prefer return$this->redirect(['the_view_you_prefer']);              

        } else {
            /*Here new record is create if enter mobile number is not exist*/$newModel = new Customers();
             $newModel->mobilenumber =  $_POST['mobilenumber']
             $newModel->attribute1 = $_POST['your_attribute1'];
             ......
             $newModel->attributeN = $_POST['your_attributeN'];
             $newModel->save()
            return$this->redirect(['the_view_you_prefer']);
        }

    } else {
/*open form if mobile number not set*/
    }
}

Otherwise if you only want render the value for ajax you can

 myArray['your_attribute1'] = $_POST['your_attribute1'];
 ....
 myArray['your_attributeN'] = $_POST['your_attributeN'];

 $arrayToAjax= json_encode($myArray);
 echo$arrayToAjax; 

This should return the json formatted result for your ajax success function

for retrive the data in success function you can use jsonParse

  success: function(data){
         alert(data);//response display herevar jsonData = JSON.parse(data);
         alert(jsonData[0].cust_fname);
         alert(jsonData[0].cust_lname);

       $('customers-cust_lname').val(data);//how to set value fot that text field from response// window.location.reload(true);
       }

Post a Comment for "How To Convert Get Perticular Field From Php Array To An Array In Javascript?"