Link to home
Start Free TrialLog in
Avatar of peter_coop
peter_coopFlag for United Kingdom of Great Britain and Northern Ireland

asked on

php variables and jquery error

I am trying to pass php variables through to jquery for processing. The only var coming through though is 'message'. Can someone check my code for error. Thanks

php code

if (mysql_num_rows($result)) 
			{
			
				 
				//$rowdate = date("d/m/Y",strtotime($row['destroyed_date'])); 
			
				while($row = mysql_fetch_array($result)) 
				{
			
					$rowdate = date("d/m/Y",strtotime($row['date_frm']));
					$id = $row['id_frm'];
					$from = $row['from_frm'];
					$subject = $row['subject_frm'];
					$message = $row['message_frm'];
					
					$myString = <<<EOF
					<div class='msgTrue' style='background-color:#eee;' data-message='%s'>
					<img src="images/sml_new_mail_icon.jpg">%s
					<div style="float:right;margin-right:35px;margin-top:5px;font-size:9px;">%s</div><br />
					<span style="font-weight:bold;">%s</span>
					</div>
					<br />
					<!-- The following end tag need to be at the start of the line -->
EOF;
					printf($myString, $message, $subject, $rowdate, $from);
			
				}
			}

Open in new window


jquery code

$(function() {
     $(".msgTrue").click(function() {
     //alert($(this).data('message'));
     $("#msgTrueSubj").show().html( '<div style="width:75%;margin-top:25px;">' 
     +   $(this).data('message')  +  '</div>' );
		 
});
	});

Open in new window

Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Maybe try removing the quotes around message.  As written I believe it is a literal string.
Avatar of Chris Stanyon
Hi Peter,

Your code is looking OK, but I can't test it because I don't have access to your database. Once you've loaded your page up in your browser, have a look at the source and make sure each of your DIVs have the data-message attribute properly filled in.

Here's a fiddle that is a working demo of what you need, but without the database bits:

http://jsfiddle.net/ChrisStanyon/XYHDu/

If you still get problems, a link to a demo page would help us to help you find a solution.
Avatar of peter_coop

ASKER

Hi Chris

I am confused buy what is happening. Am I correct in assuming that if data-from='%s' then it should send the $from var? It is still sending the 'message'. I have pasted what is output in view source for you. Thanks

BTW: Is there a special way to format code here. When I paste it seems to throw formatting out.

<div class='msgTrue' style='background-color:#eee;' data-from='this is test message 5 to make sure it works'>
	<img src="images/sml_new_mail_icon.jpg">new test message #5
	<div style="float:right;margin-right:35px;margin-top:5px;font-size:9px;">22/09/2009</div><br />
	<span style="font-weight:bold;">Admin</span>
	</div>
	<br />
	<!-- The following end tag need to be at the start of the line -->
	<div class='msgTrue' style='background-color:#eee;' data-from='this ia new test message to make sure that the mark function is working properly.'>
	<img src="images/sml_new_mail_icon.jpg">test message
	<div style="float:right;margin-right:35px;margin-top:5px;font-size:9px;">22/09/2009</div>
        <br />
	<span style="font-weight:bold;">Admin</span>
</div>
	<br />
	<!-- The following end tag need to be at the start of the line --> 

Open in new window


@Ray

Made no difference. Thanks
Right. there seems to be a discrepancy between the code you're posting. In the HTML source you've just posted, you have this:

<div class='msgTrue' style='background-color:#eee;' data-from='this is test message 5 to make sure it works'>

But in the PHP above you have this:

<div class='msgTrue' style='background-color:#eee;' data-message='%s'>

In the source, your data attribute is called data-from but in the PHP it's called data-message. That seems like the source is being generated from a different PHP file!
                              
In jQuery, $(this).data('message') will return the value of an attribute called data-message, but you don't have that in your HTML source - you have data-from so you'd need to use: $(this).data('from') to get it's value.
The %s is just a place holder as previously discussed and actually has no bearing on the data attribute

