Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Populating multiple word string into Text Box value field

Status
Not open for further replies.

jjack46

Programmer
Jul 29, 2013
10
AU
I want to bring back records from a database into an HTML form and populate all the text boxes with values from the database. This is so I can alter the text then update it. This works perfectly ok where a text box is filled with a single word e.g. a surname. If however. I try to fill the value with a multiple word string e.g. address of "125 Smith Street", only the "125" is displayed. If I print the record to the screen, outside of the form, it displays the full address correctly showing the correct record is being retrieved from the database.
Any ideas where I am going wrong.
 
Is the text box big enough to display the whole string?
Have a look in the source code, is the whole text there?
The text should look something like this although there could bea lot more stuff in there depending where it was created.
Code:
<input type='text' name='name' value='125 Smith Street'>

Keith
 
Spaces are delimiters in HTTP so you need to URL encode the string before passing anywhere.

Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.
Webmaster Forum
 
I bring back the address from the database and assign it to a variable before I echo the variable in the value attribute.

$street = $row['Street']; This is from the database which is "125 Smith Street"
echo "Street = " . $street. "\n"; //I did this to see what was coming back from the database and it is okay.
This is the line in the form
<tr><td>Address - Street:</td><td><input type="text" name="street" maxlength="40" value=<? echo $street ?> ></td></tr>
Only the "125" is displayed.
I have tried just typing "$street = "125 Smith Street" but this doesn't work either. If I type that "125 Smith Street" directly into the value attribute it does display it.

The whole code in PHP
is
PHP:
[COLOR=#FCE94F][/color]<?php
	session_start();
	
	if (isset($_SESSION['playNo']))
		{
		$playerNo = $_SESSION['playNo'];
		unset($_SESSION['playNo']);
	}else{
		$playerNo = ($_GET['playerId']);
		echo "PlayerId = " . $playerNo. "\n";
	}
	
	
	$query = "SELECT PlayerNo, FirstName, Surname, PreferredName, Dob, Street, 
				TownSuburb, State, Postcode, Handicap, StateRep, Status FROM Player WHERE PlayerNo =  ". $playerNo ;
	$mysqli = mysqli_connect("localhost", "User", "password", "2015GolfChamps");
	if (mysqli_connect_errno($mysqli)) {
    	echo "Failed to connect to MySQL: " . mysqli_connect_error();
	}
	$res = mysqli_query($mysqli, $query);
	
	$row = mysqli_fetch_assoc($res);
	$street = $row['Street'];
	$name = $row['Surname'];
	echo "Street = " . $street. "\n";
	 
?>
<html>

<head><title>Person Record</title></head>
<h2><center>Player Details</center></h2>
<br /><br /><br /><br />
<body>
<form action="addPlayer.php" method="POST">
<center><table vertical-align: top>
	
	<tr><td><input type="hidden" name="playerNo" value=<?php echo $row['PlayerNo'] ?> ></td></tr>
	<tr><td>Surname:</td><td><input type="text" name="surname" value=<?php echo $name ?> ></td></tr>
    <tr><td>First Name:</td><td><input type="text" name="firstName" value=<?php echo $row['FirstName'] ?> ></td></tr>
    <tr><td>Preferred Name:</td><td><input type="text" name="preferredName" value=<?php echo $row['PreferredName'] ?> ></td></tr>
    <tr><td>Date of Birth:</td><td><input type="text" name="preferredName" value=<?php echo $row['Dob'] ?> ></td></tr>
	<tr><td>Address - Street:</td><td><input type="text" name="street" maxlength="40" value=<? echo $street ?> ></td></tr>
	<tr><td>Town/Suburb:</td><td><input type="text" name="suburb" value=<?php echo $row['TownSuburb'] ?> ></td></tr>
	<tr><td>State:</td><td><input type="text" name="state" value=<?php echo $row['State'] ?> ></td></tr>
	<tr><td>Postcode:</td><td><input type="text" name="postcode" value=<?php echo $row['Postcode'] ?> ></td></tr>
    <tr><td>Handicap:</td><td><input type="text" name="handicap" value=<?php echo $row['Handicap'] ?> ></td></tr>
 	<tr><td>State playing for:</td><td><input type="text" name="stateRep" value=<?php echo $row['StateRep'] ?> ></td></tr>
 	<tr><td>Status:</td><td><input type="text" name="status" value=<?php echo $row['Status'] ?> ></td></tr>
 	<tr><td>&nbsp</td></tr>
 	<tr><td>&nbsp</td></tr>

</table></center>
<center><input type="submit" value="Update record"/></center>
</form>	
	
<center><a href="index.html">Cancel</a></center>
</body>
</html>
 
What does the code look like AFTER it has been delivered to a browser?


And this:

value=<? echo $street ?> >

Should be value="<? echo $street ?>" >

Otherwise the space will be used as a delimiter for the value.

Chris.

Indifference will be the downfall of mankind, but who cares?
Time flies like an arrow, however, fruit flies like a banana.
Webmaster Forum
 
Chris is right. Spaces are delimiters for values in HTML. If you don't quote the value you output the browser ends it at the first space, and takes the rest of the string as other properties of the html element.

Code:
<input type="text" name="street" maxlength="40" value=[red][b]"[/b][/red]<? echo $street ?>[red][b]"[/b][/red]


----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
That's it. Thank you very much. It's okay now.
John
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top