Value of variable changes once out of the click function in jQuery - php

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.

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.

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.

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!

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.

Related

Session variable passing

I've got a php script which builds a html table via echoing data, But i want to add a link onto one of the values and pass that value to the next page.
<td><a href='redirect.php'><?php $_SESSION['WR'] = $row['WorkOrdRef'];echo $row['WorkOrdRef'];?></a></td>
is the line in question but this will only pass the last value added to the table.
Oh, it doesnt work like this. the php code gets executed no matter if you click the link.
I guess the easiest way to do this is to pass it as a get parameter.
html page:
<?=$cellContent?>
redirect.php:
$clickedcell = $_GET['clickedcell']
now the $clickedcell will have the data from the previous page about what cell did the user click.
If you want to use session for some reason, you still have to pass it with GET or POST and store it after the user clicks.
hopefully this is understandable and good luck with your project.
you can change the session by get method also it is possible building by javascript
in the same page add this
if(isset($_GET["clicked"])){
$_SESSION['WR'] = $row['WorkOrdRef'];
$redirect'<META HTTP-EQUIV="REFRESH" CONTENT="0;URL='.$adres.'/"> ';
return $redirect;
}
and then change your url
<td><?php echo $row['WorkOrdRef'];?></td>

Make value=null when page first loads

Is it possible to remove a value whilst the page is loading?
I'm using OSClass, and on one of the pages it's by default adding a value for region (cambridshire):
I need to clear this value since it's causing problems, everytime I type something else in, by default, it registers it as Cambridgeshire...
If I look at code for it:
It's being generated by a function (can see my JQuery attempt to clear it which hasn't worked).
Then if I search the function is splits up in many different parts, so I don't know where to go from there.
Basically, is there a way to remove the value when page loads and save the new value when the user submits?
JSFiddle - Note it won't display anything due to the way code is generated
Have you tried removing <?php ItemForm::region_text(); ?> on it's own? Or passing an empty string <?php ItemForm::region_text(''); ?>
I'm not familiar with OSClass though I'm afraid.
Paste this code below the jQuery initialization:
$(document).ready(function(){
$("#region").val('');
})

PHP selectbox with onchange action

I am struggling with this problem for week now. What I would like to achieve is have a select box on website and when I change value of that selectbox I would like to show/hide some fields for filling in (selectbox function works so I am not putting code here).
Actually I was thinking if I can pass value from vlan variable to other field variable when changing selection of the selectbox and then make a check condition on that new variable, any help would be appreciated (By the way, I want to keep as much as possible to current code since I am editing application which has many dependencies which I do not intend to brake;))
$vlan=array("0"=>my_("NO"),
"1"=>my_("YES"));
insert($f,selectbox($vlan,
array("name"=>"field","onChange"=>$field),
$field=key($vlan)));
if ($field == "1") {
insert($f,input_text(array("name"=>"vlannr",
"value"=>"0",
"size"=>"4",
"maxlength"=>"4")));
}
else {
insert($f,textbr(my_("NO VLAN")));
}
Ok so I got the part where I assign value from array $vlan to another variable. Now I need to display new form whenever the variable changes to 1 and hide it when it changes back to 0. Any ideas ? (page can be reloaded , I do not care about that)

onClick add to database

I have the below code, and I'm not sure if it even works. Basically, I want to do a like system, whereby when I click on the link, it adds a +1 to the user's likes.
I've tried so hard to read up but I just don't get anything, and I found a code similar below.
When I clicked on the link, it does process my insert.php page, but I don't know how to get the variable values...
What should I do? I'm not sure if the code structure below is correct...
Thanks!
<script>
function insertSalary()
{
var salary = $("#salary").val();
$.post('insert.php', {salary: salary}, function(data)
{
$("#current-salary").html(data);
});
}
</script>
<div id="current-salary">
<a id="salary" onClick="insertSalary();">+1</a>
</div>
The variable will be in your php script as $_POST['salary']
The value of salary is passed as part of the post method in jquery.
So you can do:
$.post('script.php', {salary: 100}, function(data){...});
and this will pass the value 100 to your php script as the salary value.
In php the $_POST and $_GET hashes contain the data that you pass with a given request. In jquery $.post, $.get $.ajax create requests and take data hashes to build the data you want to pass with the request.
While this may work, I would recommend separating your logic from your website by putting the javascript in an external file and then link your HTML page to it.
I would also advise against declarative event binding which you have done by specifying onClick="insertSalary()".
jQuery provides a method to pro-grammatically assign functions to events using the on method. So for your code, you could use:
$('#current-salary').on('click', insertSalary());

Passing the value to the next page without a form

I have a page I am constructing and I need to pass in the values of the option dropdowns to the next page. The problem is that these dropdowns are not in a form.
http://posnation.com/test/pre_config/pre_config_step_2.html
Basically what i need to pass to the next page is that when i click "Proceed To Next Step" I need to pass the value of the type of field like "restaurant" and the number of stations "2" if the user selects restaurant and 2.
HTML:
<a id="proceed" href="foo.html">Proceed!</a>
JS:
$('#proceed').click(function() {
location.href = this.href +'?someVal='+ escape($('#my_select').val());
return false;
});
Working example that executes a formless google search: http://jsfiddle.net/CKcbU/
You basically just add what you want to the query string with javascript.
But really, if at all possible, you should use a form with method="get" which pretty much does this for you without any JavaScript at all.
Use a query string.
http://posnation.com/test/pre_config/pre_config_step_2.html?restaurant=The+Eatery&stations=2
In other words, pass them as part of the URL when calling the next page. The next page will be responsible for reading the query string and extracting the values out.
http://en.wikipedia.org/wiki/Query_string
I do not know what you are using to code in so I cannot be detailed regarding the mechanics of constructing the URL or parsing out the values from the query string on the receiving page.
Here is an article on doing it using JavaScript:
http://javascript.about.com/library/blqs.htm
You can use JavaScript for the same. Assign a onclick event with button of proceed and call a function. In this function use:
windows.location=url?rest=valuerest&opt=valueopt
I am not sure what you are working with, but with almost any framework that I know of you can pass a parameter as part of the url.
These would work fine.
http://posnation.com/test/pre_config/step_2
http://posnation.com/test/pre_config/step_3
Then, just grab the parameter and react accordingly.

Categories