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'}
                                    ],  

                            });
                        }

13
  • Have an example (pseudo-syntax) of how to use this "javascript variable"? Commented Oct 31, 2013 at 4:31
  • i tried a lot of stuff but not working.if u can give me an example that will be good. Commented Oct 31, 2013 at 4: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 :) Commented Oct 31, 2013 at 4:42
  • its mentioned in my question in the beginning itself.have a look again. Commented Oct 31, 2013 at 4:43
  • 1
    possible duplicate of Reference: Why does the PHP (or other server side) code in my Javascript not work? Commented Oct 31, 2013 at 7:06

2 Answers 2

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'}
Sign up to request clarification or add additional context in comments.

1 Comment

-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.
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,
    ...

6 Comments

epsflag==0 will always return a boolean value of 0 or 1 not true or false.
@RishabhRaj are you sure? in this JavaScript Comparison and Logical Operators show that return boolean
@RishabhRaj in Javascript or PHP?
actually we have to use the javascript condition in JSON,thats y it is getting bit messy.
@RishabhRaj in your sample you use not JSON, you use Object literals
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.