在 html 中渲染 Json 的正确方法

Proper way of rendering Json in html

我有一个表单,在下拉列表中有很长的姓名列表 select tag.I 想实现 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 字段与模型(City) 一对多相关 relationship.Using 上面的代码,我可以 'pull' json 来自相关模型,但是我的问题是,我的代码不显示形式properly.Instead,它会显示这样的东西

{"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>

您需要先在表单中设置下拉 select 小部件,然后再尝试在表单元素上应用 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