Jump to content

Archived

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

stevemreid

Calling a session variable doesnt show data after a space!

Recommended Posts

Hey all,
Im a newb and have came across a problem which I hope you guys can help with. I've got a form that submits some info into a mysql database. It works and all the data goes into the database, and once you have submitted your info on the form you can press a button on the form page which loads the previously submitted values back into the form. It nearly works in that it returns all the data unless theres a space in it - i.e if one form field contains the word "Jones" it will return it , but if it was "Mr Jones" it would only return "Mr".

Heres some code if its needed:

On the php page that "inserts" the data into the database I have this:

$address4=$_POST['address4'];
$postcode=$_POST['postcode'];

then

//put all the variables into a session variable so there accesible across the whole site
foreach($_POST as $key => $value) $_SESSION[$key] = $value;

Once you press the button to reload the previous values the code to populate the form is :

<td><strong>Address 4</strong></td>
      <td><input name='address4' type='text' id='address4' value=$_SESSION[address4]></td>
    </tr>
    <tr>
      <td><strong>Postcode<span class='rederror'>*</span></strong></td>
      <td><input name='postcode' type='text' id='postcode' value=$_SESSION[postcode]></td>
    </tr>
  <tr>

The mysql table fields in question are varchars.

Could someone point me in the right direction?

Thanks!
Steve

Share this post


Link to post
Share on other sites
You need to surround the value of of the value attribute with quotes:
[code]<input name='postcode' type='text' id='postcode' value="{$_SESSION['postcode']}">[/code]

Also i you start your echo statement with an " then you'll need to escape any instance of " within your echo statament, apart from the ending ". Like so:
[code]echo "<input name=\"postcode\" type=\"text\" id=\"postcode" value=\"{$_SESSION['postcode']}\">";[/code]
Or if you are echoing out large chuncks of HTML use HEREDOC:
[code]echo <<<HTML
td><strong>Address 4</strong></td>
      <td><input name="address4" type="text" id="address4" value="{$_SESSION['address4']}"></td>
    </tr>
    <tr>
      <td><strong>Postcode<span class="rederror">*</span></strong></td>
      <td><input name="postcode" type="text" id="postcode" value="{$_SESSION['postcode']}"></td>
    </tr>
      <tr>
HTML;
[/code]

Share this post


Link to post
Share on other sites
So simple when you know how but I may not have ever worked it out by myself.
Thank you so much!
Steve

Share this post


Link to post
Share on other sites
No problem, Its a common problem most new comers face, I even fell into the same trap too when I started.

Share this post


Link to post
Share on other sites

×

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.