Jump to content

Variable changing after form submitted


elmas156

Recommended Posts

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 />&nbsp<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!

Link to comment
https://forums.phpfreaks.com/topic/249784-variable-changing-after-form-submitted/
Share on other sites

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.

 

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

Now I'm adding this code:

 

<?php
$staffmessage1 = $_POST['staffmessage'];
$studentmessage1 = $_POST['studentmessage'];

$staffmessage = htmlentities($staffmessage1);
$studentmessage = htmlentities($studentmessage1);

echo "$staffmessage<br />&nbsp<br />$studentmessage";
?>

 

and I'm getting the same result... I don't understand what's wrong here...

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.

 

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?

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?

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.