doujiao1180 2016-04-07 07:44
浏览 49
已采纳

如何将特定字段从PHP数组转换为Javascript中的数组? [关闭]

enter image description hereenter image description here

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 get it will be open empty.... For that I am write one function on search button this button get mobile number and call one method which in my shipment controller create method using ajax.and in create i get a mobile number and as per result check condition..

    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 i.e in object($modelArray)
                on same form i.e is like a update form i.e 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*/
        }
    }

Form of add Shipment(shipment/_form.php) And In shipment form create button this fname,laname,address all detail save into customer table with shipment 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条回答 默认 最新

  • douyun1852 2016-04-07 09:32
    关注

    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

    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 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 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);
           }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog