【问题标题】:Proper way of rendering Json in html在html中呈现Json的正确方法
【发布时间】:2015-07-13 02:09:56
【问题描述】:

我有一个表单,在下拉选择标签中有很长的名称列表。我想实现 jquery 自动完成,但我当前的代码不起作用。

public function executeNew($r) {
    $this->getResponse()->setContentType('application/json');
    $cityId = Doctrine_Core::getTable('City')->getCity();
    return $this->renderText(json_encode($cityId));
    $this->form = new VotersForm();
    if($r->isMethod('post')) {
        $this->errors = array();
    $this->form->bind($r->getParameter('voters'));
        if($this->form->isValid()) {
            $values = $this->form->getValues();
            $firstname = $values['firstname'];
            $middlename = $values['middlename'];
            $lastname = $values['lastname'];
            $birthday = $values['birthday'];
            $email = $values['email'];
            $address = $values['address'];
           // $city = $values('city_id');
            $city = $cityId;
            $cellphone = $values['mobile_no'];
            $telephone = $values['telphone_no'];
            $profession = $values['profession_id'];
            $status = $values['civil_status'];
            $comments = $values['comments'];
            Doctrine_Core::getTable('Voters')->AddNewVoters($firstname,$middlename,$lastname,$birthday,$email,$address,$city,$cellphone,$telephone,$profession,$status, $comments,$date= date('Y-m-d, h:i:s'));
            $this->getUser()->setFlash("good", "You are added successfully");
            $this->redirect('duterte/showtotals');
        } else {
            $this->errors = 'Oops.Something is missing.Please check for required fields';
        }
    }
}

$city 字段与模型(城市)以一对多的关系相关。使用上面的代码,我可以从相关模型中“拉”json,但我的问题是,我的代码无法正确显示表单.相反,它会显示类似这样的内容

{"Candon":"Candon","Vigan":"Vigan","Alilem":"Alilem","Banayoyo":"Banayoyo","Bantay":"Bantay","Burgos":"Burgos","Cabugao":"Cabugao","Caoayan":"Caoayan","Cervantes":"Cervantes","Galimuyod":"Galimuyod","Gregorio }

如何使用自动完成下拉菜单正确显示表单?

  <?php if(isset($errors) && !empty($errors)):?>
    <div class="alert alert-warning"><?php echo $errors ?></div>
 <?php endif ?>
 <div class="page-header">
   <h3>Register here</h3>
 </div>
 <form method="post" class="form-horizontal">
 <div class="form-group">
    <div class="col-sm-10">
        <?php echo $form['firstname']->renderRow(array('class'=>'form-control','placeholder' => 'firstname')) ?>
    </div>
</div>
<div class="form-group">
    <div class="col-sm-10">
        <?php echo $form['middlename']->renderRow(array('class'=>'form-control','placeholder' => 'middlename')) ?>
    </div>
</div>
<div class="form-group">
    <div class="col-sm-10">
        <?php echo $form['lastname']->renderRow(array('class'=>'form-control','placeholder' => 'lastname')) ?>
    </div>
</div>
<div class="form-group">
    <div class="col-sm-2">
        <?php echo $form['birthday']->renderRow(array('class'=>'form-control','placeholder' => 'birthday')) ?>
    </div>
</div>
<div class="form-group">
    <div class="col-sm-10">
        <?php echo $form['address']->renderRow(array('class'=>'form-control','placeholder' => 'address')) ?>
    </div>
</div>
<div class="form-group">
    <div class="col-sm-10">
        <?php echo $form['city_id']->renderRow(array('class'=>'form-control required city_autocomplete')) ?>
    </div>
</div>
<div class="form-group">
    <div class="col-sm-10">
        <?php echo $form['profession_id']->renderRow(array('class'=>'form-control')) ?>
    </div>
</div>
<div class="form-group">
    <div class="col-sm-10">
        <?php echo $form['civil_status']->renderRow(array('class'=>'form-control')) ?>
    </div>
</div>
 <div class="form-group">
    <div class="col-sm-10">
        <?php echo $form['comments']->renderRow(array('class'=>'form-control','placeholder' => 'why Duterte')) ?>
    </div>
</div>
<div class="form-group">
    <div class="col-sm-offset-2 col-sm-10">
        <input type="submit" value="I Will Vote Duterte For President" class="btn btn-primary" />
        <input type="button" class="btn btn-danger" onclick="resetFunction()" value="Reset form" />
    </div>  
</div>

【问题讨论】:

    标签: php jquery html json symfony-1.4


    【解决方案1】:

    在尝试将renderRow 应用于表单元素之前,您需要在表单中设置下拉选择小部件。这是一个例子:

    $cityArray = array();
    foreach($cityId as $city) {
        //create an array from the City models
        $cityArray[$city->city_id] = $city->city_name; 
        //set the key to be the value in your dropdown select and the value to be the displayed label.
    }
    $form->setWidget('city_id', new sfWidgetFormChoice(array(
      'choices'   => $cityArray, //your list of cities, in an associative array
      'default'   => '0' //choose a default key/value
    )));
    

    来源:Symfony Documentation

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-23
      • 1970-01-01
      • 2021-08-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多