0

in the following code i have written a condition (if (epsflag==0)<?php $a=",hide:'true'";?> ).i want to implement the same condition using javascript.what i mean is that i want to condiionally use a javascript variable in my JSON.any help will be appreciated.if my question is not clear please let me know.

{display: 'Wave Name', name : 'wavename', sortable : true, align: 'left'<?php echo "$a"?>}

<script type="text/javascript">
function rowdata(epsflag,respflag){
if (epsflag==0) {
<?php $a=",hide:'true'";?>
}else
{
<?php $a=",hide:'false'";?> 
}                           
//alert(respflag);
$("#flex1").flexigrid(
{

url: myurl, 
dataType: 'json',
colModel : [
{display: 'Nr.', name : 'nr',  sortable : true, align: 'center',width:25},
{display: 'Datum', name : 'consultationdate',  sortable : true, align: 'left'},
{display: 'Wave Name', name : 'wavename',  sortable : true, align: 'left'<?php echo "$a"?>},
{display: 'Name', name : 'respname',  sortable : true, align: 'left'},
{display: 'E-mail', name : 'email',  sortable : true, align: 'left',width:180},
{display: 'Telefoon', name : 'telefoon',  sortable : true, align: 'left'},
{display: 'Medewerker', name : 'consultationwith',  sortable : true, align: 'left'}
                                    ],  

                            });
                        }

