如何将PHP数组中的特定字段转换为Javascript中的数组?

时间:2016-04-07 07:44:25

标签: javascript php yii2

enter image description here enter image description here

点击搜索按钮后获取手机号码并点击数据库。如果结果不为null然后这个fname,lname ....所有字段打开填充形式像更新表单但如果结果没有得到它将打开空.... 为此,我在搜索按钮上写了一个功能,这个按钮获取手机号码并调用一个方法,在我的货运控制器中使用ajax创建方法。在创建中我获得一个手机号码,并根据结果检查条件..

    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.php) 并在装运表单中创建按钮此fname,laname,将所有详细信息保存到带有装运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 个答案:

答案 0 :(得分:1)

在您的代码中有许多非推荐的做法。例如,直接访问$ _POST而不是创建合适的模型并使用model-&gt;加载值的事实;加载或无法使用视图呈现为更新您的数据进行ajax操作..在任何情况下,每个方面的讨论都会太长,所以我给你的代码是我认为更接近于你写的方式。

你的行动创造可能是像这样的事情

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*/
    }
}

否则,如果你只想渲染ajax的值,你可以

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

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

这应该返回ajax成功函数的json格式化结果

用于检索成功函数中的数据,可以使用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);
       }