Php 在yii2中使用ajax填充或填充所有字段
当我输入Email ID并点击按钮时,我想在yii2中使用ajax填充所有字段,而不是用户名,所有细节都应该从mysql数据库中获取,并使用jquery填充字段。我的代码如下 signup.phpPhp 在yii2中使用ajax填充或填充所有字段,php,jquery,mysql,ajax,yii2,Php,Jquery,Mysql,Ajax,Yii2,当我输入Email ID并点击按钮时,我想在yii2中使用ajax填充所有字段,而不是用户名,所有细节都应该从mysql数据库中获取,并使用jquery填充字段。我的代码如下 signup.php <?php $form = ActiveForm::begin([ 'options' => ['name' => 'membershipForm', '
<?php
$form = ActiveForm::begin([
'options' => ['name' => 'membershipForm', 'autocomplete' => "off", 'id' => "formajax"],
'action' => ['']
]);
?>
<div class="row">
<div class="col-sm-6">
<?= $form->field($model, 'email', ['options' => ['name' => 'Email']]) ?>
<span id="email-info" class="info"></span>
</div>
<div class="col-sm-6">
<?= $form->field($model, 'username')->textInput() ?>
<span id="username-info" class="info"></span>
</div>
</div>
<!-- Auto Popupulate -->
<div class="row">
<div class="col-sm-6">
<div class="form-group">
<?= Html::submitButton('Check If Already a Member', ['class' => 'btn btn-warning', 'name' => 'populate-button', 'id' => 'PopupulateData', 'type' => 'submit']) ?>
<!-- <input type="submit" value="Check If Already a Member" /> -->
</div>
</div>
</div>
<!-- End -->
<div class="row">
<div class="col-sm-6">
<?= $form->field($model, 'password')->passwordInput() ?>
<span id="password-info" class="info"></span>
</div>
<div class="col-sm-6">
<?= $form->field($model, 'first_name')->textInput(['maxlength' => true]) ?>
<span id="first_name-info" class="info"></span>
</div>
</div>
<div class="row">
<div class="col-sm-6">
<?= $form->field($model, 'last_name')->textInput(['maxlength' => true]) ?>
<span id="last_name-info" class="info"></span>
</div>
<div class="col-sm-6">
<?=
$form->field($model, 'dob')->label('Date of Birth')->widget(
DatePicker::className(), [
'inline' => false,
'clientOptions' => [
'autoclose' => true,
'format' => 'yyyy-mm-dd'
]
]);
?>
</div>
</div>
<div class="row">
<div class="col-sm-6">
<?= $form->field($model, 'gender')->dropDownList(Yii::$app->commonUtility->getGenderDropdown(), ['prompt' => '(Select Gender)', 'options']) ?>
</div>
<div class="col-sm-6">
<?= $form->field($model, 'country')->dropDownList(Yii::$app->commonUtility->getCountryDropdown(), ['prompt' => '(Select Country)', 'options' => ['India' => ['selected' => true]]]) ?>
<span id="country-info" class="info"></span>
</div>
</div>
<!--Added by Exabit-->
<div class="row">
<div class="col-sm-12">
<?= $form->field($model, 'address')->textarea(['rows' => 6]) ?>
</div>
</div>
<!--Ended-->
<div class="row">
<div class="col-sm-6">
<?= $form->field($model, 'city')->textInput(['maxlength' => true]) ?>
<span id="city-info" class="info"></span>
</div>
<div class="col-sm-6">
<?= $form->field($model, 'state')->textInput(['maxlength' => true]) ?>
<span id="state-info" class="info"></span>
</div>
</div>
<div class="row">
<div class="col-sm-6">
<?= $form->field($model, 'zip')->textInput(['maxlength' => true]) ?>
<span id="zip-info" class="info"></span>
</div>
<div class="col-sm-6">
<?= $form->field($model, 'phone')->textInput(['maxlength' => true]) ?>
</div>
</div>
<div class="row">
<div class="col-sm-6">
<?= $form->field($model, 'mobile')->textInput(['maxlength' => true]) ?>
<span id="mobile-info" class="info"></span>
</div>
<div class="col-sm-6">
<?= $form->field($model, 'organization')->textInput(['maxlength' => true]) ?>
</div>
</div>
<input type="checkbox" name="terms_condition" value="terms&condition" checked=""> Yes, I agree with <a href="<?= Url::toRoute('/site/'); ?>" target="_blank">Terms & Conditions</a>
<span id="terms_condition-info" class="info"></span>
<!-- <div clas="row">
<div class="col-sm-6">
<div class="form-group">
<button id="rzp-button1" class="btn btn-warning">Pay</button>
</div>
</div>
</div> -->
<div class="row">
<div class="col-sm-6">
<div class="form-group">
<?= Html::submitButton('Pay', ['class' => 'btn btn-warning', 'name' => 'signup-button', 'id' => 'MemberShipbutton']) ?>
</div>
</div>
</div>
这是在我的SiteController.php中
public function actionFormajax1(){
// return $this->render('formajax');
$sql ="SELECT email,first_name,last_name FROM member";
$formajax1 = Member::findBySql($sql)
-all();
Yii::$app->response->format = Response::FORMAT_JSON;
if (Yii::$app->request->isAjax) {
$data = Yii::$app->request->post();
// $searchname= explode(":", $data['searchname']);
// $searchby= explode(":", $data['searchby']);
$email = $data['email'];
$first_name= $data['first_name'];
$first_name= $data['first_name'];
$responsedata = // your logic;
\Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
return [
'responsedata' => $responsedata,
'formajax1' => $formajax1,
];
}
}
请让我知道怎么做,因为这对我来说是新的。。多谢各位
$("#PopupulateData").on("click", function(event){
// var form = $('form[id="formajax"]');
event.preventDefault();
var email = $("#email").val();
var first_name = $("#first_name").val();
var last_name = $("#last_name").val();
// console.log("Success");
// var form = document.getElementById("form-ajax");
// form.attr('action', '$ajax_info');
// form.$("#you_button_id").on("submit", form_submit, false);
//var valNew = bounced.split("|");
// $('#first_name').value;
// $('#last_name').value;
//$('#email').value;
$.ajax({
type: "POST", url: "formajax1",
dataType: 'json',
data: {
email : email,
first_name : first_name,
last_name : last_name,
},
async: true,
success: function (response) {
console.log(response);
if(response != ''){
var json_obj = jQuery.parseJSON(JSON.stringify(response));
console.log(json_obj);
console.log("Success");
}
}
})
});
现场控制员是
public function actionFormajax1(){
$formajax1 = Member::find()
->all();
if (Yii::$app->request->isAjax) {
$return = [];
$data = Yii::$app->request->post();
// print_r($data);exit;
// $username = $data['username'];
$email = $data['email'];
// $password = $data['password'];
$first_name = $data['first_name'];
$last_name = $data['last_name'];
// $dob = $data['dob'];
// $gender = $data['gender'];
// $country = $data['country'];
// $city = $data['city'];
// $state = $data['state'];
// $zip = $data['zip'];
// $phone = $data['phone'];
// $mobile = $data['mobile'];
// $organization = $data['organization'];
// $address = $data['address'];
// $return['username'] = $username;
$return['email'] = $email;
// $return['password'] = $password;
$return['first_name'] = $first_name;
$return['last_name'] = $last_name;
// $return['dob'] = $dob;
// $return['gender'] = $gender;
// $return['country'] = $country;
// $return['city'] = $city;
// $return['state'] = $state;
// $return['zip'] = $zip;
// $return['phone'] = $phone;
// $return['mobile'] = $mobile;
// $return['organization'] = $organization;
// $return['address'] = $address;
// $session = Yii::$app->session;
echo json_encode($return);
}
}
现在的问题是我无法从电子邮件Id获取输入
您不需要手动完成这一切,您只需使用Pjax,它会有所帮助
您可以自动执行类似的操作(更新表单字段而无需
重新加载
您可以通过以下链接找到Pjax的文档:
NoteController.php:
public function actionCreate()
{
$model = new Note();
if ($model->load(Yii::$app->request->post()) && $model->save())
{
return $this->render('edit', [
'model' => $model
]);
}
return $this->render('create', [
'model' => $model,
]);
}
create.php
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
/* @var $this yii\web\View */
/* @var $model app\modules\notes\models\Note*/
/* @var $form yii\widgets\ActiveForm */
?>
<div class="note-form">
<?php yii\widgets\Pjax::begin(['id' => 'new_note']) ?>
<?php
$form = ActiveForm::begin([
'method' => 'post',
'action' => ['note/create'],
'options' => ['data-pjax' => true]
]);
?>
<?= $form->field($model, 'note')->textarea(['rows' => 6]) ?>
<div class="form-group">
<?= Html::submitButton('Create', ['class' => 'btn btn-success']) ?>
</div>
<?php ActiveForm::end(); ?>
<?php Pjax::end(); ?>
</div>
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
/* @var $this yii\web\View */
/* @var $model app\modules\notes\models\Note*/
/* @var $form yii\widgets\ActiveForm */
?>
<div class="note-form">
<?php yii\widgets\Pjax::begin(['id' => 'new_note']) ?>
<?php
$form = ActiveForm::begin([
'method' => 'post',
'action' => ['note/create'],
'options' => ['data-pjax' => true]
]);
?>
<?=
$form
->field($model, 'id')
->hiddenInput()
->label(false);
?>
<?= $form->field($model, 'note')->textarea(['rows' => 6]) ?>
<div class="form-group">
<?= Html::submitButton('Update', ['class' => 'btn btn-success']) ?>
</div>
<?php ActiveForm::end(); ?>
<?php Pjax::end(); ?>
</div>
edit.php
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
/* @var $this yii\web\View */
/* @var $model app\modules\notes\models\Note*/
/* @var $form yii\widgets\ActiveForm */
?>
<div class="note-form">
<?php yii\widgets\Pjax::begin(['id' => 'new_note']) ?>
<?php
$form = ActiveForm::begin([
'method' => 'post',
'action' => ['note/create'],
'options' => ['data-pjax' => true]
]);
?>
<?= $form->field($model, 'note')->textarea(['rows' => 6]) ?>
<div class="form-group">
<?= Html::submitButton('Create', ['class' => 'btn btn-success']) ?>
</div>
<?php ActiveForm::end(); ?>
<?php Pjax::end(); ?>
</div>
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
/* @var $this yii\web\View */
/* @var $model app\modules\notes\models\Note*/
/* @var $form yii\widgets\ActiveForm */
?>
<div class="note-form">
<?php yii\widgets\Pjax::begin(['id' => 'new_note']) ?>
<?php
$form = ActiveForm::begin([
'method' => 'post',
'action' => ['note/create'],
'options' => ['data-pjax' => true]
]);
?>
<?=
$form
->field($model, 'id')
->hiddenInput()
->label(false);
?>
<?= $form->field($model, 'note')->textarea(['rows' => 6]) ?>
<div class="form-group">
<?= Html::submitButton('Update', ['class' => 'btn btn-success']) ?>
</div>
<?php ActiveForm::end(); ?>
<?php Pjax::end(); ?>
</div>
在您的情况下,也可以执行类似此示例的操作。您的错误在哪里?你可以在互联网上找到很多教程,帮助你如何在yii2上进行ajax调用。这些代码不起作用。因为它没有显示任何错误。如果你知道解决办法,那就把我打回去。thanksI需要知道您的代码在哪里中断,例如在控制器中发布一个var_dump($data)。在js文件中,如果ajax成功,则必须执行以下操作:url try:,success:function(response){alert(response);}Uncaught SyntaxError:JSON中位于位置1的意外标记o我认为您试图解析JSON数据两次。