R R
  • 2,999
  • 2
  • 24
  • 42
  • Have an example (pseudo-syntax) of how to use this "javascript variable"? – user2864740 Oct 31 '13 at 04:31
  • i tried a lot of stuff but not working.if u can give me an example that will be good. – R R Oct 31 '13 at 04:33
  • I'm not sure exactly what is desired, hence why I asked for some "pseudo syntax" of the desired behavior. If you can show what is *meant* by "condiionally use a javascript variable", then perhaps I can provide some insight! (Or at least edit the title/question so that someone else can :) – user2864740 Oct 31 '13 at 04:42
  • its mentioned in my question in the beginning itself.have a look again. – R R Oct 31 '13 at 04:43
  • That looks like PHP. Show me what the desired hypothetical syntax in *JavaScript* looks like. – user2864740 Oct 31 '13 at 04:45
  • if (epsflag==0) { var a=",hide:'true'"; }else { var b=",hide:'false'"; } – R R Oct 31 '13 at 04:46
  • The answer is likely: ["use ?:"](http://stackoverflow.com/questions/6259982/js-how-to-use-the-ternary-operator), but be aware that, *from within* JavaScript Object Notation (which is a *superset* of JSON), there is no way to entirely conditionally *exclude* a key if `?:` is used. – user2864740 Oct 31 '13 at 04:46
  • can u please elaborate with an example code. – R R Oct 31 '13 at 04:47
  • I've added a link to the previous comment. Anyway: `x = someValue(); y = { foo: "foo", bar: x ? "x is not falsey" : undefined }` (note that this is *not* valid JSON, but is valid JavaScript and the key `bar` is *assigned in both cases*). To truly add a key conditionally, it can only be done *after* the object is created (from the JSON/JavaScript Literal Object syntax). – user2864740 Oct 31 '13 at 04:48
  • i guess we cant add the condition inside JSON.so wat i want is to declare the condition in javascript and than use that inside JSON.if it is working with PHP than it should work with javascript also. – R R Oct 31 '13 at 04:54
  • Nope! PHP *generates* output (JSON/JavaScript). However, JSON/JavaScript is limited to certain rules. These rules can be *bypassed* by PHP (as it runs ahead of time and just returns some text which is interpreted as HTML/JSON/whatever) but *cannot* be ignored by JSON or a JavaScript Object Literal. While one *could* generate JSON (which is just technically just text markup) in JavaScript and then pass it to `JSON.parse`, this is would require an intermediate string and parse step. (Remember that embedding "JSON" directly into JavaScript works because JSON is a *subset* of valid JavaScript). – user2864740 Oct 31 '13 at 06:20
  • 1
    possible duplicate of [Reference: Why does the PHP (or other server side) code in my Javascript not work?](http://stackoverflow.com/questions/13840429/reference-why-does-the-php-or-other-server-side-code-in-my-javascript-not-wor) – Quentin Oct 31 '13 at 07:06
  • @Quentin please read my question carefully,its not duplicate of wat u mentioned. – R R Oct 31 '13 at 07:24

2 Answers2

2

I think you're misunderstanding how the web works.

PHP is a string processing language. All it does is generate strings. Since the javascript is just a very large string to PHP we can replace it with another test string to get a clearer understanding of what's happening. Lets replace it with "bla bla bla..":

bla bla bla bla bla bla
<?php $a=",hide:'true'";?>
bla bla bla bla bla bla
<?php $a=",hide:'false'";?>
bla bla bla bla bla bla
<?php echo "$a"?>
bla bla bla bla bla bla

Let's now simplify that:

<?php
    $a=",hide:'true'";
    $a=",hide:'false'";
    echo "$a";
?>

This is what php executes. Therefore, it outputs the string:

bla bla bla bla bla bla    // note: you're not outputting anything here
bla bla bla bla bla bla
,hide:'false'
bla bla bla bla bla bla

Now replace the "bla bla" with a simplified version of your original string and the output becomes:

<script type="text/javascript">
function rowdata(epsflag,respflag){
    if (epsflag==0) {
                    // note: you echoed nothing here in PHP
    }
    else {
                    // note: you echoed nothing here in PHP
    }

    $("#flex1").flexigrid({
      colModel : [
        {align:'left',hide:'false'}  // logically, it's always false
      ]
    });
}

Now, if you've been following my explanation and have suddenly realized that PHP doesn't process javascript at all and the browser doesn't see PHP at all then you'll realize that this is a duplicate question of: What is the difference between client-side and server-side programming?

So. Now that we understand how the web works, what to do to solve your problem?

For this very specific problem I notice that you don't need to actually pass anything at all back to PHP. All your're trying to do is change a value in a javascript object. So why use PHP to manipulate the javascript source code when you can just use javascript to change the value directly? Doing it in js:

var wavedata = {
    display: 'Wave Name',
    name : 'wavename',
    sortable : true
};

if (epsflag==0) {
    wavedata.hide = 'true';
}
else {
    wavedata.hide = 'false';
}  


$("#flex1").flexigrid({
    colModel : [
        // other data
        wavedata,
        // other data
    ]
});

Or if you want to keep the source as close as possible to your current code then do what Grundy suggests, only since you want to set the value to the string "true" or "false" change it to:

{/* ... */ align: 'left',hide: epsflag==0 ? 'true' : 'false'}
Community
  • 1
  • 1
slebetman
  • 109,858
  • 19
  • 140
  • 171
  • -wow such a great explanation.but m still little confused.i think if i read this answer again than my confusion will be over.thanks a lot for the answer. – R R Oct 31 '13 at 08:42
1

maybe this help:

function rowdata(epsflag,respflag){

    $("#flex1").flexigrid(
    {

    url: myurl, 
    dataType: 'json',
    colModel : [
        {display: 'Nr.', name : 'nr',  sortable : true, align: 'center',width:25},
        {display: 'Datum', name : 'consultationdate',  sortable : true, align: 'left'},
        {display: 'Wave Name', name : 'wavename',  sortable : true, align: 'left',hide: (epsflag==0)},
...

UPDATE

If you want not use variable inside object you may try somthing like this:

function rowdata(epsflag,respflag){

    var colModel = {};
    if(epsflag == 0){
        colModel = [
            {display: 'Nr.', name : 'nr',  sortable : true, align: 'center',width:25},
            {display: 'Datum', name : 'consultationdate',  sortable : true, align: 'left'},
            {display: 'Wave Name', name : 'wavename',  sortable : true, align: 'left',hide: true }
            ...
    }else{
        colModel = [
            {display: 'Nr.', name : 'nr',  sortable : true, align: 'center',width:25},
            {display: 'Datum', name : 'consultationdate',  sortable : true, align: 'left'},
            {display: 'Wave Name', name : 'wavename',  sortable : true, align: 'left',hide: false }
            ...
    }

    $("#flex1").flexigrid(
    {

    url: myurl, 
    dataType: 'json',
    colModel : colModel,
    ...
Grundy
  • 13,356
  • 3
  • 35
  • 55
  • epsflag==0 will always return a boolean value of 0 or 1 not true or false. – R R Oct 31 '13 at 07:06
  • 1
    @RishabhRaj are you sure? in this [JavaScript Comparison and Logical Operators](http://www.w3schools.com/js/js_comparisons.asp) show that return boolean – Grundy Oct 31 '13 at 07:10
  • @RishabhRaj in Javascript or PHP? – Grundy Oct 31 '13 at 07:17
  • actually we have to use the javascript condition in JSON,thats y it is getting bit messy. – R R Oct 31 '13 at 07:19
  • @RishabhRaj in your sample you use not JSON, you use [Object literals](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Values,_variables,_and_literals#Object_literals) – Grundy Oct 31 '13 at 07:24
  • upvoting ur answer for ur nice logic :) – R R Oct 31 '13 at 08:44