views:

91

answers:

4

Hi, this is the continuation of my previous question. Since I just logged in that day without an Open Id, I don't know how to login to that account and edit further. So I created a new Open Id enabled account and posted this as a new question.

Now I have the code like this. Inside the onclick event, the value is stored in the $selectedId correctly. But When I try to pass that value in the url, I do not get the correct value. The last value in the for loop is passed.

 <script type="text/javascript">

 $(document).ready(function(){

 <?php foreach ($Forms as $r): ?>

    $("<li><a id='<?=$r['Form']['id'];?>' data-attr='Formentries'  href='#'><?=$r['Form']['name']?></a></li>").appendTo("#headers").click(function () {

    <?php $selectedFormId=$r['Form']['id'];?>

    alert("selId: "+<?php echo $selectedFormId;?>); //here the selected id is alerted
   });
 alert("outside the loop"+<?php echo $selectedFormId;?>); //here the last value in the loop is alerted
 });

Once out of the click function, the value of $selectedFormId changes to the last value in the array. Can someone help me with this?

Actually what I am trying to achieve is, I list a set of Forms as links, and when I select the links I want its id to be saved in a php variable. I want it particularly be saved in a php variable coz after I select a Form I have an option to export the entries in the form through another link

 <a href="localhost/FormBuilder/reports/export/<?php echo $selectedFormId;?>" class="thickbox button" title= "Export" >Export</a> .

So I want the id in there,so that I could pass it to the export function in the controller. I also get the selected id in a javascript variable as

 formid=$(this).attr("id");

but I do not know how to pass this value to the export function in the controller.

A: 

As far as I can see you are setting $selectedFormId inside each loop in the iteration, so the assignment code will only be set when the click event is fired.

Therefore, logically, when you exit the loop, $selectedFormId will be the last item in the array. Are you meaning to put a condition around the assignment? Don't forget, the server side code will execute regardless! It won't care about client side conditions or closures!

if (something){
    <?php $selectedFormId=$r['Form']['id'];?>
}

And, again echoing the above comments, you should really be trying to achieve code separation. The above really is tag soup!

James Wiseman
A: 

I don't know if i've understood your question very well but the $selectedFormId value is set inside the loop, so everytime the loop is executed the variable is set and when the loop finishes $selectedFormId gets the last processed value. I think you should set it outside the loop.

mck89
well, I will have the id value only inside the loop..
A: 

as you are declaring it inside the block so it is not available outside;

either you declare it before the $(document).ready block or finish its usage it inside this block itself.

TheVillageIdiot
A: 

You are mixing client and server-side code -- the code you wrote seems like you're expecting a JavaScript function on the client-side to magically set a PHP variable on the server-side, when in reality that type of operation is not possible.

What you should maybe do is have your click event set a variable on the client-side. Then set an onclick on your export link to construct a URL and redirect to it based on the locally stored variable value.

That's probably not the best solution, but it would be one option.

Cory Larson
Then set an onclick on your export link to construct a URL and redirect to it based on the locally stored variable value.-- How to use that locally stores javascript variable in my export link url? That I exactly what I'm trying to do..