elmas156 Posted October 25, 2011 Share Posted October 25, 2011 Hello everyone, I'm having a difficult time figuring out what my problem is here. I'm trying to submit a couple of strings (to the user they are messages to be sent to other users) and I am having trouble with the string being cut off at quotation marks. Here is the code that I'm using: <?php if (isset($_POST['submit'])) { $staffmessage = $_POST['staffmessage']; $studentmessage = $_POST['studentmessage']; echo "$staffmessage<br /> <br />$studentmessage"; } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> </head> <body> <?php $message1="This is a test message. It does not get cut off here when the \"quotation marks\" are added."; $message2="This is a test message. It DOES get cut off here when the \"quotation marks\" are added."; ?> <form name="form" method="post" action="test.php"> <textarea class="textarea" name="staffmessage" rows="12"> <?php echo $message1; ?> </textarea></p> <input name="studentmessage" type="hidden" value="<?php echo $message2; ?>" /> <input class="submit2" type="submit" name="submit" value="Send Message" /> </form> </body> </html> The result that I'm getting is that when $staffmessage is echoed, I get the full message. When $studentmessage is echoed, the message gets cut off at the first quotation mark. The only thing that is different when creating these two variables is that $staffmessage is submitted using a "textarea" field in the form, and $studentmessage is submitted using a "hidden" field in the form. Other than that, they are handled the exact same way. Can anyone please help me figure out how to remedy this so that $studentmessage is not cut off at the quotation mark? Thanks in advance for your help! Quote Link to comment https://forums.phpfreaks.com/topic/249784-variable-changing-after-form-submitted/ Share on other sites More sharing options...
AbraCadaver Posted October 25, 2011 Share Posted October 25, 2011 echo htmlentities($staffmessage) . "<br /> <br />" . htmlentities($studentmessage); Quote Link to comment https://forums.phpfreaks.com/topic/249784-variable-changing-after-form-submitted/#findComment-1282119 Share on other sites More sharing options...
PFMaBiSmAd Posted October 25, 2011 Share Posted October 25, 2011 The quotes that are in the data have significance in html and they are breaking the value="..." html syntax on your page. You need to use htmlentities, with the second parameter set to ENT_QUOTES, on all data that you output on a web page. Quote Link to comment https://forums.phpfreaks.com/topic/249784-variable-changing-after-form-submitted/#findComment-1282120 Share on other sites More sharing options...
elmas156 Posted October 25, 2011 Author Share Posted October 25, 2011 Thanks very much for your quick responses! Can either of you explain why I was having this problem with the string submitted in the "hidden" field and not the string submitted in the "textarea" field? I was VERY confused about this... Quote Link to comment https://forums.phpfreaks.com/topic/249784-variable-changing-after-form-submitted/#findComment-1282122 Share on other sites More sharing options...
elmas156 Posted October 25, 2011 Author Share Posted October 25, 2011 Now I'm adding this code: <?php $staffmessage1 = $_POST['staffmessage']; $studentmessage1 = $_POST['studentmessage']; $staffmessage = htmlentities($staffmessage1); $studentmessage = htmlentities($studentmessage1); echo "$staffmessage<br /> <br />$studentmessage"; ?> and I'm getting the same result... I don't understand what's wrong here... Quote Link to comment https://forums.phpfreaks.com/topic/249784-variable-changing-after-form-submitted/#findComment-1282123 Share on other sites More sharing options...
Pikachu2000 Posted October 25, 2011 Share Posted October 25, 2011 The quotes that are in the data have significance in html and they are breaking the value="..." html syntax on your page. You need to use htmlentities, with the second parameter set to ENT_QUOTES, on all data that you output on a web page. Quote Link to comment https://forums.phpfreaks.com/topic/249784-variable-changing-after-form-submitted/#findComment-1282124 Share on other sites More sharing options...
elmas156 Posted October 25, 2011 Author Share Posted October 25, 2011 I AM using htmlentities with ENT_QUOTES as the second parameter... <?php if (isset($_POST['submit'])) { $staffmessage = $_POST['staffmessage']; $studentmessage = $_POST['studentmessage']; echo htmlentities($staffmessage, ENT_QUOTES); echo "<br /> <br />"; echo htmlentities($studentmessage, ENT_QUOTES); } ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> </head> <body> <?php $message1="This is a test message. It does not get cut off here when the \"quotation marks\" are added."; $message2="This is a test message. It DOES get cut off here when the \"quotation marks\" are added."; ?> <form name="form" method="post" action="test.php"> <textarea class="textarea" name="staffmessage" rows="12"> <?php echo $message1; ?> </textarea></p> <input name="studentmessage" type="hidden" value="<?php echo $message2; ?>" /> <input class="submit2" type="submit" name="submit" value="Send Message" /> </form> </body> </html> The result I'm getting is the same. Am I using htmlentities properly, or should I be doing something differently? Quote Link to comment https://forums.phpfreaks.com/topic/249784-variable-changing-after-form-submitted/#findComment-1282130 Share on other sites More sharing options...
Pikachu2000 Posted October 25, 2011 Share Posted October 25, 2011 I AM using htmlentities with ENT_QUOTES as the second parameter... No you aren't: <input name="studentmessage" type="hidden" value="<?php echo $message2; ?>" /> Quote Link to comment https://forums.phpfreaks.com/topic/249784-variable-changing-after-form-submitted/#findComment-1282131 Share on other sites More sharing options...
elmas156 Posted October 25, 2011 Author Share Posted October 25, 2011 Thanks again for your help. I had tried it several different ways but it always seemed that I was getting the same result. I found out that the problem was that my browser wasn't refreshing properly each time I would modify the code. It's working now. One other question though: If the string that is being submitted in the textarea field is giving me the desired result, basically, it's not being cut off at the quotes, should I still use htmlentities() for it? Quote Link to comment https://forums.phpfreaks.com/topic/249784-variable-changing-after-form-submitted/#findComment-1282137 Share on other sites More sharing options...
Pikachu2000 Posted October 25, 2011 Share Posted October 25, 2011 No, for <textarea>s, it isn't needed because the value isn't in a quoted value="" attribute. Quote Link to comment https://forums.phpfreaks.com/topic/249784-variable-changing-after-form-submitted/#findComment-1282174 Share on other sites More sharing options...
elmas156 Posted October 25, 2011 Author Share Posted October 25, 2011 Aaaah, that explains a lot! Thanks again! Quote Link to comment https://forums.phpfreaks.com/topic/249784-variable-changing-after-form-submitted/#findComment-1282177 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.