<?php
$myName = "chrisstanyon";
$printf("<div data-widget='%s'>This is a widget</div>", $myName);
?>

Will output the HTML:

<div data-widget='chrisstanyon'>This is a widget</div>

And this jQuery:

alert ( $(this).data('widget') );

will alert:

chrisstanyon

Hope that makes sense :)
Peter,

I've just re-read you previous comment and realised what you've done.

Am I correct in assuming that if data-from='%s' then it should send the $from var?

No, you're not. Let me try and explain. When you call the printf() function it will put the variables into the string in the order that they appear. So:

printf($myString, $message, $subject, $rowdate, $from)

That will put the content of $message into $myString where it finds the first %s in $myString. It will put $subject where it finds the 2nd %s, $rowdate where it finds the 3rd, and $from where it find the 4th.

In your example above, you changed the name of the atribute to data-from, but left the arguments in the same order and because $message was the first variable that went in the first place holder.
Then how do I pass all variables to jquery for processing Chris? How do I code 2nd %s, 3rd%s etc. Thanks
Not sure I follow. You have this in PHP:

$rowdate = date("d/m/Y",strtotime($row['date_frm']));
$id = $row['id_frm'];
$from = $row['from_frm'];
$subject = $row['subject_frm'];
$message = $row['message_frm'];

You can use the printf() function like this:

printf("We are replacing some place holder. This is the first value: %s...This is the second one: %s", $subject, $message)

This function will replace the first %s with the value of $subject (because its the first argument in the printf function) and will replace the second %s with $message, because it's second.

$fruit = "Banana";
$colour = "Yellow";
printf("The %s is %s", $fruit, $colour) //this will give The Banana is Yellow
printf("The %s is %s", $colour, $fuit) //this will give The Yellow is Banana

Now look at the code below:

$myString = <<<EOF
<div class='msgTrue' style='background-color:#eee;' data-message='%s'>
<img src="images/sml_new_mail_icon.jpg">%s
<div style="float:right;margin-right:35px;margin-top:5px;font-size:9px;">%s</div><br />
<span style="font-weight:bold;">%s</span>
</div>
<br />
EOF;

printf($myString, $message, $subject, $rowdate, $from);

Open in new window

The content of $myString has 4 placeholders (%s), so the printf() function needs four arguments, as well as the string, and the arguments will replace the place holders in order. The first placeholder in $myString (data-message='%s'>) will be replaced by $message (it's the first argument in printf()). The second will be replaced by $subject (because its the second argument) and so on.

To get these values to use in jQuery will depend on where they are stored in the HTML. Because you are storing the $message value in an attribute called data-message, you can call it like this:

//HTML
<div data-message="this is a message">
//jQuery
$(this).data('message')

If it was stored in an attribute called data-peter, then you'd need to call it like this:

//HTML
<div data-peter="this is a message">
//jQuery
$(this).data('peter')

If I've missed something, forgive me. I'm not entirely sure which bit you're stuck on!
Where I am getting confused chris, is in the correct format to use for printf. I have posted my amended code which I thought was the way to do it, but I am getting error of:

Warning: printf() [<a href='function.printf'>function.printf</a>]: Too few arguments

Open in new window


$myString = <<<EOF
	<div class='msgTrue' style='background-color:#eee;' data-message='%s' data-subject='%s' data-rowdate='%s' data-from='%s'>
	<img src="images/sml_new_mail_icon.jpg">%s
	<div style="float:right;margin-right:35px;margin-top:5px;font-size:9px;">%s</div><br />
	<span style="font-weight:bold;">%s</span>
	</div>
	<br />
	<!-- The following end tag need to be at the start of the line -->
EOF;
	printf($myString, $message, $subject, $rowdate, $from);

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Chris Stanyon
Chris Stanyon
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Chris

You have explained yourself very well. It is me that was having probs with this. Thanks to your detailed explanation, I have now managed to grasp what you were trying to say earlier. I have raised the points to 500 to reflect the work and patience you have put into this question. Thank you very much.
Thanks once again.
No worries Peter. Good luck with it :)