ztimer Posted August 30, 2012 Share Posted August 30, 2012 Hi. I'm in a need of help here. First of all i will explain what i'm after. I will query a user database and get a list of users. Then i will make a subquery based on the users active and get a list with a data. Now i have a user and a data. I want to be able to click on a link beside a user and whitout leaving the page post current user data to a php file located /include/workhours/lock_month_data.php witch will use $_Get values and place them in database and send info back that it has happened. what is my problem. I cant get it working in a loop when i use only one line whitout a loop everything works. when looping only the last worker info is there.? Please help. this is a test code but you get the idea when you see it. $q1="SELECT * FROM user_meta WHERE status=1 ORDER BY display_name limit 3"; $r1=mysql_query($q1); while ($row=mysql_fetch_array($r1)){ $worker_id = $row['user_id']; $value = $row['user_id']; echo "<a id='lock_button_$worker_id'>".$row['display_name']."</a><div id='display'></div>"; ?> <script> $(document).ready(function(){ $('<?php echo "#lock_button_".$worker_id; ?>').click(function(){sendValue();});}); function sendValue(){ $.post("include/workhours/lock_month_data.php?send=<?php echo $value; ?>", function(data){ $('#display').html(data.returnFromValue); }, "json"); } </script> <?php } ?> Quote Link to comment https://forums.phpfreaks.com/topic/267827-need-help-with-a-function/ Share on other sites More sharing options...
ztimer Posted August 30, 2012 Author Share Posted August 30, 2012 Please help. Quote Link to comment https://forums.phpfreaks.com/topic/267827-need-help-with-a-function/#findComment-1374087 Share on other sites More sharing options...
DavidAM Posted August 30, 2012 Share Posted August 30, 2012 According to the forum Rules & ToS, you are not supposed to bump: Users should not "bump" topics that are still on the first page of the forums. If you bump, you must provide additional information. If you resort to bumping, chances are your question needs to be re-thought and re-described (see Eric Raymond's "How To Ask Questions The Smart Way"). I'm not a huge fan of lambda functions, and I'm not really a JavaScript expert; but that function sendValue() looks like it is defined in global scope. Since you are sending that script inside the loop, it looks to me like you are defining that function multiple times. I'm pretty sure JavaScript will choke on that. I think you need to re-think that setup. As it is, you are registering several document.ready() functions, which is unnecessary. I would give all of those links the same class (i.e. class="lockLink"). Then do the document.ready() call once to set them all: $(document).ready(function() { $('.lockLink').click(showValue); } (or something like that). Then in the showValue() function, grab the ID of the link that was clicked (this) and send it through AJAX to the PHP script. Quote Link to comment https://forums.phpfreaks.com/topic/267827-need-help-with-a-function/#findComment-1374091 Share on other sites More sharing options...
ztimer Posted August 30, 2012 Author Share Posted August 30, 2012 sorry for so called bumping and thank you for a answer. I was hoping for a example of some sort so i could see what i have done wrong and to save a code to use in a future. Quote Link to comment https://forums.phpfreaks.com/topic/267827-need-help-with-a-function/#findComment-1374093 Share on other sites More sharing options...
DavidAM Posted August 30, 2012 Share Posted August 30, 2012 As I said, I'm no JS expert, and there might be easier/better ways to do this. But this is how I typically do it: <script> $(document).ready(pageInit); function pageInit() { $('.lockLink').click(sendValue); } function sendValue() { linkID = $(this).prop('id'); $.post("include/workhours/lock_month_data.php?send=" + linkID, function(data){ $('#display').html(data.returnFromValue); }, "json"); } </script> <?php $q1="SELECT * FROM user_meta WHERE status=1 ORDER BY display_name limit 3"; $r1=mysql_query($q1); while ($row=mysql_fetch_array($r1)){ $worker_id = $row['user_id']; $value = $row['user_id']; echo "<a class='lockLink' id='lock_button_$worker_id'>".$row['display_name']."</a><div id='display'></div>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/267827-need-help-with-a-function/#findComment-1374117 Share on other sites More sharing options...
ztimer Posted August 31, 2012 Author Share Posted August 31, 2012 The code looks good. I found it working 100% but if i want to add multiple values to send in a single click? Any ideas how to accomplish this. I hope you have this also. Quote Link to comment https://forums.phpfreaks.com/topic/267827-need-help-with-a-function/#findComment-1374218 Share on other sites More sharing options...
ztimer Posted August 31, 2012 Author Share Posted August 31, 2012 Can it be done simply by <script> $(document).ready(pageInit); function pageInit() { $('.lockLink').click(sendValue); } function sendValue() { first = $(this).prop('id'); <--- first value to send second = $(this).prop('id'); <-- second value to send $.post("include/workhours/lock_month_data.php?send=" + first + "&auh=" + second , function(data){ $('#display').html(data.returnFromValue); }, "json"); } </script> <?php $q1="SELECT * FROM user_meta WHERE status=1 ORDER BY display_name limit 3"; $r1=mysql_query($q1); while ($row=mysql_fetch_array($r1)){ $worker_id = $row['user_id']; $value = $row['user_id']; echo "<a class='lockLink' id='lock_button_$worker_id'>".$row['display_name']."</a><div id='display'></div>"; } ?> How to define that the first value is let say $row['user_id'] and the second is like $row['someotherdata']. i could make a hidden textboxes to add a value to them but how to use the texboxex id=''. Cant seem to get more than one value to post to lock_month_data.php . Quote Link to comment https://forums.phpfreaks.com/topic/267827-need-help-with-a-function/#findComment-1374229 Share on other sites More sharing options...
DavidAM Posted August 31, 2012 Share Posted August 31, 2012 The $(this).prop('id'); call is retrieving the ID of the object that was clicked. You can inject additional "values" into the tag, but it is non-standard and may not be cross-browser friendly. I'm not sure why you would want to pass a value that you retrieved from the database since the PHP script will have access to the database during the AJAX call. However, if you really want to send multiple values to the click function, you can do something like this: <script> function sendValue(value1, value2) { $.post("include/workhours/lock_month_data.php?send=" + value1 + "&other=" + value2, function(data){ $('#display').html(data.returnFromValue); }, "json"); } </script> <?php $q1="SELECT * FROM user_meta WHERE status=1 ORDER BY display_name limit 3"; $r1=mysql_query($q1); while ($row=mysql_fetch_array($r1)){ $worker_id = $row['user_id']; $value = $row['user_id']; $value2 = $row['display_name']; echo "<a class='lockLink' id='lock_button_$worker_id' onclick=\"sendValue($worker_id, '$value2');\">".$row['display_name']."</a><div id='display'></div>"; I took out the pageInit() function and put the onClick() event directly in the tag so we could pass the values there. Quote Link to comment https://forums.phpfreaks.com/topic/267827-need-help-with-a-function/#findComment-1374377 Share on other sites More sharing options...
ztimer Posted September 1, 2012 Author Share Posted September 1, 2012 Thank you so much for your help. This really helped me a lot and lets me do a lot of new things without the need of posting from page to page. I had searched this type of a example for nearly a week on the web and eater did search it wrong or just did not find it. there are a lot of too complex examples out there that does not explain to a fellow that just had started. Thank you again for your help. Wish you all the best. Quote Link to comment https://forums.phpfreaks.com/topic/267827-need-help-with-a-function/#findComment-1374459 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.