How to convert get perticular field from PHP array to array in Javascript?

enter image description here enter image description here

After clicking the search button, enter the mobile phone number and click on the database. if result not null, then this is the name fname, lname .... the whole field opens with a filling form, like an update form, but if the result does not work, it will be open empty .... For this, I write one function on the search button, this button gets the mobile number and calls one method, which in my delivery controller creates a method using ajax.and in the creation I get the mobile number and in accordance with the condition for checking the result.

public function actionCreate() { if(isset($_POST['mobilenumber'])) { //$modelArray = Customers::find()->where(['cust_mobile' => $_POST['mobilenumber']])->one(); $connection = \Yii::$app->getDb(); $command = $connection->createCommand( "SELECT * ". "FROM customers AS C ". "WHERE C.cust_mobile= " .$_POST['mobilenumber'] // "AND C.cust_mobile=" .$_POST['mobilenumber2'] ); $modelArray = $command->queryAll(); if(count($modelArray) > 0) { print_r(json_encode($modelArray)); /*Here I wanna set data which is comming from response ie in object($modelArray) on same form ie is like a update form ie here update logic i want to apply*/ } else { /*Here new record is create if enter mobile number is not exist*/ } } else { /*open form if mobile number not set*/ } } 

Add form Shipping (shipping / _form.php) And in the sending form, create this button, file_name, laname, save the address information in the client table with the delivery ID

  <script type="text/javascript"> function searchResult() { var mobilenumber = document.getElementById('customers-cust_mobile').value; $.ajax({ type: "POST", data: { mobilenumber: mobilenumber, }, dataType: "json", url: "index.php?r=shipment/create", success: function(data){ $('#customers-cust_fname').val(data[0].cust_fname); $('#customers-cust_mname').val(data[0].cust_mname); $('#customers-cust_lname').val(data[0].cust_lname); $('#customers-cust_email').val(data[0].cust_email); $('#customers-cust_street').val(data[0].cust_street); $('#customers-cust_address').val(data[0].cust_address); } }); } </script> <?= $form->field($modelCustomersSender, 'cust_mobile')->textInput(['maxlength' => true]) ?> <?= $form->field($modelCustomersSender, 'cust_fname')->textInput(['maxlength' => true]) ?> <?= $form->field($modelCustomersSender, 'cust_mname')->textInput(['maxlength' => true]) ?> <?= $form->field($modelCustomersSender, 'cust_lname')->textInput(['maxlength' => true]) ?> 
+1
javascript php yii2
source share
1 answer

There are a number of non-recommended practices in your code. for example, the fact that direct access to $ _POST instead of creating a suitable model and loading values ​​using the model-> load or refusing to use view rendering to perform an ajax action to update your data .. in any case, a discussion because every aspect will too long, so I give you the code, available as I think, closer to how you write.

You can create something like this

 public function actionCreate() { 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 ie in object($modelArray) on same form ie is like a update form ie 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 want to render only the ajax value, you can

  myArray['your_attribute1'] = $_POST['your_attribute1']; .... myArray['your_attributeN'] = $_POST['your_attributeN']; $arrayToAjax= json_encode($myArray); echo $arrayToAjax; 

This should return a json formatting result for your ajax success function

you can use jsonParse to search for data in the success function

  success: function(data){ alert(data);//response display here var 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); } 
+1
source share

All Articles