Lose a space in email in variable

I have a form where I allow users to enter a city, I then store that value in a variable

$city = $_POST['city'];

Then I try to put it in another variable

$message = <<< END
...
<td>City</td><td><input type="text" name="city" value="{$city}"></td>
..,.
END;

For some reason when I enter San Diego, it comes in my email as SanDiego, why is it loosing the space?

When storing and retrieving try enclosing the city name in quotes.

looks more like it is tighting the space rather than loosing it.

Show us what happens to the variable between where it is input, and where it is placed in the email.

Here is where I receive the input

<input type="text" name="city" class="form-control" id="city">

Its stored,

$city = $_POST['city'];

lastly, here it is in the email

$message = <<< END
....
....
<td>City</td><td><input type="text" name="city" value="{$city}"></td>
....
....
END;
  mail("admin@houston-asp.com","Submit a Review",$message,$headers);

Has the space already gone if you var_dump($message) prior to sending the email in your code above, or is it still present at that point? I’m wondering if it’s anything to do with how your mail client deals with the incoming html email, I can’t say I’ve ever sent an email that includes a form input tag.

$message = <<< END
....
....
<td>City</td><td><input type="text" name="city" value="{$city}"></td>
....
....
END;
var_dump($message); // is the space still here at this point?
  mail("admin@houston-asp.com","Submit a Review",$message,$headers);

ya the space seems to get cut on the var_dump()

When I view the source

<td>City</td><td><input type="text" name="city" value="San
Diego"></td>

so it looks like instead of a space, its seing a newline though

OK, so the next thing is to var_dump($city) just after you’ve copied it from the $_POST array. Is it OK there?

Yes, I see the space there. So, Im guessing its loosing it in $message
I put the variable in like

$message = <<< END
...
<td>City</td><td><input type="text" name="city" value="{$city}"></td>
...
END;

Well, there must be something else going on somewhere. I created a very small php file which contains this code:

<?php
$city = "San Diego";
var_dump($city);
$message = <<< END
<td>City</td><td><input type="text" name="city" value="{$city}"></td>
END;
var_dump($message);

and it produces this:

string 'San Diego' (length=9)

string '<td>City</td><td><input type="text" name="city" value="San Diego"></td>' (length=71)

As you see, it still has the space. So I can’t say why your code behaves differently, perhaps someone with more knowledge can explain?

Ya, something screwy is going on here, I noticed a few things though…
this a screenshot of my result


I think Its mixing " with ’
But I don’t see why theres a difference (heres my code, its like yours)

$city = $_POST['city'];
....
$message = <<< END
....
<td>City</td><td><input type="text" name="city" value="{$city}"></td>
...
</form>
END;

Any ideas?

Sorry, I can’t add anything. Your screen-shot shows the city name correctly at the top, then it seems to go wrong once you add it into the $message string. I haven’t used that notation other than in the quick test above, so I don’t know whether that might cause problems in itself, and of course there’s lots of code missing out of your example above - are you sure nothing else happens to $city anywhere else?

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.