Validating and retaining form data using only php

No, there’s no invalid data error. So if I write an address in the inv_address text area like this:
This House
This Road
This County
using carriage returns
when the form is validated and there is an error in another field the data in the inv_address field is then reproduced like this:
This House\r
This Road\r
This County
I tried using stripslashes() but of course that just gets rid of the slashes!!
Glad I’m helping yuor English!

Some code in your script does this replace.
Ahhhh. got it. mysql_real_escape_string does.
it is strictly required for SQL query, but for query only.
Need to change your code somethere. To make mysql_real_escape_string work only then data is ok.

But I wouldn’t need that until I wanted to enter the data into the mysql database and the carriage return and line break stuff appears in the form before I do that, when I validate the form and there is an error somewhere else. From previous experience mysql ignores the line breaks when the data is entered. Anyway, I sort of solved the problem by using str_replace(). By that I mean that the (
\r)s are replaced by a space, so the inv_address is shown without line breaks, on one line but I have made the cols size smaller and added wrap=“strict” to the <textarea>to try to force the data to display like an address. Can you think of a better way to do this?

There is only one way.
Do not replace \r
with carriage returns.
Make your script not to replace carriage returns with \r
before it needed.
Did you find a place where \r
being added?

Note, Mysql do not ignore the line breaks. Or, it ignores it, of course, as well, as any other character. Mysql do not touches your data. By any means. What you put is what you get. Mysql have nothing to deal with line breaks.

I think you have not quite understood what is happening, probably my explanation is not clear enough.
First we have the blank form as in the code above.
Then the customer fills it in and presses the submit button
The form is validated and one of the fields is not valid
The form now appears with the valid data and the error message/messages for the invalid fields. Now, when this happens the inv_address data (assuming it does not come up with an error) appears with \r
in places where there customer has made a carriage return, as in the post I made earlier. Obviously I don’t want the user to see that and also I don’t want the data entered like that to the mysql database. That is why I have used str_replace() like this:

<textarea name="inv_address" name="inv_address" cols="40" rows="3" wrap="hard" id="inv_address"><?php echo str_replace('\\r\
',' ',$inv_address); ?></textarea>

and then the customer sees the invoice address like this:
This House This Road This County - the \r
is being replaced by a space, which will work fine for mysql entry but doesn’t look as good as when the address is on separate lines. That is why I was wondering if there was a better way to get rid of the \r
?

No, everything correct. I understood all allright.

You have some code in your script, which adds \r
to your data.
You must get rid if it.

But if you tired of it, then okay, do your str replace.
But note it does replace some more symbols too.

I think I will live with str_replace for the moment. But now i come to the last problem in my validation!
I have two select menus in the whole form (I’ve been doing things bit by bit so far but now I’m getting to the last parts). I think part of the problem is that I have to select the options from a mysql database first, then print them in the options - which works fine - but then I cannot replicate the choice made by the customer when the form is validated.
The code for one of the select menus is as follows:

<td class= "content">
<SELECT NAME="region_name">
<OPTION SELECTED VALUE=""> Select One
<?php
//first get the names of the regions
$sql1 = "SELECT region_name FROM tbl_region";
$result1 = mysql_query($sql1);
if ($result1 = mysql_query($sql1)) {
if (mysql_num_rows($result1)){
	while ($row = mysql_fetch_assoc($result1)) {
	echo "<OPTION VALUE=\\"{$row['region_name']}\\">{$row['region_name']}</option>";
}
}
}
?>
</SELECT>
<p class= "error"><?php echo $error8; ?></p></td>

Where should I put the <?php echo?> for the customer’s selection? Or will I have to do things a different way? I’ve tried several ways but they don’t work and when the form is validated the region selection just comes up with “select one” as in the empty form.

thanks

yes. different way
do you know at least what HTML you want to get?

Yes, the select menu should look like the attached image.
So, I add the “select one” and then the options come from the mysql table. I can’t do it with set options in the html according to the system spec. “Select one” shows in the form as it appears on the screen and then when the user clicks on the arrow the options drop down.
I have set the error for this field to be:

if ($region_name = ('Select One')) {
          $error = true;
          $error8 = errorRegionName;
      }

which would work if the user did not make a selection but when the form is validated it just defaults to “select one” - which gives an error but is not what should happen if the user has made a choice

do you know what HTML you need to have menu as displayed on attached picture?

Yes, it’s the html in the code above together with the php there. I assume that it is the ?<php echo that is causing the problem for the replication of the data but can’t see how to change that. Is there a better way of displaying the menu from the results of the mysql query?

everything ok with this way of displaying.
do you understand that this PHP+HTML code will produce HTML only code, which goes to the browser alone?
do you know which HTML code you want in browser?

Mmmm not quite sure what you mean. I know that the results of the mysql only go to the browser. When I was testing the form before I tried validating as well, all the data I wanted was entered into the database. The problem is that in the customer table, where the contact info goes, the region-name only has an id which is generated from the region table so when I send the data from the form to the customer table I have to do a query on the region table to get the id of the chosen region to then insert it into the customer table with the rest of the data. So, surely it does not matter that the code I’m using for the menu in the form will only go to the browser? Or am I being very dense?

while ($row = mysql_fetch_assoc($result1)) {
if ($_POST[‘region_name’]==$row[‘region_name’]) $sel=" selected"; else $sel=“”;
echo “<OPTION VALUE=\”{$row[‘region_name’]}\“$sel>{$row[‘region_name’]}</option>”;

I tried that but I then get an error message that region_anme is an undefined index, so then I changed the last 2 rows around like this:

<SELECT NAME="region_name">
<OPTION SELECTED VALUE=""> Select One
<?php
//first get the names of the regions
$sql1 = "SELECT region_name FROM tbl_region";
$result1 = mysql_query($sql1);
if ($result1 = mysql_query($sql1)) {
if (mysql_num_rows($result1)){
	while ($row = mysql_fetch_assoc($result1)) {
echo "<OPTION VALUE=\\"{$row['region_name']}\\"$sel>{$row['region_name']}</option>";
if ($_POST['region_name']==$row['region_name']) $sel=" selected"; else $sel="";
}
}
}
?>
</SELECT>

and I then get an error message that sel is undefined. Have I used your code incorrectly?

oh yes, of course.
everything the same

But I still get the error message “undefined index: region_name”
the code I use is this:

<SELECT NAME="region_name">
<OPTION SELECTED VALUE=""> Select One
<?php
//first get the names of the regions
$sql1 = "SELECT region_name FROM tbl_region";
$result1 = mysql_query($sql1);
if ($result1 = mysql_query($sql1)) {
while ($row = mysql_fetch_assoc($result1)) {
if ($_POST['region_name']==$row['region_name']) $sel=" selected"; else $sel="";
echo "<OPTION VALUE=\\"{$row['region_name']}\\"$sel>{$row['region_name']}</option>";
}
}
?>
</SELECT>

yea, i see
if (isset($_POST[‘region_name’]) AND $_POST[‘region_name’]==$row[‘region_name’]) $sel=" selected"; else $sel=“”;

and change line above to
<OPTION VALUE=“”> Select One</OPTION>

Oh, that works! thank-you so much!
But one little problem…
I’m not sure what the $error condition for region_name should be. At the moment an error message comes up for that field even if a region has been selected.

ugh. how do you check it?
it should be whare all other conditions are