如何将获取特定字段从 PHP 数组转换为 Javascript 中的数组? [英] How to convert get perticular field from PHP array to an array in Javascript?

查看:13
本文介绍了如何将获取特定字段从 PHP 数组转换为 Javascript 中的数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

点击搜索按钮后获取手机号码并点击数据库.如果结果不为空,则此 fname,lname.... 所有字段都以填写表单(如更新表单)打开,但如果结果未得到,它将打开为空....为此,我在搜索按钮上写了一个功能,此按钮获取手机号码并调用一种方法,该方法在我的发货控制器中使用 ajax 创建方法.在创建时,我获取手机号码并根据结果检查条件..

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

添加货件的形式(shipment/_form.php)并且在发货表单中创建按钮这个 fname, laname,address 所有详细信息保存到客户表中,并带有发货 id

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]) ?>

推荐答案

在您的代码中有许多不推荐的做法.例如,直接访问 $_POST 而不是创建合适的模型并使用模型加载值的事实->加载或未能使用视图呈现来为更新您的数据进行ajax操作..无论如何讨论因为每个方面都会太长,所以我以我认为更接近您编写方式的方式为您提供可用代码.

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

否则,如果您只想呈现 ajax 的值,您可以

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; 

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

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

要在成功函数中检索数据,您可以使用 jsonParse

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);
       }

这篇关于如何将获取特定字段从 PHP 数组转换为 Javascript 中的数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