gpt4 book ai didi

php - 在 html 中渲染 Json 的正确方法

转载 作者:行者123 更新时间:2023-11-28 03:08:58 28 4
gpt4 key购买 nike

我有一个表单,在下拉选择标记中包含很长的名称列表。我想实现 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>

最佳答案

在尝试在表单元素上应用 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

关于php - 在 html 中渲染 Json 的正确方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31374537/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com