Yii2 : Sending data to same page not working properly

Tushar Walzade

I'm using Yii2-advanced-app. I want to send the value of my selected dropdown to a php code(which shows list with checkboxes) on same page. Please see the following image for reference (That's how I want actually)-

Please see this image for reference

But, after selecting some element from dropdown list, I'm getting the result, but with whole html page in that specific portion. Here is the Snap for reference -

Here is the Snap for reference

I don't understand why I'm getting the whole page in response.

I've wrote the following code for that -

Dropdown List

<?= $form->field($countries[0], 'id')->dropDownList(
    ArrayHelper::map($countries, 'phonecode', 'name'),
    [
    'prompt' => 'Select Country',
    'onchange' => '
    var id;
    jQuery.ajax({
        type: "POST",
        data: {
            "id": $(this).val(), 
        },
        // url: "",
        success: function(data){
            jQuery(".tobechanged").html(data);
            alert(data);
        }
    });'
    ])->label(false);
    ?>

And my respective div is

<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12 tobechanged" style="max-height: 400px; background: white; overflow: auto;">
    <ul class="list-unstyled" id="chkall">
        <?php
        if(isset($contacts)) {
            $contacts = $contacts;
                            // print_r($contacts[0]['cust_country_code']);exit();
            foreach ($contacts as $contact) {
                if(isset($_POST['id'])) {
                    if($contact['cust_country_code'] == $_POST['id']) {
                        echo '<li>
                        <div class="checkbox" id="checkboxes">
                            <label><input type="checkbox" value="'.$contact['cust_id'].'" id="'.$contact['cust_id'].'">'.$contact['cust_fname'].' '.$contact['cust_lname'].'</label>
                        </div>
                    </li>';
                }
            } else {
                echo '<li>
                <div class="checkbox" id="checkboxes">
                    <label><input type="checkbox" value="'.$contact['cust_id'].'" id="'.$contact['cust_id'].'">'.$contact['cust_fname'].' '.$contact['cust_lname'].'</label>
                </div>
            </li>';
        }
    }
}
?>
</ul>
</div>

It doesn't work even when we display 'Hello' instead of checkboxes code. i.e like this-

<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12 tobechanged" style="max-height: 400px; background: white; overflow: auto;">
    Hello
</div>

So, please help me to avoid unwanted html.

I'm getting the Response like this

Response

Tushar Walzade

I've made it in the following way -

Dropdown -

<?= $form->field($countries[0], 'phonecode')->dropDownList(
    ArrayHelper::map($countries, 'phonecode', 'name'),
    [
    'prompt' => 'Select Country',
    'onchange' => '
    dataType : "json",
    $.post("index.php?r=messages/contactist&id="+$(this).val(), function(response) {
        jQuery(".tobechanged").html(response);
    });'
    ])->label(false);
    ?>

The div 'tobechanged' remain same. It's also okay to make it empty, like this -

<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12 tobechanged">***Nothing Here***</div>

Then, I've shown my checklist in 'contactist' method as follows -

public function actionContactist($id)
{
    $model = new Messages();
    $query = new \yii\db\Query;
    $query->select(['cust_id','cust_fname','cust_lname','cust_mobile'])->from('customers')->where(['cust_country_code' => $id])->orderBy('cust_fname');
    $query->createCommand();

    $contactList = new ActiveDataProvider([
    'query' => $query,
    'pagination' => false,
    ]);
    $contacts = $contactList->getModels();
    // print_r(json_encode($contacts));
    echo '<ul class="list-unstyled" id="chkall">';
    foreach ($contacts as $contact) {
    echo '<li>
    <div class="checkbox" id="checkboxes">
        <label><input type="checkbox" class="case" name="case" value="'.$contact['cust_id'].'" id="'.$contact['cust_id'].'">'.$contact['cust_fname'].' '.$contact['cust_lname'].'</label>
        <input type="hidden" value="'.$contact['cust_mobile'].'" id="'.$contact['cust_id'].'">
    </div>
</li>';
}
echo '</ul>';
}

That's it.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

getimagesize() not working properly in Yii2

From Dev

nstimer not working properly showing the same data and page controller also not indicate page

From Dev

Anchors within the Same page are not working properly

From Dev

Django: ajax POST sending data of array of objects not working properly

From Dev

Pass data to next page in yii2

From Dev

issue in sending form data to controller in yii2

From Dev

Yii2: data-method='post' is sending GET request

From Dev

Sending query to the same page

From Dev

Ajax not working properly when I tried to call it on same page again

From Dev

Sending context with request, and {% if %} not working properly

From Dev

Ajax call not working when sending data to another page

From Dev

MY php form not working ie not sending data to my page

From Dev

SideNav opening the page in the same window (Yii2)

From Dev

Yii2 - form and related gridview on the same page

From Dev

Loading page not working properly

From Dev

Sending data to detailViewController not working?

From Dev

HTTP request not sending data properly

From Dev

Yii2 Gridview didn't show data in Index page

From Dev

Yii2 kartik-v select2 widget allowClear and placeholder not working properly

From Dev

Sending SMS in yii2

From Dev

Yii2 and OAuth2 Plugin Filsh/yii2-oauth2-server: Unauthorized when sending POST data

From Dev

sending request parameters to the same page and same servlet

From Dev

Data array not working properly?

From Dev

Yii2 always log application category with $_COOKIE, $_SESSION and $_SERVER (category filter not working properly)

From Dev

Show loading page not working properly

From Dev

Android Login Page Not Working Properly

From Dev

2 columns with left design is not working properly in add to cart page?

From Dev

HTML data attributes on the same page are not working for countdown script

From Dev

jQuery Ajax data to same PHP page not working as INTENDED?

Related Related

  1. 1

    getimagesize() not working properly in Yii2

  2. 2

    nstimer not working properly showing the same data and page controller also not indicate page

  3. 3

    Anchors within the Same page are not working properly

  4. 4

    Django: ajax POST sending data of array of objects not working properly

  5. 5

    Pass data to next page in yii2

  6. 6

    issue in sending form data to controller in yii2

  7. 7

    Yii2: data-method='post' is sending GET request

  8. 8

    Sending query to the same page

  9. 9

    Ajax not working properly when I tried to call it on same page again

  10. 10

    Sending context with request, and {% if %} not working properly

  11. 11

    Ajax call not working when sending data to another page

  12. 12

    MY php form not working ie not sending data to my page

  13. 13

    SideNav opening the page in the same window (Yii2)

  14. 14

    Yii2 - form and related gridview on the same page

  15. 15

    Loading page not working properly

  16. 16

    Sending data to detailViewController not working?

  17. 17

    HTTP request not sending data properly

  18. 18

    Yii2 Gridview didn't show data in Index page

  19. 19

    Yii2 kartik-v select2 widget allowClear and placeholder not working properly

  20. 20

    Sending SMS in yii2

  21. 21

    Yii2 and OAuth2 Plugin Filsh/yii2-oauth2-server: Unauthorized when sending POST data

  22. 22

    sending request parameters to the same page and same servlet

  23. 23

    Data array not working properly?

  24. 24

    Yii2 always log application category with $_COOKIE, $_SESSION and $_SERVER (category filter not working properly)

  25. 25

    Show loading page not working properly

  26. 26

    Android Login Page Not Working Properly

  27. 27

    2 columns with left design is not working properly in add to cart page?

  28. 28

    HTML data attributes on the same page are not working for countdown script

  29. 29

    jQuery Ajax data to same PHP page not working as INTENDED?

HotTag

Archive