KevinM1 Posted November 6, 2007 Share Posted November 6, 2007 My problem is a bit involved, so this will be a pretty long post. It stems from my thread in the 'Application/Design Layout' subforum (http://www.phpfreaks.com/forums/index.php/topic,163951.0.html), so those interested in helping me should probably read through that first to get the background info. I'm trying to set some preliminary values in my form-switching JavaScript. So, in order to do that, I've hard-coded some JS in my PHP script: <?php $configQuery = "SELECT * FROM sbclassified_mails ORDER BY id ASC"; $configResult = mysql_query($configQuery); ?><script type="text/javascript"> var mailSettings = new Array(); <?php while($row = mysql_fetch_assoc($configResult)){ ?> mailSettings[<?php echo $row['mailid'];?>] = new Array('<?php echo $row['fromid'];?>', '<?php echo $row['subject'];?>', '<?php echo nl2br($row['mail']);?>'); <?php } //end while-loop ?> </script> As you can see, I'm trying to store the mail settings that were saved in the database in a two-dimensional hash (the index of the mailSettings array, while numeric, isn't sequential -- it's a long story). My problem is that I'm getting an unterminated string literal with the $row['mail'] values. I'm thinking that this error is stemming from the length of those values -- I'm trying to store a fairly large chunk of text in there, and they have linebreaks and tabs (although I tried combating against that with PHP's nl2br function). I've double-checked my quotes, and I can't see a problem there. Here's what the source code shows: <script type="text/javascript"> var mailSettings = new Array(); mailSettings[1] = new Array('webmaster@rextrader.com', 'Welcome to RexTrader.com', 'Hi <fname> <lname>,<br /> Welcome to RexTrader.com.<br /> Your registration information is as follows:<br /> <br /> Username: <br /> Password: <br /> Email: <br /> <br /> Thanks for being part of our website.<br /> <br /> Regards,<br /> Admin<br /> For login click <loginurl>'); . . . </script> Any ideas? Thanks in advance! Quote Link to comment Share on other sites More sharing options...
fenway Posted November 6, 2007 Share Posted November 6, 2007 Two things: 1) It's not really a hash, since the RVALUE should really be: { 'fromid':'<?php echo $row['fromid'];?>', 'subject':'<?php echo $row['subject'];?>', 'mail':'<?php echo nl2br($row['mail']);?>' }; 2) That's because you have to escape linebreaks in JS -- in fact, if you had single-quotes in your subject field, you'd have problems too. So I don't know what nl2br() is not doing... Quote Link to comment Share on other sites More sharing options...
KevinM1 Posted November 6, 2007 Author Share Posted November 6, 2007 Two things: 1) It's not really a hash, since the RVALUE should really be: { 'fromid':'<?php echo $row['fromid'];?>', 'subject':'<?php echo $row['subject'];?>', 'mail':'<?php echo nl2br($row['mail']);?>' }; Hmm...maybe I should use some JSON. I'll try it next. 2) That's because you have to escape linebreaks in JS -- in fact, if you had single-quotes in your subject field, you'd have problems too. So I don't know what nl2br() is not doing... I think I know how to solve that as well. Hopefully my next response will be a big, green 'Solved' checkmark. Quote Link to comment Share on other sites More sharing options...
KevinM1 Posted November 6, 2007 Author Share Posted November 6, 2007 Well, I'm much closer than I was before. I've changed the JS inside my PHP file to: <?php $configQuery = "SELECT * FROM sbclassified_mails WHERE id != 4 ORDER BY id ASC"; $configResult = mysql_query($configQuery); ?><script type="text/javascript"> var mailSettings = new Array(); <?php while($row = mysql_fetch_assoc($configResult)){ ?> mailSettings[<?php echo $row['mailid'];?>] = {from : '<?php echo $row['fromid']?>', subject : '<?php echo $row['subject']?>', mail : '<?php echo $row['mail']?>'}; <?php } //end while-loop ?> </script> The mail values in the JSON work now because I fixed my input validation/escaping. So, I'm left with JSON objects like: var mailSettings = new Array(); mailSettings[1] = {from : 'webmaster@rextrader.com', subject : 'Welcome to RexTrader.com', mail : 'Hi <fname> <lname>,\nWelcome to RexTrader.com.\nYour registration information is as follows:\n\nUsername:\t<username>\nPassword:\t<password>\nEmail:\t\t<email>\n\nThanks for being part of our website.\n\nRegards,\nAdmin\nFor login click <loginurl>'}; I kept the objects in an array because that seems to be the easiest way for me to get to them with my external file. That being said, I can't seem to get a handle on the objects within that file. I've tried an alert box, but I get undefined values. Specifically, this code: alert('Array values -- Reply address: ' + mailSettings[value].from + ' Mail Subject: ' + mailSettings[value].subject); Returns: Array values -- Reply address: undefined Mail Subject: undefined The 'value' variable comes from a select element, which lets the user choose which e-mail to view/edit. So, the question remains: how can I get into the heart of my JSON objects? I'll post all of my external file and my PHP script, if you wish. Quote Link to comment Share on other sites More sharing options...
fenway Posted November 6, 2007 Share Posted November 6, 2007 You shouldn't keep them in an array -- the "uid" should be the key of the top hash. Once you've changed this, go through all of the keys in the mailSettings hash, make sure all your uids are there. Quote Link to comment Share on other sites More sharing options...
KevinM1 Posted November 6, 2007 Author Share Posted November 6, 2007 You shouldn't keep them in an array -- the "uid" should be the key of the top hash. Once you've changed this, go through all of the keys in the mailSettings hash, make sure all your uids are there. I'm lost. I quite literally have no idea what you're saying here. I'm thinking the uid would be the value I'm currently using for my array indexes, but I'm not sure what you're trying to tell me in terms of what to do with them. Sorry. Quote Link to comment Share on other sites More sharing options...
fenway Posted November 6, 2007 Share Posted November 6, 2007 It sounds like you can't find the array indexes that you made... I'm saying you should put them into a hash. Quote Link to comment Share on other sites More sharing options...
KevinM1 Posted November 6, 2007 Author Share Posted November 6, 2007 It sounds like you can't find the array indexes that you made... I'm saying you should put them into a hash. Yeah, that's the thing that's getting me, though. The array indexes are equal to the value that the select element currently has. So, if I select welcome message, the array index should be 1. In terms of putting them into a hash, there's still some confusion. I'm thinking of a hash as unique key => value. That's why I considered my array setup a hash. Even though the keys were numeric, they were unique and bound to the greater context of the form. With that said, I'm having a hard time visualizing how to hash my hash, so to speak. Are you suggesting that I make a separate hash for the ids? Something like: var hash = new Array(); hash['Welcome'] = 1; hash['Approved'] = 2; . . . Or am I completely misreading you? Quote Link to comment Share on other sites More sharing options...
KevinM1 Posted November 6, 2007 Author Share Posted November 6, 2007 Okay, it's now working. Apparently, in JSON, you need quotes around the name of the property you're trying to set. D'oh! Quote Link to comment Share on other sites More sharing options...
fenway Posted November 6, 2007 Share Posted November 6, 2007 Okay, it's now working. Apparently, in JSON, you need quotes around the name of the property you're trying to set. D'oh! Can't believe I didn't see that... I thought my example had quotes. Quote Link to comment Share on other sites More sharing options...
KevinM1 Posted November 6, 2007 Author Share Posted November 6, 2007 Okay, it's now working. Apparently, in JSON, you need quotes around the name of the property you're trying to set. D'oh! Can't believe I didn't see that... I thought my example had quotes. Yours did, but one in my thread in the Application Design subforum didn't. Because of that, I thought they were optional. It's funny...using the correct syntax fixes a lot of problems! Quote Link to comment Share on other sites More sharing options...
fenway Posted November 6, 2007 Share Posted November 6, 2007 Nope... only perl figures out what to do with barewords properly. Quote Link to comment 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.