user2281489
user2281489

Reputation: 1

Java Script is not working inside the php

public function actionAjaxUpdate()
    {   
        $action = $_GET['act'];


        if(!isset($_POST['selectedId']))
        {
            echo '<script> alert("No row is selected"); </script>';
        }

        else
        {
        $selectedAll = $_POST['selectedId'];
        if(count($selectedAll)>0)
        {
            foreach($selectedAll as $selectedId)
            {
                $model=$this->loadCompositeModel($selectedId);
                switch ($action) {
                    case 'Delete':
                        $model['account']->delete();
                        break;
                    case 'Active':
                        $model['account']->active = '1';
                        break;
                    case 'Inactive':
                        $model['account']->active = '0';                     
                        break;
                    default:
                        break;
                }
                $model['account']->save();
            }
        }
        }

    }

In this code Alert is not working, if no id is selected.So some one help me out. i have tried alot but js is not working. I have used js inside the php and this is the first time when js is not working inside the php

Upvotes: 0

Views: 76

Answers (3)

Amir
Amir

Reputation: 4111

It seems $_POST['selectedId'] is an array. then you should use isset for elements of $_POST['selectedId'] for example if (!isset($_POST['selectedId'][0]))

EDIT 1: try this one

$array_id = array_filter($_POST['selectedId']);

then use if (!empty($array_id)) instead of if (!isset($_POST['selectedId']))

Upvotes: 0

Basic
Basic

Reputation: 26766

Unless you're injecting the response back into the page, the Javascript will never be evaluated.

If you were to do...

document.write('<script src="/path/to/phpscript.php">')

then the response will be appended to the page and evaluated.

However, if you do...

$.ajax({
    success: function(response) {
        //Blah....  
    }
});

or something to that effect, the response is never evaluated, it's passed to your success function as a string. So you could just get the PHP to print 1 or 0 for success/failure - or even better return a Json object

<?php

    ...
    if(!isset($_POST['selectedId']))
    {
        $Ret->Success = false;
        $Ret->Reason = "No row is selected";
        header("Content-Type: application/json");
        print json_encode($Ret)
    }     
?>

Then use the $.getJson Ajax call to retrieve the response as a Js object (See this manual page)

Upvotes: 0

Quentin
Quentin

Reputation: 944441

Without seeing the code that makes the HTTP request, it is hard to say what the problem is.

Them most likely explanation that selectedId is set to an empty string when no id has been selected. The condition !isset($_POST['selectedId']) will then not give you the result you want. Use empty() instead (and make sure that 0 is not an acceptable id value).

Upvotes: 2

Related Questions