Jump to content

Need help with a function


ztimer

Recommended Posts

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
				} ?>

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Link to comment
Share on other sites

:qft: 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.

 

Link to comment
Share on other sites

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>";
?>

Link to comment
Share on other sites

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 .

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.